//BLE相关库
#include <NimBLEDevice.h>
#include <NimBLEAdvertisedDevice.h>
// 定义BLE服务UUID和特征UUID
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
//库函数定义
BLEServer *pServer;
BLEService *pService;
BLECharacteristic *pCharacteristic;
BLEScan *pBLEScan;
//BLE输入指令回调函数,处理BLE指令
class MyCharacteristicCallbacks : public NimBLECharacteristicCallbacks {
void onWrite(NimBLECharacteristic *pCharacteristic) {
std::string value = pCharacteristic->getValue();
// 在这里处理收到的数据,根据需要调整引脚电平
if (value == "LOCK") {
digitalWrite(S2_PIN, HIGH);
client.publish(topicLockstate, "XXXXX", true);
client.publish(topicSwitch2state, "ON", true);
delay(500);
digitalWrite(S2_PIN, LOW);
client.publish(topicSwitch2state, "OFF", true);
}
else if (value == "UNLOCK") {
digitalWrite(S1_PIN, HIGH);
client.publish(topicLockstate, "XXXXX", true);
client.publish(topicSwitch1state, "ON", true);
delay(500);
digitalWrite(S1_PIN, LOW);
client.publish(topicSwitch1state, "OFF", true);
}
else if (value == "XXXXX") {
digitalWrite(S4_PIN, LOW);
client.publish(topicSwitch4state, "OFF", true);
}
}
};
//BeaconScan回调函数,处理BeaconScan结果
class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice *advertisedDevice) {
// 获取设备的UUID
std::string deviceMAC = advertisedDevice->getAddress().toString();
if (deviceMAC == "XXXXXXXXXXXX") {
foundbeacon = true;
// 获取设备的RSSI值
beaconrssi = advertisedDevice->getRSSI();
}
}
};
void setup() {
//BLE设置及服务
NimBLEDevice::setPower(ESP_PWR_LVL_P9);
NimBLEDevice::init("Lilygo_BLE"); // 初始化 BLE 设备
NimBLEDevice::setSecurityAuth(true, true, true);
NimBLEDevice::setSecurityPasskey(104707); //BLE连接配对密码
NimBLEDevice::setSecurityIOCap(BLE_HS_IO_DISPLAY_ONLY);
NimBLEServer* pServer = NimBLEDevice::createServer();
NimBLEService* pService = pServer->createService(SERVICE_UUID);
NimBLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::WRITE_AUTHEN);
pCharacteristic->setCallbacks(new MyCharacteristicCallbacks());
pService->start();
NimBLEAdvertising *pAdvertising = NimBLEDevice::getAdvertising();
pAdvertising->addServiceUUID(pService->getUUID());
pAdvertising->start();
//BeaconScan设置
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
pBLEScan->setInterval(150);
pBLEScan->setWindow(80); // less or equal setInterval value
void loop() {
//Beacon Key
if (beaconkey == true){
handlebeaconkey();
}
}
//功能函数定义-------------------------------------------------------------
//Beaconkey解锁车
void handlebeaconkey() {
BLEDevice::getScan()->start(2, false);
client.publish("LilygoTSIM1/beacon_rssi", String(beaconrssi).c_str(), true);
if (carlocked == false) {
if (foundbeacon & beaconrssi <= -90) {
digitalWrite(S2_PIN, HIGH);
client.publish(topicLockstate, "LOCKED", true);
client.publish(topicSwitch2state, "ON", true);
delay(500);
digitalWrite(S2_PIN, LOW);
client.publish(topicSwitch2state, "OFF", true);
delay(100);
carlocked = true;
}
} else {
if (foundbeacon & beaconrssi >= -75) {
digitalWrite(S1_PIN, HIGH);
client.publish(topicLockstate, "UNLOCKED", true);
client.publish(topicSwitch1state, "ON", true);
delay(500);
digitalWrite(S1_PIN, LOW);
client.publish(topicSwitch1state, "OFF", true);
delay(100);
carlocked = false;
}
}