#include <Arduino.h>
#include <NimBLEDevice.h>
#include <NimBLEAdvertisedDevice.h>
#include "NimBLEEddystoneURL.h"
#include "NimBLEEddystoneTLM.h"
#include "NimBLEBeacon.h"
#include <WiFi.h>
#include <PubSubClient.h>
const char *ssid = "k";
const char *password = "passwd";
const char *mqtt_server = "192.168.1.62";
const char *topic = "ble/open";
const char *mqtt_username = "idummy";
const char *mqtt_password = "passwd";
WiFiClient espClient;
PubSubClient client(espClient);
const int blueLedPin = 2;
BLEAdvertising *pAdvertising;
bool mqttMessageReceived = false;
void callback(char *topic, byte *payload, unsigned int length)
{
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
// Print payload
for (int i = 0; i < length; i++)
{
Serial.print((char)payload[i]);
}
Serial.println();
// 将 payload 转换为字符串
String receivedMessage = "";
for (int i = 0; i < length; i++)
{
receivedMessage += (char)payload[i];
}
// 检查 payload 内容是否为 "22"
if (receivedMessage == "22") {
Serial.println("Received message is '22'. Sending BLE broadcast.");
publishBLE();
}
}
void onBLEDataReceived(const char *data)
{
Serial.print("Received BLE data: ");
Serial.println(data);
}
void setup()
{
Serial.begin(115200);
connectWiFi();
setupBLE();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop()
{
if (WiFi.status() != WL_CONNECTED)
{
connectWiFi();
}
if (!client.connected())
{
reconnect();
}
client.loop();
delay(1000);
}
void connectWiFi()
{
Serial.println("Connecting to WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.println("Connecting...");
}
Serial.println("Connected to WiFi");
}
void setupBLE()
{
NimBLEDevice::init("MyESP32");
NimBLEServer *pServer = NimBLEDevice::createServer();
pAdvertising = pServer->getAdvertising();
NimBLEAdvertisementData advertisementData;
advertisementData.setFlags(0x06);
advertisementData.setManufacturerData({0x46, 0x00, 0x08, 0x36, 0xc9, 0x54, 0xa4, 0x23, 0x60, 0xFF, 0xFF, 0xFF, 0x30, 0x43, 0x52, 0x4B, 0x54, 0x4D});
pAdvertising->setAdvertisementData(advertisementData);
pAdvertising->start();
delay(1000);
pAdvertising->stop();
}
void reconnect()
{
while (!client.connected())
{
Serial.print("Attempting MQTT connection...");
if (client.connect("ESP32Client", mqtt_username, mqtt_password))
{
Serial.println("connected");
client.subscribe(topic);
mqttMessageReceived = false; // Reset the flag when reconnecting
}
else
{
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void publishBLE()
{
Serial.println("publishBLE: " + String(mqttMessageReceived));
String bleData = "okokokok6";
NimBLEAdvertising *pAdvertising = NimBLEDevice::getAdvertising();
NimBLEAdvertisementData advertisementData;
advertisementData.setFlags(0x06);
advertisementData.setManufacturerData({0x46, 0x00, 0x08, 0x36, 0xc9, 0x54, 0xa4, 0x23, 0x60, 0xFF, 0xFF, 0xFF, 0x30, 0x43, 0x52, 0x4B, 0x54, 0x4D});
pAdvertising->setAdvertisementData(advertisementData);
pAdvertising->start();
delay(1000);
pAdvertising->stop();
client.publish(topic, bleData.c_str());
}