123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- #include "esp_gap_ble_api.h"
- #include "BLEUUID.h"
- #include "BLEAdvertising.h"
- #include "WS_Bluetooth.h"
- #include "WS_GPIO.h"
- #include <ArduinoJson.h>
- #include <MacroDebugger.h>
- 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.");
- }
|