13#include "../../Defines.h"
14#define pettutorApproach
17static NimBLEServer* _pBLEServer;
31#define MAX_MESSAGE 600
55 void onConnect(NimBLEServer* pServer)
57 SerialInfo.println(
"Client connected");
58 SerialInfo.println(
"Multi-connect support: start advertising");
59 NimBLEDevice::startAdvertising();
64 void onConnect(NimBLEServer* pServer, ble_gap_conn_desc* desc)
66 SerialInfo.print(
"Client address: ");
67 SerialInfo.println(NimBLEAddress(desc->peer_ota_addr).toString().c_str());
76 pServer->updateConnParams(desc->conn_handle, 24, 48, 0, 60);
82 void onDisconnect(NimBLEServer* pServer)
84 SerialInfo.println(
"Client disconnected - start advertising");
85 NimBLEDevice::startAdvertising();
87 void onMTUChange(uint16_t MTU, ble_gap_conn_desc* desc)
89 SerialInfo.printf(
"MTU updated: %u for connection ID: %u\n", MTU, desc->conn_handle);
94 uint32_t onPassKeyRequest()
96 SerialDebug.println(
"Server Passkey Request");
103 bool onConfirmPIN(uint32_t pass_key)
105 SerialDebug.print(
"The passkey YES/NO number: "); SerialDebug.println(pass_key);
110 void onAuthenticationComplete(ble_gap_conn_desc* desc)
113 if (!desc->sec_state.encrypted)
115 NimBLEDevice::getServer()->disconnect(desc->conn_handle);
116 SerialDebug.println(
"Encrypt connection failed - disconnecting client");
119 SerialDebug.println(
"Starting BLE work!");
128 void onRead(NimBLECharacteristic* pCharacteristic)
130 SerialDebug.print(pCharacteristic->getUUID().toString().c_str());
131 SerialDebug.print(
": onRead(), value: ");
133 SerialDebug.println(pCharacteristic->getValue().c_str());
137 void onWrite(NimBLECharacteristic* pCharacteristic)
139 SerialDebug.print(pCharacteristic->getUUID().toString().c_str());
140 SerialDebug.print(
": onWrite(), value: ");
147 std::string value = pCharacteristic->getValue();
148 SerialDebug.println(value.c_str());
150 if (value.length() > 0) {
151 SerialDebug.println(
"*********");
152 SerialDebug.printf(
"New value: %d - \n", value.length());
155 for (
int i = 0; i < value.length(); i++)
159 if (i >= value.length())
165 SerialDebug.println(pCharacteristic->getUUID().toString().c_str());
170 {SerialInfo.println(
"pChar->setValue(_ESP_32)");
172 pCharacteristic->setValue((
char*)
"_ESP_32");
180 SerialDebug.println(
"*** performing an M5 specific command from BLE trigger");
192 takePicture_CameraModule();
195#ifdef NOT_NOW_MQTT_NETWORKING
199 if (processJSONMessage(ascii))
202 pCharacteristic->setValue(0x01);
207 SerialInfo.println(
"Perform FEED");
212 pCharacteristic->setValue(0x01);
227 SerialDebug.println(
"*********");
239 void onNotify(NimBLECharacteristic * pCharacteristic)
241 SerialDebug.println(
"Sending notification to clients");
248 void onStatus(NimBLECharacteristic * pCharacteristic, Status status,
int code)
250#if (SERIAL_DEBUG_DEBUG)
251 String str = (
"Notification/Indication status code: ");
253 str +=
", return code: ";
256 str += NimBLEUtils::returnCodeToString(code);
257 SerialDebug.println(str);
261 void onSubscribe(NimBLECharacteristic * pCharacteristic, ble_gap_conn_desc * desc, uint16_t subValue)
263#if (SERIAL_DEBUG_DEBUG)
265 String str =
"Client ID: ";
266 str += desc->conn_handle;
268 str += std::string(NimBLEAddress(desc->peer_ota_addr)).c_str();
270 str +=
" Unsubscribed to ";
272 else if (subValue == 1) {
273 str +=
" Subscribed to notfications for ";
275 else if (subValue == 2) {
276 str +=
" Subscribed to indications for ";
278 else if (subValue == 3) {
279 str +=
" Subscribed to notifications and indications for ";
281 str += std::string(pCharacteristic->getUUID()).c_str();
283 SerialDebug.println(str);
291 void onWrite(NimBLEDescriptor* pDescriptor)
293#if (SERIAL_DEBUG_DEBUG)
297 SerialDebug.print(
"Descriptor written value:");
298 SerialDebug.println(pDescriptor->getValue());
300 std::string dscVal((
char*)pDescriptor->getValue(), pDescriptor->getLength());
301 SerialDebug.print(
"Descriptor written value:");
302 SerialDebug.println(dscVal.c_str());
308 void onRead(NimBLEDescriptor* pDescriptor)
310 SerialDebug.print(pDescriptor->getUUID().toString().c_str());
311 SerialDebug.println(
"Descriptor read");
331#ifdef originalApproach
332 if (_pBLEServer->getConnectedCount()) {
333 NimBLEService* pSvc = pServer->getServiceByUUID(
"BAAD");
335 NimBLECharacteristic* pChr = pSvc->getCharacteristic(
"F00D");
342#ifdef pettutorApproach
343 if (_pBLEServer->getConnectedCount()) {
344 NimBLEService* pSvc = _pBLEServer->getServiceByUUID(
_SERVICE_UUID);
361 SerialMin.printf(
"setupBLEServerNetworking(%s,%s,%s,%s)\n", serviceName?serviceName:
"NULL", deviceName?deviceName:
"NULL", serviceUUID?serviceUUID:
"NULL", characteristicUUID?characteristicUUID:
"NULL");
364 SerialTemp.print(
"Stored DeviceName = ");
365 SerialTemp.println(storedDeviceName);
366 if (strcmp(serviceName,
"PTClicker")==0)
373#ifdef FORCE_USE_BLE_SERVER_DEVICE_NAME
386 SerialMin.print(
"Setting BLE serviceName: ");
396 NimBLEDevice::setPower(ESP_PWR_LVL_P9);
412 NimBLEDevice::setSecurityAuth( BLE_SM_PAIR_AUTHREQ_SC);
415 _pBLEServer = NimBLEDevice::createServer();
416 _pBLEServer->setCallbacks(
new BLEServerCallbacks());
418 NimBLEService* pDeadService = _pBLEServer->createService(
"DEAD");
420 NimBLECharacteristic* pBeefCharacteristic = pDeadService->createCharacteristic(
424 NIMBLE_PROPERTY::READ |
425 NIMBLE_PROPERTY::WRITE |
427 NIMBLE_PROPERTY::READ_ENC |
428 NIMBLE_PROPERTY::WRITE_ENC
432#ifdef OLD_BLE_NO_ACK_APPROACH
440 pBeefCharacteristic->setValue(deviceName);
447 pBeefCharacteristic->setCallbacks(&_characteristicBLEServerCallbacks);
454 NimBLE2904* pBeef2904 = (NimBLE2904*)pBeefCharacteristic->createDescriptor(
"2904");
455 pBeef2904->setFormat(NimBLE2904::FORMAT_UTF8);
456 pBeef2904->setCallbacks(&_descriptorBLEServerCallbacks);
459#ifdef originalApproach
460 NimBLEService* pBaadService = pServer->createService(
"BAAD");
461 NimBLECharacteristic* pFoodCharacteristic = pBaadService->createCharacteristic(
463 NIMBLE_PROPERTY::READ |
464 NIMBLE_PROPERTY::WRITE |
465 NIMBLE_PROPERTY::NOTIFY
468#ifdef pettutorApproach
469 NimBLEService* pBaadService = _pBLEServer->createService(
_SERVICE_UUID);
470 NimBLECharacteristic* pFoodCharacteristic = pBaadService->createCharacteristic(
472 NIMBLE_PROPERTY::READ |
473 NIMBLE_PROPERTY::WRITE
478#ifdef originalApproach
479 pFoodCharacteristic->setValue(
"Fries");
482 pFoodCharacteristic->setCallbacks(&_characteristicBLEServerCallbacks);
488#ifdef originalApproach
490 NimBLEDescriptor* pC01Ddsc = pFoodCharacteristic->createDescriptor(
492 NIMBLE_PROPERTY::READ |
493 NIMBLE_PROPERTY::WRITE |
494 NIMBLE_PROPERTY::WRITE_ENC,
497 pC01Ddsc->setValue(
"Send it back!");
498 pC01Ddsc->setCallbacks(&dscCallbacks);
500#ifdef pettutorApproach
502 NimBLEDescriptor* pPetTutordsc = pFoodCharacteristic->createDescriptor(
504 NIMBLE_PROPERTY::READ |
505 NIMBLE_PROPERTY::WRITE |
508 pPetTutordsc->setValue(
"feed s/a/j type u/m ");
509 pPetTutordsc->setCallbacks(&_descriptorBLEServerCallbacks);
512#ifdef CURRENT_BLE_ACK_APPROACH
519#ifdef TRY_THIRD_SERVICE_WITH_NAME
520#define DEVICE_NAME_UUID "53636F74-7479426F79"
521#define DEVICE_NAME_SERVICE "6E616D65"
523 NimBLEService* pNameService = _pBLEServer->createService(DEVICE_NAME_SERVICE);
525 NimBLECharacteristic* pNameCharacteristic = pNameService->createCharacteristic(
529 NIMBLE_PROPERTY::READ |
530 NIMBLE_PROPERTY::WRITE |
532 NIMBLE_PROPERTY::READ_ENC |
533 NIMBLE_PROPERTY::WRITE_ENC
539 pNameCharacteristic->setValue(deviceName);
546 pNameCharacteristic->setCallbacks(&_characteristicBLEServerCallbacks);
553 NimBLE2904* pName2904 = (NimBLE2904*)pNameCharacteristic->createDescriptor(
"2904");
554 pName2904->setFormat(NimBLE2904::FORMAT_UTF8);
555 pName2904->setCallbacks(&_descriptorBLEServerCallbacks);
556 pNameService->start();
561 pDeadService->start();
562 pBaadService->start();
564 NimBLEAdvertising* pAdvertising = NimBLEDevice::getAdvertising();
566 pAdvertising->addServiceUUID(pDeadService->getUUID());
567 pAdvertising->addServiceUUID(pBaadService->getUUID());
571#ifdef TRY_THIRD_SERVICE_WITH_NAME
572 pAdvertising->addServiceUUID(pNameService->getUUID());
575 pAdvertising->setScanResponse(
true);
576 pAdvertising->start();
585 SerialInfo.println(
"Advertising Started");
592 SerialTemp.printf(
"%d sendBLEMessageACKMessage._pCharacteristic\n",
_pCharacteristic);
void setBLEServerDeviceName(char *deviceName)
sets the device name
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
char _serviceName_BLEServer[50]
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...
#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 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 ...