Hello! I have been working on connecting one BLE client ESP32 to two ESP32 servers connected to BNO055 9DOF sensors. So far, I have been able to do a one to one connection and received the orientation and acceleration data. I'm not exactly sure how to adjust my code to account for another server connection. Would there be a way to only stop scanning once both servers are connected? Or is there a way to manually control connections and disconnections? Any tips or advice is appreciated
Hello !
Same project as you, I need to connect one client BLE ESP32 to 5 devices (BLE server) simultaneously (with notification activated)
I found a post https://esp32.com/viewtopic.php?t=11745 but it doesn't work
If someone could share his method, it will be great !
Regards
I manage to connect 3 BLE servers to 1 Client but no more...
No effet after changing the CONFIG_BTDM_CONTROLLER_BLE_MAX_CONN and CONFIG_BTDM_CONTROLLER_BLE_MAX_CONN_EFF from 5 to 3 in sdkconfig.h
at C:\Users\MyName\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\tools\sdk\include\config.
#include "BLEDevice.h"
// #define DEBUG
int MaxDevice = 3;
int CountDevice = 0;
String ConnectedDevices[5];
String ScannedBLEAddress[5];
static BLEAddress *ServerBLEAddress[5];
static BLEAdvertisedDevice *ServerBLE[5];
// Struct definition.
struct Array3 {
int array[3];
};
#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E" // RX Characteristic
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E" // TX Characteristic
static BLEUUID serviceUUID(SERVICE_UUID);
static BLEUUID charUUID_RX(CHARACTERISTIC_UUID_RX);
static BLEUUID charUUID_TX(CHARACTERISTIC_UUID_TX);
static boolean doConnect = false;
static boolean connected = false;
static boolean doScan = false;
static BLERemoteCharacteristic* pTXCharacteristic;
static BLERemoteCharacteristic* pRXCharacteristic;
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);
*/
#ifdef DEBUG
Serial.print((char*)pData);
Serial.println("");
Serial.print("HEX: ");
printHex(pData, sizeof(pData));
Serial.println("");
Serial.print("DEC: ");
printDec(pData, sizeof(pData));
Serial.println("");
#endif
ParseMessage(pData);
}
class MyClientCallback : public BLEClientCallbacks {
void onConnect(BLEClient* pclient) {
//CountDevice +=1;
//ConnectedDevices[CountDevice]= myDevice->getAddress().toString().c_str();
Serial.printf("CountConnectedDevice = %i\n", CountDevice);
}
void onDisconnect(BLEClient* pclient) {
connected = false;
Serial.println("onDisconnect");
//CountDevice -=1;
}
};
bool connectToServer() {
Serial.print("Forming a connection to ");
//Serial.println(myDevice->getAddress().toString().c_str());
Serial.println(ServerBLE[CountDevice]->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);
pClient->connect(ServerBLE[CountDevice]);
// 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.
// charUUID_TX
pTXCharacteristic = pRemoteService->getCharacteristic(charUUID_TX);
if (pTXCharacteristic == nullptr) {
Serial.print("Failed to find our characteristic UUID: ");
Serial.println(charUUID_TX.toString().c_str());
pClient->disconnect();
return false;
}
Serial.println(" - Found charUUID_TX characteristic");
// Read the value of the characteristic.
if(pTXCharacteristic->canRead()) {
std::string value = pTXCharacteristic->readValue();
Serial.print("The characteristic value was: ");
Serial.println(value.c_str());
}
if(pTXCharacteristic->canNotify())
pTXCharacteristic->registerForNotify(notifyCallback);
// ======================================================
// Obtain a reference to the characteristic in the service of the remote BLE server.
// charUUID_RX
pRXCharacteristic = pRemoteService->getCharacteristic(charUUID_RX);
if (pRXCharacteristic == nullptr) {
Serial.print("Failed to find our characteristic UUID: ");
Serial.println(charUUID_RX.toString().c_str());
pClient->disconnect();
return false;
}
Serial.println(" - Found charUUID_RX characteristic");
//String helloValue = "Hello Remote Server";
//pRXCharacteristic->writeValue(helloValue.c_str(), helloValue.length());
// ======================================================
connected = true;
return 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 (strcmp(advertisedDevice.getAddress().toString().c_str(), "e8:57:57:05:a5:bd")==0) // ConnectedDevices[CountDevice]
// if (strcmp(advertisedDevice.getAddress().toString().c_str(), ConnectedDevices[CountDevice].c_str())==0) // ConnectedDevices[CountDevice]
// {
if (advertisedDevice.haveServiceUUID() &&
advertisedDevice.isAdvertisingService(serviceUUID)) {
//BLEDevice::getScan()->stop();
myDevice = new BLEAdvertisedDevice(advertisedDevice);
ServerBLE[CountDevice] = myDevice;
/*
ServerBLEAddress[CountDevice] = new BLEAddress(advertisedDevice.getAddress());
ScannedBLEAddress[CountDevice] = ServerBLEAddress->toString().c_str();
*/
doConnect = true;
doScan = true;
CountDevice +=1;
} // Found our server
// } // Found our address
} // 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(100); //1349
pBLEScan->setWindow(99); //499
pBLEScan->setActiveScan(true);
BLEScanResults foundDevices =pBLEScan->start(3, false); // 5,false
Serial.printf("foundDevices.getCount()= %i \n",foundDevices.getCount());
for(int i=0;i<foundDevices.getCount();i++) {
BLEAdvertisedDevice res = foundDevices.getDevice(i);
Serial.print(res.getName().c_str());
Serial.print(" @ ");
Serial.println(res.getAddress().toString().c_str());
ConnectedDevices[i]= res.getAddress().toString().c_str();
/*
MyClient* pMyClient = new MyClient();
pMyClient->setStackSize(5000);
pMyClient->start(new BLEAddress(*res.getAddress().getNative()));
vTaskDelay(100);
*/
}
CountDevice=0;
} // 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) {
//Serial.println(ConnectedDevices[CountDevice].getAddress().toString().c_str());
Serial.printf("CountDevice = %i\n", CountDevice);
Serial.println(ServerBLE[CountDevice]->getAddress().toString().c_str());
Serial.println(ServerBLE[CountDevice]->getName().c_str());
// if (connectToServerByMAC(*ServerBLE)) { //ConnectedDevices[CountDevice].getAddress()
if (connectToServer()) {
Serial.println("We are now connected to the BLE Server.");
CountDevice +=1;
} else {
Serial.println("We have failed to connect to the server; there is nothin more we will do.");
CountDevice +=1;
}
if (CountDevice >= MaxDevice)
{
doConnect = false;
}
// } //ENDIF ScannedBLEAddress
} //ENDif doConnect
// 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) {
//String newValue = "Time since boot: " + String(millis()/1000 + "\n");
//Serial.println("Setting new characteristic value to \"" + newValue + "\"");
/*
String newValue = " " + String(millis()/1000);
// Set the characteristic's value to be the array of bytes that is actually a string.
pRXCharacteristic->writeValue(newValue.c_str(), newValue.length());
*/
/*
//uint8_t SET_LED_i[] = {8,255,255,255};
uint8_t SET_LED_i[] = {8,0,0,0};
pRXCharacteristic->writeValue(SET_LED_i, sizeof(SET_LED_i)); //SET_LED 8
uint8_t BATTERY_LEVEL_i = 3;
pRXCharacteristic->writeValue(BATTERY_LEVEL_i, sizeof(BATTERY_LEVEL_i)); //BATTERY_LEVEL 3
*/
}else if(doScan){
BLEDevice::getScan()->start(0); //start(0)
// this is just example to start scan after disconnect,
// most likely there is better way to do it in arduino
}
delay(1000); // Delay a second between loops.
} // End of loop
void printHex(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}
/**
Helper routine to dump a byte array as dec values to Serial.
*/
void printDec(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], DEC);
}
}
void ParseMessage(byte *data)
{
// Get a message and fire the matchig event
}