|
@@ -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();
|
|
|
|
|
|
|