Преглед на файлове

Added Preferences storage
Improved BT Communication for configuration options

frankvandenbos преди 5 месеца
родител
ревизия
c0959e8dc0

+ 46 - 15
Arduino/Digital Key v1/MAIN_ALL/MAIN_ALL.ino

@@ -1,4 +1,11 @@
-#include <HardwareSerial.h>     // Reference the ESP32 built-in serial port library
+#include <HardwareSerial.h>
+#include <Preferences.h>
+#include <iostream>
+#include <sstream>
+#include <stdio.h>
+#include <string.h>
+#include <WiFi.h>
+
 #include "WS_Bluetooth.h"
 #include "WS_GPIO.h"
 #include "Arduino_DebugUtils.h"
@@ -7,6 +14,28 @@
 
 unsigned long timers[6] = { 0, 0, 0, 0, 0, 0 };
 bool isOverride = false;
+Preferences preferences;
+std::string macAddress;
+
+std::string GetContactInfo()
+{
+  preferences.begin("prs-digital-key", true);
+  String result = preferences.getString("contact-info");
+  preferences.end();
+  return result.c_str();
+}
+
+void SetContactInfo(std::string info)
+{
+  preferences.begin("prs-digital-key", false);
+  preferences.putString("contact-info", info.c_str());
+  preferences.end();
+}
+
+std::string GetMacAddress()
+{
+  return macAddress;
+}
 
 void SetRelay(int relay, char status)
 {
@@ -57,6 +86,21 @@ void setup()
   Serial.begin(9600);
   Debug.timestampOn();
 
+  WiFi.mode(WIFI_STA);
+  macAddress = WiFi.macAddress().c_str();
+  
+  // uint8_t baseMac[6];
+  // char buffer[18];
+  
+  // esp_err_t ret = esp_wifi_get_mac(WIFI_IF_STA, baseMac);
+  // if (ret == ESP_OK) 
+  // {
+  //   sprintf(buffer, "%02x:%02x:%02x:%02x:%02x:%02x\n", baseMac[0], baseMac[1], baseMac[2], baseMac[3], baseMac[4], baseMac[5]);
+  //   macAddress = std::string(buffer);
+  // }
+  // else 
+  //   macAddress = "00:00:00:00:00:00";
+
   GPIO_Init();  
   Bluetooth_Init();
 }
@@ -64,20 +108,7 @@ void setup()
 /**********************************************************  While  **********************************************************/
 void loop() 
 {
-  bool override = IsOverride();
-  if (override && !isOverride)
-  {
-    isOverride = true;
-    RGB_Light(0, 60, 60); 
-    SetRelays("******");
-  }
-  else if (!override && isOverride)
-  {
-    isOverride = false;
-    RGB_Light(0, 0, 0); 
-    SetRelays("------");
-  }
-
+  
   unsigned long ms = millis();
   for (int relay=0; relay<6; relay++)
   {

+ 96 - 44
Arduino/Digital Key v1/MAIN_ALL/WS_Bluetooth.cpp

@@ -1,84 +1,136 @@
 #include "WS_Bluetooth.h"
 
 BLEServer* pServer;                                                             // Used to represent a BLE server
-BLECharacteristic* pTxCharacteristic;
-BLECharacteristic* pRxCharacteristic;
+BLECharacteristic* pInfoCharacteristic;
+//BLECharacteristic* pAddressCharacteristic;
+//BLECharacteristic* pStatusCharacteristic;
+BLECharacteristic* pCommandCharacteristic;
 
 /**********************************************************  Bluetooth   *********************************************************/
 
-class MyServerCallbacks : public BLEServerCallbacks {                           //By overriding the onConnect() and onDisconnect() functions
-    void onConnect(BLEServer* pServer) {                                        // When the Device is connected, "Device connected" is printed.
-    RGB_Light(0, 0, 60); 
+int Info_Status = 0;
+int Info_MacAddress = 1;
+int Info_ContactInfo = 2;
 
+int infoType = Info_Status;
+
+class ServerCallbacks : public BLEServerCallbacks {
+                             
+  void onConnect(BLEServer* pServer) {                                        
+    RGB_Light(0, 0, 60);
+    infoType = Info_Status;
   }
 
-  void onDisconnect(BLEServer* pServer) {                                       // "Device disconnected" will be printed when the device is disconnected
+  void onDisconnect(BLEServer* pServer) {
+    infoType = Info_Status;                              
     RGB_Light(0, 0, 0);
-
-    BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();                 // Re-broadcast so that the device can query
-    pAdvertising->addServiceUUID(SERVICE_UUID);                                 // Re-broadcast so that the device can query
-    pAdvertising->setScanResponse(true);                                        // Re-broadcast so that the device can query
-    pAdvertising->setMinPreferred(0x06);                                        // Re-broadcast so that the device can query 
-    pAdvertising->setMinPreferred(0x12);                                        // Re-broadcast so that the device can query 
-    BLEDevice::startAdvertising();                                              // Re-broadcast so that the device can query 
-    pRxCharacteristic->notify();                                                // Re-broadcast so that the device can query  
-    pAdvertising->start();                                                      // Re-broadcast so that the device can query
+    BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();                 
+    pAdvertising->addServiceUUID(SERVICE_UUID);                                 
+    pAdvertising->setScanResponse(true);                                        
+    pAdvertising->setMinPreferred(0x06);                                        
+    pAdvertising->setMinPreferred(0x12);                                        
+    BLEDevice::startAdvertising();                                               
+    pCommandCharacteristic->notify();                                                  
+    pAdvertising->start();                                                      
   }
 };
 
-class MyRXCallback : public BLECharacteristicCallbacks 
+class CommandCallback : public BLECharacteristicCallbacks 
 {
   void onWrite(BLECharacteristic* pCharacteristic) 
   {          
-    SetRelays(pCharacteristic->getValue());                 
-    pRxCharacteristic->setValue("");  
-  
+    std::string command = pCharacteristic->getValue();
+
+    if (command.find("CFG") == 0)
+      SetContactInfo(command.substr(3));
+
+    if (command.find("RLYS") == 0)
+      SetRelays(command.substr(4));
+
+    if (command.find("CONT") == 0)
+      infoType = Info_ContactInfo;
+
+    if (command.find("ADDR") == 0)
+      infoType = Info_MacAddress;
+
+    pCharacteristic->setValue("");  
   }
 
 };
 
-class MyTXCallback : public BLECharacteristicCallbacks 
+class InfoCallback : public BLECharacteristicCallbacks 
 {
   void onRead(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param) 
-  {                           
-    pTxCharacteristic->setValue(GetRelays());  
+  { 
+    std::string data = "";
+    if (infoType == Info_ContactInfo)        
+      data = GetContactInfo().c_str();
+    else if (infoType == Info_MacAddress)
+      data = GetMacAddress().c_str();
+    else
+      data = GetRelays().c_str();
+
+    std::vector<uint8_t> vector(data.begin(), data.end());
+    uint8_t *p = &vector[0];           
+    pCharacteristic->setValue(p,data.length());  
+    infoType = Info_Status;
   }
 
 };
 
+// class InfoCallback : public BLECharacteristicCallbacks 
+// {
+//   void onRead(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param) 
+//   {               
+//     std::string info = GetContactInfo();            
+//     std::vector<uint8_t> myVector(info.begin(), info.end());
+//     uint8_t *p = &myVector[0];            
+//     pCharacteristic->setValue(p,info.length());  
+//   }
+
+// };
 
-// void SetStatus(char* Data) {                                           // Send data using Bluetooth
-//   if (Data != nullptr && strlen(Data) > 0) {
-//     if (pServer->getConnectedCount() > 0) {  
-//       std::string SendValue = Data;                       
-//       pTxCharacteristic->setValue(SendValue);                                   // Set SendValue to the eigenvalue
-//       pTxCharacteristic->notify();                                              // Sends a notification to all connected devices
-//     }
+// class AddressCallback : public BLECharacteristicCallbacks 
+// {
+//   void onRead(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param) 
+//   {               
+//     std::string info = GetMacAddress();            
+//     std::vector<uint8_t> myVector(info.begin(), info.end());
+//     uint8_t *p = &myVector[0];            
+//     pCharacteristic->setValue(p,info.length());  
 //   }
-// }
+
+// };
+
 
 void Bluetooth_Init()
 {
   /*************************************************************************
   Bluetooth
   *************************************************************************/
-  BLEDevice::init("ESP32 S3 Relay 6CH");                                        // Initialize Bluetooth and start broadcasting                           
+
+  infoType = Info_Status;
+
+  BLEDevice::init("PRS Digital Key");
+
   pServer = BLEDevice::createServer();                                          
-  pServer->setCallbacks(new MyServerCallbacks());                               
-  BLEService* pService = pServer->createService(SERVICE_UUID);                  
   
-  pTxCharacteristic = pService->createCharacteristic(
-                                    TX_CHARACTERISTIC_UUID,
-                                    BLECharacteristic:: PROPERTY_READ);         // The eigenvalues are readable and can be read by remote devices
-  pTxCharacteristic->setCallbacks(new MyTXCallback());
-
-  pRxCharacteristic = pService->createCharacteristic(
-                                    RX_CHARACTERISTIC_UUID,
-                                    BLECharacteristic::PROPERTY_WRITE);         // The eigenvalues are writable and can be written to by remote devices
+  pServer->setCallbacks(new ServerCallbacks());                               
+  
+  BLEService* pService = pServer->createService(SERVICE_UUID);                  
   
-  pRxCharacteristic->setCallbacks(new MyRXCallback());
+  pInfoCharacteristic = pService->createCharacteristic(INFO_CHARACTERISTIC_UUID,BLECharacteristic::PROPERTY_READ);       
+  pInfoCharacteristic->setCallbacks(new InfoCallback());
+
+  // pStatusCharacteristic = pService->createCharacteristic(STATUS_CHARACTERISTIC_UUID,BLECharacteristic::PROPERTY_READ);         
+  // pStatusCharacteristic->setCallbacks(new StatusCallback());
+
+  // pAddressCharacteristic = pService->createCharacteristic(ADDRESS_CHARACTERISTIC_UUID,BLECharacteristic::PROPERTY_READ);         
+  // pAddressCharacteristic->setCallbacks(new AddressCallback());
 
-  //pRxCharacteristic->setValue("Connected!");      
+  pCommandCharacteristic = pService->createCharacteristic(COMMAND_CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_WRITE);    
+  pCommandCharacteristic->setCallbacks(new CommandCallback());
+     
   pService->start();   
 
   BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();                   
@@ -87,7 +139,7 @@ void Bluetooth_Init()
   pAdvertising->setMinPreferred(0x06);                                          
   pAdvertising->setMinPreferred(0x12);                                          
   BLEDevice::startAdvertising();                                                
-  pRxCharacteristic->notify();                                                    
+  pCommandCharacteristic->notify();                                                    
   pAdvertising->start();
  
 

+ 10 - 4
Arduino/Digital Key v1/MAIN_ALL/WS_Bluetooth.h

@@ -5,14 +5,20 @@
 #include <BLEServer.h>
 #include "WS_GPIO.h"
 
-#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"                     // UUID of the server
-#define RX_CHARACTERISTIC_UUID  "beb5483e-36e1-4688-b7f5-ea07361b26a8"          // UUID of the characteristic Tx
-#define TX_CHARACTERISTIC_UUID  "beb5484a-36e1-4688-b7f5-ea07361b26a8"          // UUID of the characteristic Rx
+#define SERVICE_UUID "ce6c0b18-8df3-4bb8-ad41-350525ac1cd5"                     
+#define INFO_CHARACTERISTIC_UUID  "eef4182c-f870-4785-9fb1-635182936e54"          
+//#define ADDRESS_CHARACTERISTIC_UUID  "a8b8b6ff-1b1f-46f7-a590-cee5f3e88037"          
+//#define STATUS_CHARACTERISTIC_UUID  "9bb2f9fa-7e59-43a4-9c43-736bb508e096"          
+#define COMMAND_CHARACTERISTIC_UUID  "447c1982-77ef-49be-a39a-2920f33c31e5"  
 
 #define Bluetooth_Mode    2
 
-extern void SetRelays(std::string value);
 extern std::string GetRelays();
+extern std::string GetContactInfo();
+extern std::string GetMacAddress();
+
+extern void SetContactInfo(std::string info);
+extern void SetRelays(std::string value);
 
 void SetStatus(char * Data);     
                                       // Send data using Bluetooth

+ 1 - 7
Arduino/Digital Key v1/MAIN_ALL/WS_GPIO.cpp

@@ -26,11 +26,6 @@ bool RelayStatus(int relay)
   return digitalRead(relays[relay] > 0);
 }
 
-bool IsOverride()
-{
-  return digitalRead(GPIO_OVERRIDE > 0);
-}
-
 void GPIO_Init()
 {
   /*************************************************************************
@@ -38,8 +33,7 @@ void GPIO_Init()
   *************************************************************************/
   for (int i=0; i<6; i++)
     pinMode(relays[i], OUTPUT);
-  
-  pinMode(GPIO_OVERRIDE, INPUT);  
+    
   pinMode(GPIO_PIN_RGB, OUTPUT);                            // Initialize the control GPIO of RGB
   pinMode(GPIO_PIN_Buzzer, OUTPUT);                         // Initialize the control GPIO of Buzzer
   

+ 0 - 3
Arduino/Digital Key v1/MAIN_ALL/WS_GPIO.h

@@ -10,7 +10,6 @@
 #define GPIO_PIN_CH4      42    // CH4 Control GPIO
 #define GPIO_PIN_CH5      45    // CH5 Control GPIO
 #define GPIO_PIN_CH6      46    // CH6 Control GPIO
-#define GPIO_OVERRIDE     3
 #define GPIO_PIN_RGB      38    // RGB Control GPIO
 #define GPIO_PIN_Buzzer   21    // Buzzer Control GPIO
 
@@ -25,8 +24,6 @@ void TriggerRelay(int relay, bool closed);
 
 bool RelayStatus(int relay);
 
-bool IsOverride();
-
 void RGB_Light(uint8_t red_val, uint8_t green_val, uint8_t blue_val);
 
 void GPIO_Init();

+ 1 - 1
Arduino/ESP32_S3_Relay_6CH_Demo/MAIN_ALL/WS_Bluetooth.cpp

@@ -78,7 +78,7 @@ void Bluetooth_Init()
   BLEService* pService = pServer->createService(SERVICE_UUID);                  
   pTxCharacteristic = pService->createCharacteristic(
                                     TX_CHARACTERISTIC_UUID,
-                                    BLECharacteristic:: PROPERTY_READ);         // The eigenvalues are readable and can be read by remote devices
+                                    BLECharacteristic::PROPERTY_READ);         // The eigenvalues are readable and can be read by remote devices
   pRxCharacteristic = pService->createCharacteristic(
                                     RX_CHARACTERISTIC_UUID,
                                     BLECharacteristic::PROPERTY_WRITE);         // The eigenvalues are writable and can be written to by remote devices