Automatic pairing of ESP32 using BLE
-
// Server side code /* A connect hander associated with the server starts a background task that performs notification every couple of seconds. */ #include <BLEDevice.h> #include <BLEServer.h> #include <BLEUtils.h> #include <BLE2902.h> #ifdef __cplusplus extern "C" { #endif uint8_t temprature_sens_read(); #ifdef __cplusplus } #endif uint8_t temprature_sens_read(); BLEServer* pServer = NULL; BLECharacteristic* pCharacteristic = NULL; bool deviceConnected = false; bool oldDeviceConnected = false; float mea = 0; int LED13 = 5; // The on-board Arduion LED // https://www.uuidgenerator.net/ #define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b" #define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8" class MyServerCallbacks: public BLEServerCallbacks { void onConnect(BLEServer* pServer) { deviceConnected = true; }; void onDisconnect(BLEServer* pServer) { deviceConnected = false; } }; void setup() { Serial.begin(115200); // Create the BLE Device BLEDevice::init("ESP32"); // Create the BLE Server pServer = BLEDevice::createServer(); pServer->setCallbacks(new MyServerCallbacks()); // Create the BLE Service BLEService *pService = pServer->createService(SERVICE_UUID); // Create a BLE Characteristic pCharacteristic = pService->createCharacteristic( CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY ); // https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml // Create a BLE Descriptor pCharacteristic->addDescriptor(new BLE2902()); // Start the service pService->start(); // Start advertising BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); pAdvertising->addServiceUUID(SERVICE_UUID); pAdvertising->setScanResponse(false); pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter BLEDevice::startAdvertising(); pinMode(LED13, OUTPUT); Serial.println("Waiting a client connection to notify..."); } void loop() { // notify changed value // Read the PulseSensor's value. // Assign this value to the "Signal" variable. if (deviceConnected) { mea = (temprature_sens_read()-32)/1.8; String send_Data = String(mea); pCharacteristic->setValue(send_Data); pCharacteristic->notify(); // bluetooth stack will go into congestion, if too many packets are sent, in 6 hours test i was able to go as low as 3ms } else { digitalWrite(LED13, LOW); // Else, the sigal must be below "550", so "turn-off" this LED. } delay(10); }
The following code is for the Client side.
// Client Side code #include "BLEDevice.h" #include "BLEScan.h" // The remote service we wish to connect to. static BLEUUID serviceUUID("4fafc201-1fb5-459e-8fcc-c5c9c331914b"); // The characteristic of the remote service we are interested in. static BLEUUID charUUID("beb5483e-36e1-4688-b7f5-ea07361b26a8"); static boolean doConnect = false; static boolean connected = false; static boolean doScan = false; static BLERemoteCharacteristic* pRemoteCharacteristic; static BLEAdvertisedDevice* myDevice; static void notifyCallback( BLERemoteCharacteristic* pBLERemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify) { Serial.print("Notify callback for characteristic "); Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str()); Serial.print(" of data length "); Serial.println(length); Serial.print("data: "); Serial.println((char*)pData); } 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. pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID); if (pRemoteCharacteristic == nullptr) { Serial.print("Failed to find our characteristic UUID: "); Serial.println(charUUID.toString().c_str()); pClient->disconnect(); return false; } Serial.println(" - Found our characteristic"); // Read the value of the characteristic. if(pRemoteCharacteristic->canRead()) { std::string value = pRemoteCharacteristic->readValue(); Serial.print("The characteristic value was: "); Serial.println(value.c_str()); } if(pRemoteCharacteristic->canNotify()) pRemoteCharacteristic->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() { Serial.begin(115200); Serial.println("Starting Arduino BLE Client application..."); BLEDevice::init(""); // 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; } delay(1000); // Delay a second between loops. } // End of loop
-
@kowshik1729 You are trying to pass a string value (send_Data) to a pointer object(setValue), to solve that you should use std::string class which is an instantiation of the basic_string class template that uses char (i.e., bytes) as its character type.
Comment line no.96 which doing convertion from Float value into String using the String constructor
// String send_Data = String(mea);
and add the following line to use the std:string this will help represent the sequence of characters as an object of the class .
std::string send_Data((char*)&mea, 2);
edited code
// Server side code /* A connect hander associated with the server starts a background task that performs notification every couple of seconds. */ #include <BLEDevice.h> #include <BLEServer.h> #include <BLEUtils.h> #include <BLE2902.h> #include <BLECharacteristic.h> #ifdef __cplusplus extern "C" { #endif uint8_t temprature_sens_read(); #ifdef __cplusplus } #endif uint8_t temprature_sens_read(); BLEServer* pServer = NULL; BLECharacteristic* pCharacteristic = NULL; bool deviceConnected = false; bool oldDeviceConnected = false; float mea = 0; int LED13 = 5; // The on-board Arduion LED // https://www.uuidgenerator.net/ #define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b" #define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8" class MyServerCallbacks: public BLEServerCallbacks { void onConnect(BLEServer* pServer) { deviceConnected = true; }; void onDisconnect(BLEServer* pServer) { deviceConnected = false; } }; void setup() { Serial.begin(115200); // Create the BLE Device BLEDevice::init("ESP32"); // Create the BLE Server pServer = BLEDevice::createServer(); pServer->setCallbacks(new MyServerCallbacks()); // Create the BLE Service BLEService *pService = pServer->createService(SERVICE_UUID); // Create a BLE Characteristic pCharacteristic = pService->createCharacteristic( CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY ); // https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml // Create a BLE Descriptor pCharacteristic->addDescriptor(new BLE2902()); // Start the service pService->start(); // Start advertising BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); pAdvertising->addServiceUUID(SERVICE_UUID); pAdvertising->setScanResponse(false); pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter BLEDevice::startAdvertising(); pinMode(LED13, OUTPUT); Serial.println("Waiting a client connection to notify..."); } void loop() { // notify changed value // Read the PulseSensor's value. // Assign this value to the "Signal" variable. if (deviceConnected) { mea = (temprature_sens_read()-32)/1.8; // String send_Data = String(mea); std::string send_Data((char*)&mea, 2); pCharacteristic->setValue(send_Data); pCharacteristic->notify(); // bluetooth stack will go into congestion, if too many packets are sent, in 6 hours test i was able to go as low as 3ms } else { digitalWrite(LED13, LOW); // Else, the sigal must be below "550", so "turn-off" this LED. } delay(10); }
Try it out.
-
@salmanfaris Thanks for correcting me back. I implemented this on my ESP's and the data that I acquired on the client end is very weird as shown in the below image and whereas my client-side code remained the same as shown in the previous post.
-
@kowshik1729 The value you are getting is hex, you should convert the receved hex into to string, int or float in order to see the data.
you can also debug BLE applications using the nRF Connect by Nordic Semiconductor.
-
@salmanfaris Thanks for the reply. I have two concerns with the reply.
- I am making an application where the information from the server ESP32 must be sent only to another ESP which means I am not going to read it over phone or anything like that. So, I can't use nRF connect app for my project.
- But, anyways I used the app for debugging my project and the information received by me on the phone is something as shown in the below snapshot.
Please help me out with this sir..!!
-
@kowshik1729 The nRF app is very helpful for debugging the BLE application like I said the previous replay " The value you are getting as hex,so you should convert the received hex into to string, int or float in order to see the data. " .
-
static void notifyCallback (BLERemoteCharacteristic * pBLERemoteCharacteristic, uint8_t * pData, size_t length, bool isNotify) { Serial.print ("Notify callback for characteristic "); Serial.print (pBLERemoteCharacteristic->getUUID ().toString ().c_str ()); Serial.print (" of data length "); Serial.println (length); Serial.print ("data: "); Serial.println ((char *) pData); }
@salmanfaris With a little research I got to know I need to work out on this notifyCallBack function. I have a doubt, just by type casting the last line of code to my required data type will solve the problem?
Serial.println(int((char* )pData));
Will this solve my problem?
-
@kowshik1729 You may try it out , that's the best way to check it's working or not .
-
@salmanfaris Hello bro, I've tried the typecasting the incoming data and got the following result which is not the desired value that I want to receive. It is looking vague.
I even tried changing it to float by using the following code,
Serial.println((float)(char*)pData);
but the IDE threw an error saying
Arduino: 1.8.9 (Windows 10), Board: "ESP32 Dev Module, Disabled, Default, 240MHz (WiFi/BT), QIO, 80MHz, 4MB (32Mb), 921600, None" D:\Arduino_practises\BLE_Client\BLE_Client.ino: In function 'void notifyCallback(BLERemoteCharacteristic*, uint8_t*, size_t, bool)': BLE_Client:26:32: error: invalid cast from type 'char*' to type 'float' Serial.println((float)(char*)pData); ^ exit status 1 invalid cast from type 'char*' to type 'float'
What can I do??
-
@salmanfaris I have finally figured out a way to send any data that comes over the serial of the Server ESP32 to client ESP32 over BLE. I understood that the whole BLE code that we are dealing with, works very fine in accepting the "C Strings". So, I wrote the below function in order to accept the values coming from the Serial monitor of the server(I connected the ESP's to my computer via serial port and that's how I am able to write the values to them using serial monitor). Check out my corrected code
if(Serial.available()>0) { String sending = Serial.readString(); //Reading from the Serial monitor std::string s = sending.c_str(); //Converting the read string to so called "C String" pCharacteristic->setValue(s); pCharacteristic->notify(); }
It worked very fine in receiving the data over the Client side. But, there is a discrepancy here. A speck of data that is sent in 1st iteration is being repeated in the second one and the data from second and first iterations are being visible in the third iteration and so on.
I suspect that there is something wrong with the storage buffer(Bluetooth stack). I think that the buffer is not being cleared/flushed properly each time the incoming data is coming. Is there any way to flush the data each time the server notifies the client with a data? Please suggest me ways to do it.
The code for accepting data from the mobile phone is
// Server side code /* A connect hander associated with the server starts a background task that performs notification every couple of seconds. */ #include <BLEDevice.h> #include <BLEServer.h> #include <BLEUtils.h> #include <BLE2902.h> #include <BLECharacteristic.h> #ifdef __cplusplus extern "C" { #endif uint8_t temprature_sens_read(); #ifdef __cplusplus } #endif uint8_t temprature_sens_read(); BLEServer* pServer = NULL; BLECharacteristic* pCharacteristic = NULL; bool deviceConnected = false; bool oldDeviceConnected = false; float mea = 0; int LED13 = 5; // The on-board Arduion LED // https://www.uuidgenerator.net/ #define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b" #define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8" class MyServerCallbacks: public BLEServerCallbacks { void onConnect(BLEServer* pServer) { deviceConnected = true; BLEDevice::startAdvertising(); }; void onDisconnect(BLEServer* pServer) { deviceConnected = false; } }; void setup() { Serial.begin(115200); // Create the BLE Device BLEDevice::init("Serv"); // Create the BLE Server pServer = BLEDevice::createServer(); pServer->setCallbacks(new MyServerCallbacks()); // Create the BLE Service BLEService *pService = pServer->createService(SERVICE_UUID); // Create a BLE Characteristic pCharacteristic = pService->createCharacteristic( CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY ); // https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml // Create a BLE Descriptor pCharacteristic->addDescriptor(new BLE2902()); // Start the service pService->start(); // Start advertising BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); pAdvertising->addServiceUUID(SERVICE_UUID); pAdvertising->setScanResponse(false); pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter BLEDevice::startAdvertising(); pinMode(LED13, OUTPUT); Serial.println("Waiting a client connection to notify..."); } void loop() { // notify changed value // Read the PulseSensor's value. // Assign this value to the "Signal" variable if (deviceConnected) { if(Serial.available()>0) { String sending = Serial.readString(); std::string s =sending.c_str(); pCharacteristic->setValue(s); pCharacteristic->notify(); } // Bluetooth stack will go into congestion, if too many packets are sent, in 6 hours test i was able to go as low as 3ms } else { digitalWrite(LED13, LOW); // Else, the sigal must be below "550", so "turn-off" this LED. } delay(10); }