#include "esp_gap_ble_api.h" #include "BLEUUID.h" #include "BLEAdvertising.h" #include "WS_Bluetooth.h" #include "WS_GPIO.h" #include #include BLEServer* pServer; BLEService* pConfigService; BLECharacteristic* pConfigCharacteristic; BLEService* pControlService; BLECharacteristic* pControlCharacteristic; BLEAdvertising *pAdvertising; bool requiresrestart = false; bool isBluetoothConnected = false; /********************************************************** Bluetooth *********************************************************/ class ServerCallbacks : public BLEServerCallbacks { void onConnect(BLEServer* pServer) { DEBUGLN("BLE::Connect()"); //pAdvertising->stop(); isBluetoothConnected = true; } void onDisconnect(BLEServer* pServer) { DEBUGLN("BLE::Disconnect()"); isBluetoothConnected = false; // BLEDevice::startAdvertising(); // pConfigCharacteristic->notify(); // if (pControlService) // pControlCharacteristic->notify(); // pAdvertising->start(); UpdateAdvertising(); if (requiresrestart) Restart(); } }; class ConfigCharacteristicCallback : public BLECharacteristicCallbacks { void onRead(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param) { DEBUGLN("Config::Read()"); JsonDocument doc; doc["warning"] = GetWarningNotice(); doc["contact"] = GetContactInfo(); String json = ""; serializeJson(doc,json); pCharacteristic->setValue(json); } void onWrite(BLECharacteristic* pCharacteristic) { String json = pCharacteristic->getValue(); DEBUGLN(json); JsonDocument doc; deserializeJson(doc, json); String name = doc["name"]; String warning = doc["warning"]; String contact = doc["contact"]; String equipmentid = doc["id"]; Configure(name,warning,contact,equipmentid); pCharacteristic->setValue(""); requiresrestart = true; } }; // char json[] = "{ \"Relay00\" : 15000, \"Relay01\" : 0, \"Relay02\" : 0, \"Relay03\" : 0, \"Relay04\" : 0, \"Relay05\" : 0 }" class ControlCharacteristicCallback : public BLECharacteristicCallbacks { void onWrite(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t *param) { String json = pCharacteristic->getValue(); JsonDocument doc; deserializeJson(doc, json); char name[10]; for (int i=0; i<6; i++) { sprintf(name,"Relay%02D",i); long time = doc[name]; SetRelay(i,time); } pCharacteristic->setValue(""); } // char json[] = "{ \"Relay00\" : true, \"Relay01\" : false, \"Relay02\" : false, \"Relay03\" : false, \"Relay04\" : false, \"Relay05\" : false }" void onRead(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param) { DEBUGLN("Control::Read()"); JsonDocument doc; char name[10]; for (int i=0; i<6; i++) { sprintf(name,"Relay%02d",i); doc[name] = GetRelay(i); } String json; serializeJson(doc,json); pCharacteristic->setValue(json); } }; bool IsConnected() { return isBluetoothConnected; } void UpdateAdvertising() { DEBUGLN("UpdateAdvertising()"); pAdvertising->stop(); BLEAdvertisementData advData; advData.addData(UUID32ToAdvertisingData(0xce6c0b18)); if (pControlService) advData.addData(UUID128ToAdvertisingData(pControlService->getUUID())); advData.setManufacturerData(DeviceStatus()); pAdvertising->setAdvertisementData(advData); pAdvertising->start(); } String DeviceStatus() { String result; uint16_t companyID = 0x02E5; // Espressif's Bluetooth SIG company ID //uint16_t companyID = 0xFFFF; // Deliberately invalid for identification purposes result += (char)(companyID & 0xFF); // LSB result += (char)((companyID >> 8) & 0xFF); // MSB byte status = 0; if (RelayStatus(0)) status += 1; if (RelayStatus(1)) status += 2; if (RelayStatus(2)) status += 4; if (RelayStatus(3)) status += 8; if (RelayStatus(4)) status += 16; if (RelayStatus(5)) status += 32; result += (char)status; return result; } String UUID128ToAdvertisingData(BLEUUID uuid128) { String adField; // Get raw 16 bytes of the 128-bit UUID const uint8_t* uuidBytes = uuid128.getNative()->uuid.uuid128; // First byte: length (16 bytes data + 1 byte type) adField += (char)(16 + 1); // Second byte: type = 0x07 (complete list of 128-bit UUIDs) adField += (char)0x07; // Append 16 UUID bytes for (int i=0; i<16; i++) { adField += (char)(uuidBytes[i] & 0xFF); } return adField; } String UUID32ToAdvertisingData(uint32_t uuid32) { String adField; // 4 bytes of UUID + 1 byte type = 5 bytes total payload length adField += (char)(4 + 1); // total length adField += (char)0x05; // 0x05 = Complete List of 32-bit Service Class UUIDs // Append UUID in little-endian byte order adField += (char)(uuid32 & 0xFF); adField += (char)((uuid32 >> 8) & 0xFF); adField += (char)((uuid32 >> 16) & 0xFF); adField += (char)((uuid32 >> 24) & 0xFF); return adField; } void Bluetooth_Init() { /************************************************************************* Bluetooth *************************************************************************/ DEBUGLN("Initialising BLE..."); String devicename = GetDeviceName().substring(0,24); // Initialise the Device BLEDevice::init(devicename); pServer = BLEDevice::createServer(); pServer->setCallbacks(new ServerCallbacks()); pAdvertising = BLEDevice::getAdvertising(); // Set up the Configuration interface (always) pConfigService = pServer->createService(CONFIG_SERVICE_UUID); pConfigCharacteristic = pConfigService->createCharacteristic(CONFIG_CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE); pConfigCharacteristic->setCallbacks(new ConfigCharacteristicCallback()); pConfigService->start(); pAdvertising->addServiceUUID(CONFIG_SERVICE_UUID); String equipmentid = GetEquipmentId(); if (!equipmentid.isEmpty() && !equipmentid.equalsIgnoreCase("00000000-0000-0000-0000-000000000000")) { pControlService = pServer->createService(equipmentid); pControlCharacteristic = pControlService->createCharacteristic(CONTROL_CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE); pControlCharacteristic->setCallbacks(new ControlCharacteristicCallback()); pControlService->start(); pAdvertising->addServiceUUID(equipmentid); } //pAdvertising->setScanResponse(true); pAdvertising->setMinPreferred(0x06); pAdvertising->setMinPreferred(0x12); BLEDevice::startAdvertising(); pConfigCharacteristic->notify(); if (pControlCharacteristic) pControlCharacteristic->notify(); pAdvertising->start(); UpdateAdvertising(); DEBUGLN("BLE Initialisation complete."); }