12#include <NimBLEDevice.h>
13#include "../../Defines.h"
14#define pettutorApproach
17static NimBLEServer* _pBLEServer;
31#define MAX_MESSAGE 600
41#ifdef NOT_HERE_USE_MAIN
46void setBLEServerDeviceName(
char *deviceName)
59 void onConnect(NimBLEServer* pServer)
61 SerialInfo.println(
"Client connected");
62 SerialInfo.println(
"Multi-connect support: start advertising");
63 NimBLEDevice::startAdvertising();
68 void onConnect(NimBLEServer* pServer, ble_gap_conn_desc* desc)
70 SerialInfo.print(
"Client address: ");
71 SerialInfo.println(NimBLEAddress(desc->peer_ota_addr).toString().c_str());
80 pServer->updateConnParams(desc->conn_handle, 24, 48, 0, 60);
86 void onDisconnect(NimBLEServer* pServer)
88 SerialInfo.println(
"Client disconnected - start advertising");
89 NimBLEDevice::startAdvertising();
91 void onMTUChange(uint16_t MTU, ble_gap_conn_desc* desc)
93 SerialInfo.printf(
"MTU updated: %u for connection ID: %u\n", MTU, desc->conn_handle);
98 uint32_t onPassKeyRequest()
100 SerialDebug.println(
"Server Passkey Request");
107 bool onConfirmPIN(uint32_t pass_key)
109 SerialDebug.print(
"The passkey YES/NO number: "); SerialDebug.println(pass_key);
114 void onAuthenticationComplete(ble_gap_conn_desc* desc)
117 if (!desc->sec_state.encrypted)
119 NimBLEDevice::getServer()->disconnect(desc->conn_handle);
120 SerialDebug.println(
"Encrypt connection failed - disconnecting client");
123 SerialDebug.println(
"Starting BLE work!");
132 void onRead(NimBLECharacteristic* pCharacteristic)
134 SerialDebug.print(pCharacteristic->getUUID().toString().c_str());
135 SerialDebug.print(
": onRead(), value: ");
137 SerialDebug.println(pCharacteristic->getValue().c_str());
141 void onWrite(NimBLECharacteristic* pCharacteristic)
143 SerialDebug.print(pCharacteristic->getUUID().toString().c_str());
144 SerialDebug.print(
": onWrite(), value: ");
151 std::string value = pCharacteristic->getValue();
152 SerialDebug.println(value.c_str());
154 if (value.length() > 0) {
155 SerialDebug.println(
"*********");
156 SerialDebug.printf(
"New value: %d - \n", value.length());
159 for (
int i = 0; i < value.length(); i++)
163 if (i >= value.length())
171 SerialDebug.println(
" **** REBOOT ***");
177 SerialDebug.println(pCharacteristic->getUUID().toString().c_str());
182 {SerialInfo.println(
"pChar->setValue(_ESP_32)");
184 pCharacteristic->setValue((
char*)
"_ESP_32");
192 SerialDebug.println(
"*** performing an M5 specific command from BLE trigger");
204#ifdef NOT_NOW_MQTT_NETWORKING
208 if (processJSONMessage(ascii))
211 pCharacteristic->setValue(0x01);
216 SerialInfo.println(
"Perform FEED");
221 pCharacteristic->setValue(0x01);
236 SerialDebug.println(
"*********");
248 void onNotify(NimBLECharacteristic * pCharacteristic)
250 SerialDebug.println(
"Sending notification to clients");
257 void onStatus(NimBLECharacteristic * pCharacteristic, Status status,
int code)
259#if (SERIAL_DEBUG_DEBUG)
260 String str = (
"Notification/Indication status code: ");
262 str +=
", return code: ";
265 str += NimBLEUtils::returnCodeToString(code);
266 SerialDebug.println(str);
270 void onSubscribe(NimBLECharacteristic * pCharacteristic, ble_gap_conn_desc * desc, uint16_t subValue)
272#if (SERIAL_DEBUG_DEBUG)
274 String str =
"Client ID: ";
275 str += desc->conn_handle;
277 str += std::string(NimBLEAddress(desc->peer_ota_addr)).c_str();
279 str +=
" Unsubscribed to ";
281 else if (subValue == 1) {
282 str +=
" Subscribed to notfications for ";
284 else if (subValue == 2) {
285 str +=
" Subscribed to indications for ";
287 else if (subValue == 3) {
288 str +=
" Subscribed to notifications and indications for ";
290 str += std::string(pCharacteristic->getUUID()).c_str();
292 SerialDebug.println(str);
300 void onWrite(NimBLEDescriptor* pDescriptor)
302#if (SERIAL_DEBUG_DEBUG)
306 SerialDebug.print(
"Descriptor written value:");
307 SerialDebug.println(pDescriptor->getValue());
309 std::string dscVal((
char*)pDescriptor->getValue(), pDescriptor->getLength());
310 SerialDebug.print(
"Descriptor written value:");
311 SerialDebug.println(dscVal.c_str());
317 void onRead(NimBLEDescriptor* pDescriptor)
319 SerialDebug.print(pDescriptor->getUUID().toString().c_str());
320 SerialDebug.println(
"Descriptor read");
340#ifdef originalApproach
341 if (_pBLEServer->getConnectedCount()) {
342 NimBLEService* pSvc = pServer->getServiceByUUID(
"BAAD");
344 NimBLECharacteristic* pChr = pSvc->getCharacteristic(
"F00D");
351#ifdef pettutorApproach
352 if (_pBLEServer->getConnectedCount()) {
353 NimBLEService* pSvc = _pBLEServer->getServiceByUUID(
_SERVICE_UUID);
370 SerialMin.printf(
"setup_BLEServerNetworking(%s,%s,%s,%s)\n", serviceName?serviceName:
"NULL", deviceName?deviceName:
"NULL", serviceUUID?serviceUUID:
"NULL", characteristicUUID?characteristicUUID:
"NULL");
373 SerialTemp.printf(
"Stored DeviceName = '%s'\n", storedDeviceName);
374#define CALCULATE_BLE_NAME
375#ifdef CALCULATE_BLE_NAME
388 if (strcmp(serviceName,
"PTClicker")==0)
435 NimBLEDevice::setPower(ESP_PWR_LVL_P9);
450 NimBLEDevice::setSecurityAuth( BLE_SM_PAIR_AUTHREQ_SC);
453 _pBLEServer = NimBLEDevice::createServer();
454 _pBLEServer->setCallbacks(
new BLEServerCallbacks());
456 NimBLEService* pDeadService = _pBLEServer->createService(
"DEAD");
458 NimBLECharacteristic* pBeefCharacteristic = pDeadService->createCharacteristic(
462 NIMBLE_PROPERTY::READ |
463 NIMBLE_PROPERTY::WRITE |
465 NIMBLE_PROPERTY::READ_ENC |
466 NIMBLE_PROPERTY::WRITE_ENC
470#ifdef OLD_BLE_NO_ACK_APPROACH
478 pBeefCharacteristic->setValue(deviceName);
485 pBeefCharacteristic->setCallbacks(&_characteristicBLEServerCallbacks);
492 NimBLE2904* pBeef2904 = (NimBLE2904*)pBeefCharacteristic->createDescriptor(
"2904");
493 pBeef2904->setFormat(NimBLE2904::FORMAT_UTF8);
494 pBeef2904->setCallbacks(&_descriptorBLEServerCallbacks);
497#ifdef originalApproach
498 NimBLEService* pBaadService = pServer->createService(
"BAAD");
499 NimBLECharacteristic* pFoodCharacteristic = pBaadService->createCharacteristic(
501 NIMBLE_PROPERTY::READ |
502 NIMBLE_PROPERTY::WRITE |
503 NIMBLE_PROPERTY::NOTIFY
506#ifdef pettutorApproach
507 NimBLEService* pBaadService = _pBLEServer->createService(
_SERVICE_UUID);
508 NimBLECharacteristic* pFoodCharacteristic = pBaadService->createCharacteristic(
510 NIMBLE_PROPERTY::READ |
511 NIMBLE_PROPERTY::WRITE
516#ifdef originalApproach
517 pFoodCharacteristic->setValue(
"Fries");
520 pFoodCharacteristic->setCallbacks(&_characteristicBLEServerCallbacks);
526#ifdef originalApproach
528 NimBLEDescriptor* pC01Ddsc = pFoodCharacteristic->createDescriptor(
530 NIMBLE_PROPERTY::READ |
531 NIMBLE_PROPERTY::WRITE |
532 NIMBLE_PROPERTY::WRITE_ENC,
535 pC01Ddsc->setValue(
"Send it back!");
536 pC01Ddsc->setCallbacks(&dscCallbacks);
538#ifdef pettutorApproach
540 NimBLEDescriptor* pPetTutordsc = pFoodCharacteristic->createDescriptor(
542 NIMBLE_PROPERTY::READ |
543 NIMBLE_PROPERTY::WRITE |
546 pPetTutordsc->setValue(
"feed s/a/j type u/m ");
547 pPetTutordsc->setCallbacks(&_descriptorBLEServerCallbacks);
550#ifdef CURRENT_BLE_ACK_APPROACH
557#ifdef TRY_THIRD_SERVICE_WITH_NAME
558#define DEVICE_NAME_UUID "53636F74-7479426F79"
559#define DEVICE_NAME_SERVICE "6E616D65"
561 NimBLEService* pNameService = _pBLEServer->createService(DEVICE_NAME_SERVICE);
563 NimBLECharacteristic* pNameCharacteristic = pNameService->createCharacteristic(
567 NIMBLE_PROPERTY::READ |
568 NIMBLE_PROPERTY::WRITE |
570 NIMBLE_PROPERTY::READ_ENC |
571 NIMBLE_PROPERTY::WRITE_ENC
577 pNameCharacteristic->setValue(deviceName);
584 pNameCharacteristic->setCallbacks(&_characteristicBLEServerCallbacks);
591 NimBLE2904* pName2904 = (NimBLE2904*)pNameCharacteristic->createDescriptor(
"2904");
592 pName2904->setFormat(NimBLE2904::FORMAT_UTF8);
593 pName2904->setCallbacks(&_descriptorBLEServerCallbacks);
594 pNameService->start();
599 pDeadService->start();
600 pBaadService->start();
602 NimBLEAdvertising* pAdvertising = NimBLEDevice::getAdvertising();
604 pAdvertising->addServiceUUID(pDeadService->getUUID());
605 pAdvertising->addServiceUUID(pBaadService->getUUID());
609#ifdef TRY_THIRD_SERVICE_WITH_NAME
610 pAdvertising->addServiceUUID(pNameService->getUUID());
613 pAdvertising->setScanResponse(
true);
614 pAdvertising->start();
623 SerialInfo.println(
"Advertising Started");
630 SerialTemp.printf(
"%d sendBLEMessageACKMessage._pCharacteristic\n",
_pCharacteristic);
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
char * _SERVICE_UUID
need to device way to change these ...
char * _deviceName_BLEServer
device name
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]
#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).
Note: the onRead and onWrite are from the perspective of the client. So onRead is the client calling ...