chegewara / esp32-snippets

Sample ESP32 snippets and code fragments
https://leanpub.com/kolban-ESP32
Apache License 2.0
9 stars 4 forks source link

How can I rebuild BLE_Client for more than one characteristic? #9

Closed Jorfuenst closed 5 years ago

Jorfuenst commented 5 years ago

Hello:

This is my "new" BLE_Client, requesting one service and two characteristics. Based in a BLE_Client example: /* A BLE client example that is rich in capabilities. There is a lot new capabilities implemented. author unknown updated by chegewara /

define LED_REC 2

include

include

include

include // contiene algunos parámetros GATT definidos.

define ESP_GATT_UUID_TEMP_CELSIUS 0x2A1F

/* ------------------------------------------------------------/ / // Servidor Ejemplo

// Servicio static BLEUUID serviceUUID("4fafc201-1fb5-459e-8fcc-c5c9c331914b"); // Característica static BLEUUID charUUID("beb5483e-36e1-4688-b7f5-ea07361b26a8"); // */

// Servidor Custom Server

// Servicio static BLEUUID serviceUUID("9850a346-e262-48bd-9df0-f1c5e15d6c36"); // Característica static BLEUUID charUUID_Custom("b435bf89-2864-4125-b166-7b3aecd08faa"); // Caract. Creada static BLEUUID charUUID_Temp((uint16_t) ESP_GATT_UUID_TEMP_CELSIUS); // Caract. TX Temperature Celsius */

/---------------------------------------------------------------------- /

static boolean doConnect = false; static boolean connected = false; static boolean doScan = false;

static BLERemoteCharacteristic pRemoteCharacteristic1; static BLERemoteCharacteristic pRemoteCharacteristic2; static BLEAdvertisedDevice* myDevice;

static void notifyCallback( BLERemoteCharacteristic pBLERemoteCharacteristic1, BLERemoteCharacteristic pBLERemoteCharacteristic2, uint8_t pData1, uint8_t pData2, size_t length, bool isNotify) { Serial.print("Notify callback for characteristics "); Serial.print(pBLERemoteCharacteristic1->getUUID().toString().c_str()); Serial.print(pBLERemoteCharacteristic2->getUUID().toString().c_str()); Serial.print(" of data length "); Serial.println(length); Serial.print("data 1: "); Serial.print((char) pData1); Serial.print("data 2: "); Serial.print((int8_t) pData2); Serial.println(" °C"); }

class MyClientCallback : public BLEClientCallbacks { void onConnect(BLEClient* pclient) { }

void onDisconnect(BLEClient* pclient) { connected = false; Serial.println("onDisconnect"); } };

bool connectToServer() { Serial.print("Forming a connection to "); Serial.println(myDevice->getAddress().toString().c_str());

BLEClient* pClient = BLEDevice::createClient(); Serial.println(" - Created client");

pClient->setClientCallbacks(new MyClientCallback());

// Connect to the remove BLE Server. pClient->connect(myDevice); // if you pass BLEAdvertisedDevice instead of address, it will be recognized type of peer device address (public or private) Serial.println(" - Connected to server");

// Obtain a reference to the service we are after in the remote BLE server. BLERemoteService* pRemoteService = pClient->getService(serviceUUID); if (pRemoteService == nullptr) { Serial.print("Failed to find our service UUID: "); Serial.println(serviceUUID.toString().c_str()); pClient->disconnect(); return false; } Serial.println(" - Found our service");

// Obtain a reference to the characteristic in the service of the remote BLE server. pRemoteCharacteristic1 = pRemoteService->getCharacteristic(charUUID_Custom); pRemoteCharacteristic2 = pRemoteService->getCharacteristic(charUUID_Temp);

if (pRemoteCharacteristic2 == nullptr | pRemoteCharacteristic2 == nullptr ) { Serial.print("Failed to find our characteristics UUID: "); Serial.println(charUUID_Custom.toString().c_str()); Serial.println(charUUID_Temp.toString().c_str()); pClient->disconnect(); return false; } Serial.println(" - Found our characteristic");

// Read the value of the characteristic. if(pRemoteCharacteristic1->canRead()) { std::string value = pRemoteCharacteristic1->readValue(); Serial.print("The characteristic value was: "); Serial.println(value.c_str()); } if (pRemoteCharacteristic2->canRead()) { uint8_t valueTemp = pRemoteCharacteristic2->readUInt8(); Serial.print("The characteristic value was: "); Serial.println((int8_t)valueTemp); }

if (pRemoteCharacteristic1->canNotify()){ pRemoteCharacteristic1->registerForNotify(notifyCallback); } if (pRemoteCharacteristic2->canNotify()){ pRemoteCharacteristic2->registerForNotify(notifyCallback); }

connected = true; } /* Scan for BLE servers and find the first one that advertises the service we are looking for. /

class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks { /* Called for each advertising BLE server. / void onResult(BLEAdvertisedDevice advertisedDevice) { Serial.print("BLE Advertised Device found: "); Serial.println(advertisedDevice.toString().c_str());

// We have found a device, let us now see if it contains the service we are looking for. if (advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(serviceUUID)) { BLEDevice::getScan()->stop(); myDevice = new BLEAdvertisedDevice(advertisedDevice); doConnect = true; doScan = true;

} // Found our server } // onResult }; // MyAdvertisedDeviceCallbacks

void setup() {

pinMode(LED_REC, OUTPUT); Serial.begin(115200); Serial.println("Starting Arduino BLE Client application..."); BLEDevice::init("CLNT");

// Retrieve a Scanner and set the callback we want to use to be informed when we // have detected a new device. Specify that we want active scanning and start the // scan to run for 5 seconds. BLEScan* pBLEScan = BLEDevice::getScan(); pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); pBLEScan->setInterval(1349); pBLEScan->setWindow(449); pBLEScan->setActiveScan(true); pBLEScan->start(5, false); } // End of setup.

// This is the Arduino main loop function. void loop() {

// If the flag "doConnect" is true then we have scanned for and found the desired // BLE Server with which we wish to connect. Now we connect to it. Once we are // connected we set the connected flag to be true. if (doConnect == true) { if (connectToServer()) { Serial.println("We are now connected to the BLE Server."); } else { Serial.println("We have failed to connect to the server; there is nothin more we will do."); } doConnect = false; }

// If we are connected to a peer BLE Server, update the characteristic each time we are reached // with the current time since boot. if (connected) { std::string value = pRemoteCharacteristic1->readValue(); uint8_t valueTemp = pRemoteCharacteristic2->readUInt8(); Serial.print("The characteristic value was: "); Serial.println(value.c_str()); Serial.println((int8_t)valueTemp);

digitalWrite(LED_REC, HIGH); delay(500); }

digitalWrite(LED_REC, LOW); delay(500); // Delay a second between loops.

} // End of loop

Thanks

vtomanov commented 5 years ago

Do you have any example for server that publish two Characteristics by any chance ?