@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.
fe6f6353-b313-49ed-a080-d106f3c40d6f-image.png
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);
}