| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 | 
							- #include "BLEUUID.h"
 
- #include "BLEAdvertising.h"
 
- #include "WS_Bluetooth.h"
 
- #include <ArduinoJson.h>
 
- BLEServer* pServer;
 
- BLEService* pConfigService;
 
- BLECharacteristic* pConfigCharacteristic;
 
- BLEService* pControlService;
 
- BLECharacteristic* pControlCharacteristic;
 
- bool requiresrestart = false;
 
- bool isBluetoothConnected = false;
 
- /**********************************************************  Bluetooth   *********************************************************/
 
- class ServerCallbacks : public BLEServerCallbacks {
 
-                              
 
-   void onConnect(BLEServer* pServer) {                                        
 
-     BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();   
 
-     pAdvertising->stop();
 
-     isBluetoothConnected = true;  
 
-   }
 
-   void onDisconnect(BLEServer* pServer) {                              
 
-     isBluetoothConnected = false;
 
-     BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();  
 
-     // String devicename = GetDeviceName().substring(0,30);
 
-     // BLEAdvertisementData data;
 
-     // data.setName(devicename);
 
-     // pAdvertising->addServiceUUID(CONFIG_SERVICE_UUID);   
 
-     // if (pControlService)   
 
-     //   pAdvertising->addServiceUUID(pControlService->getUUID());
 
-     // pAdvertising->setScanResponse(true);                                        
 
-     // pAdvertising->setMinPreferred(0x06);                                        
 
-     // pAdvertising->setMinPreferred(0x12);                                        
 
-     BLEDevice::startAdvertising();                                               
 
-     pConfigCharacteristic->notify();  
 
-     if (pControlService)   
 
-       pControlCharacteristic->notify();                                                
 
-     pAdvertising->start();  
 
-     if (requiresrestart)
 
-       Restart();
 
-   }
 
- };
 
- class ConfigCharacteristicCallback : public BLECharacteristicCallbacks 
 
- {
 
-   
 
-   // char json[] = "{ \"warning\" : "", \"contact\" : "" }"
 
-   void onRead(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param) 
 
-   { 
 
-     Serial.println("into writeConfig");
 
-     
 
-     JsonDocument doc;
 
-     doc["warning"] = GetWarningNotice();
 
-     doc["contact"] = GetContactInfo();
 
-     String json = "";
 
-     serializeJson(doc,json);
 
-     Serial.println("serialised");
 
-     Serial.println(json);
 
-     pCharacteristic->setValue(json);  
 
-     Serial.println("done");
 
-   }
 
- // char json[] = "{ \"name\" : "PRS Digital Key", \"warning\" : "", \"contact\" : "" \"id\" : "00000000-0000-0000-0000-000000000000" }"
 
-   void onWrite(BLECharacteristic* pCharacteristic) 
 
-   {          
 
-     String json = pCharacteristic->getValue();
 
-     
 
-     Serial.println(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 
 
- {
 
-   // char json[] = "{\"Relay01\"="15000"
 
-   void onWrite(BLECharacteristic* pCharacteristic) 
 
-   {          
 
-     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) 
 
-   { 
 
-     
 
-     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 Bluetooth_Init()
 
- {
 
-   /*************************************************************************
 
-   Bluetooth
 
-   *************************************************************************/
 
-   
 
-   String devicename = GetDeviceName().substring(0,30);
 
-   
 
-   // Initialise the Device
 
-   BLEDevice::init(devicename);
 
-   pServer = BLEDevice::createServer();                                          
 
-   pServer->setCallbacks(new ServerCallbacks());  
 
-   BLEAdvertising *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"))
 
-   {
 
-     BLEService* 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();
 
-  
 
- }
 
 
  |