13#include <NimBLEDevice.h>
14#include "../../Defines.h"
16#define pettutorApproach
19static NimBLEServer* _pBLEServer;
33#define MAX_MESSAGE 600
48 void onConnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo)
override {
49 SerialDebug.printf(
"Client connected:\n%s", connInfo.toString().c_str());
59 pServer->updateConnParams(connInfo.getConnHandle(), 24, 48, 0, 180);
62 void onDisconnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo,
int reason)
override {
63 SerialDebug.printf(
"Client disconnected - start advertising\n");
64 NimBLEDevice::startAdvertising();
67 void onMTUChange(uint16_t MTU, NimBLEConnInfo& connInfo)
override {
68 SerialDebug.printf(
"MTU updated: %u for connection ID: %u\n", MTU, connInfo.getConnHandle());
72 uint32_t onPassKeyDisplay()
override {
73 SerialDebug.printf(
"Server Passkey Display\n");
81 void onConfirmPassKey(NimBLEConnInfo& connInfo, uint32_t passkey)
override {
82 SerialDebug.printf(
"The passkey YES/NO number: %" PRIu32
"\n", passkey);
84 NimBLEDevice::injectConfirmPasskey(connInfo,
true);
87 void onAuthenticationComplete(NimBLEConnInfo& connInfo)
override {
89 if (!connInfo.isEncrypted())
91 NimBLEDevice::getServer()->disconnect(connInfo.getConnHandle());
92 SerialDebug.printf(
"Encrypt connection failed - disconnecting client\n");
96 SerialDebug.printf(
"Secured connection to: %s\n", connInfo.getAddress().toString().c_str());
105 void onConnect(NimBLEServer* pServer)
107 SerialInfo.println(
"Client connected");
108 SerialInfo.println(
"Multi-connect support: start advertising");
109 NimBLEDevice::startAdvertising();
114 void onConnect(NimBLEServer* pServer, ble_gap_conn_desc* desc)
116 SerialInfo.print(
"Client address: ");
117 SerialInfo.println(NimBLEAddress(desc->peer_ota_addr).toString().c_str());
126 pServer->updateConnParams(desc->conn_handle, 24, 48, 0, 60);
132 void onDisconnect(NimBLEServer* pServer)
134 SerialInfo.println(
"Client disconnected - start advertising");
135 NimBLEDevice::startAdvertising();
137 void onMTUChange(uint16_t MTU, ble_gap_conn_desc* desc)
139 SerialInfo.printf(
"MTU updated: %u for connection ID: %u\n", MTU, desc->conn_handle);
144 uint32_t onPassKeyRequest()
146 SerialDebug.println(
"Server Passkey Request");
153 bool onConfirmPIN(uint32_t pass_key)
155 SerialDebug.print(
"The passkey YES/NO number: "); SerialDebug.println(pass_key);
160 void onAuthenticationComplete(ble_gap_conn_desc* desc)
163 if (!desc->sec_state.encrypted)
165 NimBLEDevice::getServer()->disconnect(desc->conn_handle);
166 SerialDebug.println(
"Encrypt connection failed - disconnecting client");
169 SerialDebug.println(
"Starting BLE work!");
181 void onRead(NimBLECharacteristic* pCharacteristic, NimBLEConnInfo& connInfo)
override
183 SerialDebug.print(pCharacteristic->getUUID().toString().c_str());
184 SerialDebug.print(
": onRead(), value: ");
186 SerialDebug.println(pCharacteristic->getValue().c_str());
190 void onWrite(NimBLECharacteristic* pCharacteristic, NimBLEConnInfo& connInfo)
override
192 SerialDebug.print(pCharacteristic->getUUID().toString().c_str());
193 SerialDebug.print(
": onWrite(), value: ");
195 std::string value = pCharacteristic->getValue();
196 SerialDebug.println(value.c_str());
198 if (value.length() > 0) {
199 SerialDebug.println(
"*********");
200 SerialDebug.printf(
"New value: %d - \n", value.length());
203 for (
int i = 0; i < value.length(); i++)
207 if (i >= value.length())
215 SerialDebug.println(
" **** REBOOT ***");
221 SerialDebug.println(pCharacteristic->getUUID().toString().c_str());
228 SerialDebug.println(
"*** performing an M5 specific command from BLE trigger");
247 void onStatus(NimBLECharacteristic* pCharacteristic,
int code)
override {
248 SerialLots.printf(
"Notification/Indication return code: %d, %s\n", code, NimBLEUtils::returnCodeToString(code));
252 void onSubscribe(NimBLECharacteristic* pCharacteristic, NimBLEConnInfo& connInfo, uint16_t subValue)
override {
253 std::string str =
"Client ID: ";
254 str += connInfo.getConnHandle();
256 str += connInfo.getAddress().toString();
258 str +=
" Unsubscribed to ";
259 }
else if (subValue == 1) {
260 str +=
" Subscribed to notifications for ";
261 }
else if (subValue == 2) {
262 str +=
" Subscribed to indications for ";
263 }
else if (subValue == 3) {
264 str +=
" Subscribed to notifications and indications for ";
266 str += std::string(pCharacteristic->getUUID());
268 SerialDebug.printf(
"%s\n", str.c_str());
277 void onRead(NimBLECharacteristic* pCharacteristic)
279 SerialDebug.print(pCharacteristic->getUUID().toString().c_str());
280 SerialDebug.print(
": onRead(), value: ");
282 SerialDebug.println(pCharacteristic->getValue().c_str());
286 void onWrite(NimBLECharacteristic* pCharacteristic)
288 SerialDebug.print(pCharacteristic->getUUID().toString().c_str());
289 SerialDebug.print(
": onWrite(), value: ");
296 std::string value = pCharacteristic->getValue();
297 SerialDebug.println(value.c_str());
299 if (value.length() > 0) {
300 SerialDebug.println(
"*********");
301 SerialDebug.printf(
"New value: %d - \n", value.length());
304 for (
int i = 0; i < value.length(); i++)
308 if (i >= value.length())
316 SerialDebug.println(
" **** REBOOT ***");
322 SerialDebug.println(pCharacteristic->getUUID().toString().c_str());
327 {SerialInfo.println(
"pChar->setValue(_ESP_32)");
329 pCharacteristic->setValue((
char*)
"_ESP_32");
337 SerialDebug.println(
"*** performing an M5 specific command from BLE trigger");
349#ifdef NOT_NOW_MQTT_NETWORKING
353 if (processJSONMessage(ascii))
356 pCharacteristic->setValue(0x01);
361 SerialInfo.println(
"Perform FEED");
366 pCharacteristic->setValue(0x01);
381 SerialDebug.println(
"*********");
393void onNotify(NimBLECharacteristic * pCharacteristic)
395 SerialDebug.println(
"Sending notification to clients");
402void onStatus(NimBLECharacteristic * pCharacteristic, Status status,
int code)
404#if (SERIAL_DEBUG_DEBUG)
405 String str = (
"Notification/Indication status code: ");
407 str +=
", return code: ";
410 str += NimBLEUtils::returnCodeToString(code);
411 SerialDebug.println(str);
415void onSubscribe(NimBLECharacteristic * pCharacteristic, ble_gap_conn_desc * desc, uint16_t subValue)
417#if (SERIAL_DEBUG_DEBUG)
419 String str =
"Client ID: ";
420 str += desc->conn_handle;
422 str += std::string(NimBLEAddress(desc->peer_ota_addr)).c_str();
424 str +=
" Unsubscribed to ";
426 else if (subValue == 1) {
427 str +=
" Subscribed to notfications for ";
429 else if (subValue == 2) {
430 str +=
" Subscribed to indications for ";
432 else if (subValue == 3) {
433 str +=
" Subscribed to notifications and indications for ";
435 str += std::string(pCharacteristic->getUUID()).c_str();
437 SerialDebug.println(str);
447 void onWrite(NimBLEDescriptor* pDescriptor, NimBLEConnInfo& connInfo)
override {
448 std::string dscVal = pDescriptor->getValue();
449 SerialDebug.printf(
"Descriptor written value: %s\n", dscVal.c_str());
452 void onRead(NimBLEDescriptor* pDescriptor, NimBLEConnInfo& connInfo)
override {
453 SerialDebug.printf(
"%s Descriptor read\n", pDescriptor->getUUID().toString().c_str());
462 void onWrite(NimBLEDescriptor* pDescriptor)
464#if (SERIAL_DEBUG_DEBUG)
468 SerialDebug.print(
"Descriptor written value:");
469 SerialDebug.println(pDescriptor->getValue());
471 std::string dscVal((
char*)pDescriptor->getValue(), pDescriptor->getLength());
472 SerialDebug.print(
"Descriptor written value:");
473 SerialDebug.println(dscVal.c_str());
479 void onRead(NimBLEDescriptor* pDescriptor)
481 SerialDebug.print(pDescriptor->getUUID().toString().c_str());
482 SerialDebug.println(
"Descriptor read");
499 if (_pBLEServer && _pBLEServer->getConnectedCount() >0 )
501 NimBLEService* pSvc = _pBLEServer->getServiceByUUID(
_SERVICE_UUID);
519 SerialDebug.printf(
"setup_BLEServerNetworking(%s,%s,%s,%s)\n", serviceName?serviceName:
"NULL", deviceName?deviceName:
"NULL", serviceUUID?serviceUUID:
"NULL", characteristicUUID?characteristicUUID:
"NULL");
521 SerialTemp.printf(
"Stored DeviceName = '%s'\n", storedDeviceName);
523#pragma mark CALCULATE_BLE_NAME
548#pragma mark START BLE Server
552 _pBLEServer = NimBLEDevice::createServer();
557#pragma mark DEAD Service
559 NimBLEService* pDeadService = _pBLEServer->createService(
"DEAD");
560 NimBLECharacteristic* pBeefCharacteristic = pDeadService->createCharacteristic(
"BEEF",NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::NOTIFY);
562 pBeefCharacteristic->setValue(
"Beef");
570 NimBLE2904* pBeef2904 = (NimBLE2904*)pBeefCharacteristic->createDescriptor(
"2904");
571 pBeef2904->setFormat(NimBLE2904::FORMAT_UTF8);
575#pragma mark SERVICE_UUID service (main service)
577 NimBLEService* pIDogWatchService = _pBLEServer->createService(
_SERVICE_UUID);
578 NimBLECharacteristic* pIDogWatchCharacteristic = pIDogWatchService->createCharacteristic(
_CHARACTERISTIC_UUID,NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::NOTIFY);
586 NimBLEDescriptor* pPetTutordsc = pIDogWatchCharacteristic->createDescriptor(
"C01D",NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::WRITE_ENC, 20);
595 NimBLEAdvertising* pAdvertising = NimBLEDevice::getAdvertising();
599 pAdvertising->addServiceUUID(pDeadService->getUUID());
602 pAdvertising->addServiceUUID(pIDogWatchService->getUUID());
608 pAdvertising->enableScanResponse(
true);
610 pAdvertising->setScanResponse(
true);
612 pAdvertising->start();
614 SerialInfo.println(
"Advertising Started");
621 SerialTemp.printf(
"%d sendBLEMessageACKMessage._pCharacteristic\n",
_pCharacteristic);
626#pragma mark START_OF_OLD_CODE
641#define pettutorApproach
644static NimBLEServer* _pBLEServer;
648char *_deviceName_BLEServer = NULL;
658#define MAX_MESSAGE 600
668#ifdef NOT_HERE_USE_MAIN
673void setBLEServerDeviceName(
char *deviceName)
678 _deviceName_BLEServer = deviceName;
686 void onConnect(NimBLEServer* pServer)
688 SerialInfo.println(
"Client connected");
689 SerialInfo.println(
"Multi-connect support: start advertising");
690 NimBLEDevice::startAdvertising();
695 void onConnect(NimBLEServer* pServer, ble_gap_conn_desc* desc)
697 SerialInfo.print(
"Client address: ");
698 SerialInfo.println(NimBLEAddress(desc->peer_ota_addr).toString().c_str());
707 pServer->updateConnParams(desc->conn_handle, 24, 48, 0, 60);
713 void onDisconnect(NimBLEServer* pServer)
715 SerialInfo.println(
"Client disconnected - start advertising");
716 NimBLEDevice::startAdvertising();
718 void onMTUChange(uint16_t MTU, ble_gap_conn_desc* desc)
720 SerialInfo.printf(
"MTU updated: %u for connection ID: %u\n", MTU, desc->conn_handle);
725 uint32_t onPassKeyRequest()
727 SerialDebug.println(
"Server Passkey Request");
734 bool onConfirmPIN(uint32_t pass_key)
736 SerialDebug.print(
"The passkey YES/NO number: "); SerialDebug.println(pass_key);
741 void onAuthenticationComplete(ble_gap_conn_desc* desc)
744 if (!desc->sec_state.encrypted)
746 NimBLEDevice::getServer()->disconnect(desc->conn_handle);
747 SerialDebug.println(
"Encrypt connection failed - disconnecting client");
750 SerialDebug.println(
"Starting BLE work!");
759 void onRead(NimBLECharacteristic* pCharacteristic)
761 SerialDebug.print(pCharacteristic->getUUID().toString().c_str());
762 SerialDebug.print(
": onRead(), value: ");
764 SerialDebug.println(pCharacteristic->getValue().c_str());
768 void onWrite(NimBLECharacteristic* pCharacteristic)
770 SerialDebug.print(pCharacteristic->getUUID().toString().c_str());
771 SerialDebug.print(
": onWrite(), value: ");
778 std::string value = pCharacteristic->getValue();
779 SerialDebug.println(value.c_str());
781 if (value.length() > 0) {
782 SerialDebug.println(
"*********");
783 SerialDebug.printf(
"New value: LEN = %d => \n", value.length());
786 for (
int i = 0; i < value.length(); i++)
790 if (i >= value.length())
793 SerialDebug.printf(
"_asciiMessage = '%s' = %0x\n",
_asciiMessage, value[0]);
799 SerialDebug.println(
" **** REBOOT ***");
805 SerialDebug.println(pCharacteristic->getUUID().toString().c_str());
810 {SerialInfo.println(
"pChar->setValue(_ESP_32)");
812 pCharacteristic->setValue((
char*)
"_ESP_32");
821 SerialDebug.println(
"*** performing an M5 specific command from BLE trigger");
833#ifdef NOT_NOW_MQTT_NETWORKING
837 if (processJSONMessage(ascii))
840 pCharacteristic->setValue(0x01);
845 SerialInfo.println(
"Perform FEED");
850 pCharacteristic->setValue(0x01);
865 SerialDebug.println(
"*********");
877void onNotify(NimBLECharacteristic * pCharacteristic)
879 SerialDebug.println(
"Sending notification to clients");
886void onStatus(NimBLECharacteristic * pCharacteristic, Status status,
int code)
888#if (SERIAL_DEBUG_DEBUG)
889 String str = (
"Notification/Indication status code: ");
891 str +=
", return code: ";
894 str += NimBLEUtils::returnCodeToString(code);
895 SerialDebug.println(str);
899void onSubscribe(NimBLECharacteristic * pCharacteristic, ble_gap_conn_desc * desc, uint16_t subValue)
901#if (SERIAL_DEBUG_DEBUG)
903 String str =
"Client ID: ";
904 str += desc->conn_handle;
906 str += std::string(NimBLEAddress(desc->peer_ota_addr)).c_str();
908 str +=
" Unsubscribed to ";
910 else if (subValue == 1) {
911 str +=
" Subscribed to notfications for ";
913 else if (subValue == 2) {
914 str +=
" Subscribed to indications for ";
916 else if (subValue == 3) {
917 str +=
" Subscribed to notifications and indications for ";
919 str += std::string(pCharacteristic->getUUID()).c_str();
921 SerialDebug.println(str);
929 void onWrite(NimBLEDescriptor* pDescriptor)
931#if (SERIAL_DEBUG_DEBUG)
935 SerialDebug.print(
"Descriptor written value:");
936 SerialDebug.println(pDescriptor->getValue());
938 std::string dscVal((
char*)pDescriptor->getValue(), pDescriptor->getLength());
939 SerialDebug.print(
"Descriptor written value:");
940 SerialDebug.println(dscVal.c_str());
946 void onRead(NimBLEDescriptor* pDescriptor)
948 SerialDebug.print(pDescriptor->getUUID().toString().c_str());
949 SerialDebug.println(
"Descriptor read");
950 SerialDebug.printf(
" DeviceName = %s\n", _deviceName_BLEServer?_deviceName_BLEServer:
"nil");
969#ifdef originalApproach
970 if (_pBLEServer->getConnectedCount()) {
971 NimBLEService* pSvc = pServer->getServiceByUUID(
"BAAD");
973 NimBLECharacteristic* pChr = pSvc->getCharacteristic(
"F00D");
980#ifdef pettutorApproach
981 if (_pBLEServer->getConnectedCount()) {
982 NimBLEService* pSvc = _pBLEServer->getServiceByUUID(
_SERVICE_UUID);
999 SerialMin.printf(
"setup_BLEServerNetworking(%s,%s,%s,%s)\n", serviceName?serviceName:
"NULL", deviceName?deviceName:
"NULL", serviceUUID?serviceUUID:
"NULL", characteristicUUID?characteristicUUID:
"NULL");
1002 SerialTemp.printf(
"Stored DeviceName = '%s'\n", storedDeviceName);
1003#define CALCULATE_BLE_NAME
1004#ifdef CALCULATE_BLE_NAME
1017 if (strcmp(serviceName,
"PTClicker")==0)
1064 NimBLEDevice::setPower(ESP_PWR_LVL_P9);
1079 NimBLEDevice::setSecurityAuth( BLE_SM_PAIR_AUTHREQ_SC);
1082 _pBLEServer = NimBLEDevice::createServer();
1083 _pBLEServer->setCallbacks(
new BLEServerCallbacks());
1085 NimBLEService* pDeadService = _pBLEServer->createService(
"DEAD");
1087 NimBLECharacteristic* pBeefCharacteristic = pDeadService->createCharacteristic(
1091 NIMBLE_PROPERTY::READ |
1092 NIMBLE_PROPERTY::WRITE |
1094 NIMBLE_PROPERTY::READ_ENC |
1095 NIMBLE_PROPERTY::WRITE_ENC
1099#ifdef OLD_BLE_NO_ACK_APPROACH
1107 pBeefCharacteristic->setValue(deviceName);
1114 pBeefCharacteristic->setCallbacks(&_characteristicBLEServerCallbacks);
1121 NimBLE2904* pBeef2904 = (NimBLE2904*)pBeefCharacteristic->createDescriptor(
"2904");
1122 pBeef2904->setFormat(NimBLE2904::FORMAT_UTF8);
1123 pBeef2904->setCallbacks(&_descriptorBLEServerCallbacks);
1126#ifdef originalApproach
1127 NimBLEService* pBaadService = pServer->createService(
"BAAD");
1128 NimBLECharacteristic* pFoodCharacteristic = pBaadService->createCharacteristic(
1130 NIMBLE_PROPERTY::READ |
1131 NIMBLE_PROPERTY::WRITE |
1132 NIMBLE_PROPERTY::NOTIFY
1135#ifdef pettutorApproach
1136 NimBLEService* pBaadService = _pBLEServer->createService(
_SERVICE_UUID);
1137 NimBLECharacteristic* pFoodCharacteristic = pBaadService->createCharacteristic(
1139 NIMBLE_PROPERTY::READ |
1140 NIMBLE_PROPERTY::WRITE
1145#ifdef originalApproach
1146 pFoodCharacteristic->setValue(
"Fries");
1149 pFoodCharacteristic->setCallbacks(&_characteristicBLEServerCallbacks);
1155#ifdef originalApproach
1157 NimBLEDescriptor* pC01Ddsc = pFoodCharacteristic->createDescriptor(
1159 NIMBLE_PROPERTY::READ |
1160 NIMBLE_PROPERTY::WRITE |
1161 NIMBLE_PROPERTY::WRITE_ENC,
1164 pC01Ddsc->setValue(
"Send it back!");
1165 pC01Ddsc->setCallbacks(&dscCallbacks);
1167#ifdef pettutorApproach
1169 NimBLEDescriptor* pPetTutordsc = pFoodCharacteristic->createDescriptor(
1171 NIMBLE_PROPERTY::READ |
1172 NIMBLE_PROPERTY::WRITE |
1175 pPetTutordsc->setValue(
"feed s/a/j type u/m ");
1176 pPetTutordsc->setCallbacks(&_descriptorBLEServerCallbacks);
1179#ifdef CURRENT_BLE_ACK_APPROACH
1186#ifdef TRY_THIRD_SERVICE_WITH_NAME
1187#define DEVICE_NAME_UUID "53636F74-7479426F79"
1188#define DEVICE_NAME_SERVICE "6E616D65"
1190 NimBLEService* pNameService = _pBLEServer->createService(DEVICE_NAME_SERVICE);
1192 NimBLECharacteristic* pNameCharacteristic = pNameService->createCharacteristic(
1196 NIMBLE_PROPERTY::READ |
1197 NIMBLE_PROPERTY::WRITE |
1199 NIMBLE_PROPERTY::READ_ENC |
1200 NIMBLE_PROPERTY::WRITE_ENC
1206 pNameCharacteristic->setValue(deviceName);
1213 pNameCharacteristic->setCallbacks(&_characteristicBLEServerCallbacks);
1220 NimBLE2904* pName2904 = (NimBLE2904*)pNameCharacteristic->createDescriptor(
"2904");
1221 pName2904->setFormat(NimBLE2904::FORMAT_UTF8);
1222 pName2904->setCallbacks(&_descriptorBLEServerCallbacks);
1223 pNameService->start();
1228 pDeadService->start();
1229 pBaadService->start();
1231 NimBLEAdvertising* pAdvertising = NimBLEDevice::getAdvertising();
1233 pAdvertising->addServiceUUID(pDeadService->getUUID());
1234 pAdvertising->addServiceUUID(pBaadService->getUUID());
1238#ifdef TRY_THIRD_SERVICE_WITH_NAME
1239 pAdvertising->addServiceUUID(pNameService->getUUID());
1242 pAdvertising->setScanResponse(
true);
1243 pAdvertising->start();
1252 SerialInfo.println(
"Advertising Started");
1259 SerialTemp.printf(
"%d sendBLEMessageACKMessage._pCharacteristic\n",
_pCharacteristic);
BLEServerNetworkingCharacteristicCallbacks _BLEServerNetworkingCharacteristicCallbacks
BLEServerNetworkingDescriptorCallbacks _BLEServerNetworkingDescriptorCallbacks
void loop_BLEServerNetworking()
the 'loop' for this module BLEServerNetworking.
void sendBLEMessageACKMessage()
send ACK over bluetooth, this right now is 0x01
char _asciiMessage[MAX_MESSAGE]
char * getServiceName_BLEServerNetworking()
retrieve the service name (PTFEEDER, PTFeeder:Name, PTClicker:Name, etc)
char * _CHARACTERISTIC_UUID
NimBLECharacteristic * _pCharacteristic
BLEServeNetworkingCallbacks _BLEServeNetworkingCallbacks
char * _SERVICE_UUID
need to device way to change these ...
void setup_BLEServerNetworking(char *serviceName, char *deviceName, char *serviceUUID, char *characteristicUUID)
the 'setup' for this module BLEServerNetworking. Here the service name is added (and potentially more...
char _serviceName_BLEServer[100]
device name
#define BLE_SERVER_CALLBACK_ONWRITE
void addToTextMessages_displayModule(String text)
void incrementScreenColor_displayModule()
void showText_displayModule(String text)
char * deviceName_mainModule()
gets the device name
#define CALLBACKS_BLE_SERVER
void processClientCommandChar_mainModule(char cmd)
single character version of processClientCommand (since many used that already)
void rebootDevice_mainModule()
void callCallbackMain(int callbacksModuleId, int callbackType, char *message)
performs the indirect callback based on the callbackType
boolean getPreferenceBoolean_mainModule(int preferenceID)
called to set a preference (which will be an identifier and a string, which can be converted to a num...
#define PREFERENCE_BLE_SERVER_USE_DEVICE_NAME_SETTING
if set, the BLE Server (like PTFeeder) will tack on the device name (or none if not defined).
7.25.26 trying to work with 2.1 libs..
7.25.26 trying to work with 2.1 libs..