ESP32 Bluetooth communcation with 2 Slaves

Hello everyone! I have a programming question. I am currently working on this project where I have several ESP32 talking to each other over Bleutooth.

My problem:

When I connect one Master and one Slave the code works, but when I try to connect more then 1 Slave it doesn´t work anymore.

The way I tried to connect the second Slave was by adding another BluetoothSerial stream for Slave 2. Like this:

BluetoothSerial Slave1;
BluetoothSerial Slave2;

//Setup bluetooth
String name1 = "ESP32_Pod_2"; //                  <------- set this to be the name of the other ESP32!!!!!!!!!
bool connect_1;
String name2 = "ESP32_Pod_3"; //                  <------- set this to be the name of the other ESP32!!!!!!!!!
bool connect_2;

I am not sure if this is the correct way, beacuse I can´t find any documentation on how to connect more than 1 Slave to a master over Bluetooth.

My code:

In the following code you will see how I tried to connect the Master with 2 Slaves.

So basically what the code does is that the Master sends a number to each Slave, the Slaves check, if the number is their ID-number and act based on that. The master waits until one of the Slaves returns a value to him.

Master code:

#include <Arduino.h>
#include "BluetoothSerial.h"

#define PIN 26

BluetoothSerial Slave1;
BluetoothSerial Slave2;

//Setup bluetooth
String name1 = "ESP32_Pod_2"; //                  <------- set this to be the name of the other ESP32!!!!!!!!!
bool connect_1;
String name2 = "ESP32_Pod_3"; //                  <------- set this to be the name of the other ESP32!!!!!!!!!
bool connect_2;

//Identification for POD Number
const int pod_ID = 1;

//data to send
int pod;

// data to recieve
int reactiontime = 0;

void setup() 
{
  Serial.begin(115200);

  //Setup BLuetooth
  Slave1.begin("ESP32_Pod_1", true);
  Slave2.begin("ESP32_Pod_1", true);  
  connect_1 = Slave1.connect("ESP32_Pod_2");
  connect_2 = Slave2.connect("ESP32_Pod_3");

  if(connect_1 && connect_2)
  {
    Serial.println("Connected Succesfully!");
  }
  else
  {
    while(!Slave1.connected(10000)) 
    {
      Serial.println("Failed to connect to POD 1");
    }
    while(!Slave2.connected(10000)) 
    {
      Serial.println("Failed to connect to POD 2");
    }
  }
}

void loop() 
{
  pod = random(1,4);
  
  //Send POD to all slaves
  Slave1.write(pod);
  Slave2.write(pod);

  if(pod == pod_ID)
  {
    Serial.println("");
    Serial.println("-----------------------------------");
    Serial.println("Master");
    Serial.println("-----------------------------------");
    Serial.println("");
  }
  else 
  {
    //Wait for Slave1 or Slave2
    while(!(Slave1.available()) || !(Slave2.available()))
    {
      //do nothing
    }

    //Read whatever Slave is there
    if(Slave1.available())
    {
      Serial.println("");
      Serial.println("-----------------------------------");
      reactiontime = Slave1.read();
      Serial.println(reactiontime);
      Serial.println("Slave 1");
      Serial.println("-----------------------------------");
      Serial.println("");      
    }
    else if(Slave2.available())
    {
      Serial.println("");
      Serial.println("-----------------------------------");
      reactiontime = Slave2.read();
      Serial.println(reactiontime);
      Serial.println("Slave 2");
      Serial.println("-----------------------------------");
      Serial.println("");
    }
  }

  delay(1000);
}

Slave 1 code:

#include <Arduino.h>
#include "BluetoothSerial.h"

#define PIN 26 

BluetoothSerial Slave1;

//Identification for POD Number
const int pod_ID = 2;

//data to send
int reactiontime = 0;

// data to recieve
int pod;

void setup() 
{
  Serial.begin(115200);
  Slave1.begin("ESP32_Pod_2"); 
}

void loop() 
{
  //wait for command
  while(!(Slave1.available()))
  {
    //do nothing
  }

  //read the command
  pod = Slave1.read();
  Serial.println(pod);
 
  if(pod == pod_ID) 
  { 
    delay(random(1,3001));  

    //send data
    reactiontime=random(4000,5001);
    Slave1.write(reactiontime);
  }
  else 
  {
    //do nothing
  }
}

Slave 2 code:

#include <Arduino.h>
#include "BluetoothSerial.h"

#define PIN 26 

BluetoothSerial Slave2;

//Identification for POD Number
const int pod_ID = 3;

//data to send
int reactiontime = 0;

// data to recieve
int pod;

void setup() 
{
  Serial.begin(115200);
  Slave2.begin("ESP32_Pod_3"); 
}

void loop() 
{
  //wait for command
  while(!(Slave2.available()))
  {
    //do nothing
  }

  //read the command
  pod = Slave2.read();
  Serial.println(pod);

  if(pod == pod_ID) 
  {  
    delay(random(1,3001));

    //send data
    reactiontime = random(9000,10001);
    Slave2.write(reactiontime);
  }
  else 
  {
    //do nothing
  }
}

I hope someone can help me fix my problem here. Any help would be much appreciated!

Best Regards, Markus

  Slave1.begin("ESP32_Pod_1", true);
  Slave2.begin("ESP32_Pod_1", true); 

2 connections with the same name ?
Is that right ?

I think it is correct this way. The master device in this case is called "ESP32_POD_1" and I connect it to each bluetooth serial stream.

I tried it with different names just to be sure, but unfortunately it didn´t work.

Thanks for the help though!

Classic Bluetooth is meant for 1:1 communication and you can not connect to multiple devices. You may be able to connect and disconnect from two different slaves.

Alternatively, I think you can switch to BLE and deal with multiple services and characteristics.

I second that...

Here are some links to BLE information regarding several clients with ESP32:

https://docs.espressif.com/projects/espressif-esp-faq/en/latest/software-framework/ble-bt.html

and

https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/bluetooth/esp-ble-mesh.html

However, I think switching to WiFi TCP or UDP connection would be much easier ...

Ok thank you guys very much!

I solved it now by connecting and disconnecting each slave every time. It might not be the best way to do it but it works for now. :grin:

Thanks for all the suggestions, I will definitly look into BLE to see how I could solve it better.

If anyone interested this is what the master code looks like now:

#include <Arduino.h>
#include "BluetoothSerial.h"

#define PIN 26

BluetoothSerial Slave;

//Setup bluetooth
String name1 = "ESP32_Pod_2"; //                  <------- set this to be the name of the other ESP32!!!!!!!!!
bool connect_1;
String name2 = "ESP32_Pod_3"; //                  <------- set this to be the name of the other ESP32!!!!!!!!!
bool connect_2;

//Identification for POD Number
const int pod_ID = 1;

//data to send
int pod;

// data to recieve
int reactiontime = 0;

void setup() 
{
  Serial.begin(115200);

  //Setup Bluetooth
  Slave.begin("ESP32_Pod_1", true);
}

void loop() 
{
  pod = random(1,4);
  
  if(pod==2)
  {
    connect_1 = Slave.connect("ESP32_Pod_2");

    while(!Slave.connected(5000)) 
    {
      Serial.println("Failed to connect to POD 2");
    }

    Slave.write(pod);

    //Wait for Slave1
    while(!(Slave.available()))
    {
      //do nothing
    }
    
    Serial.println("");
    Serial.println("-----------------------------------");
    reactiontime = Slave.read();
    Serial.println(reactiontime);
    Serial.println("Slave 1");
    Serial.println("-----------------------------------");
    Serial.println("");      

    connect_1 = Slave.disconnect();
  }

  else if(pod==3)
  {
    connect_2 = Slave.connect("ESP32_Pod_3");

    while(!Slave.connected(5000)) 
    {
      Serial.println("Failed to connect to POD 3");
    }

    Slave.write(pod);

    //Wait for Slave2
    while(!(Slave.available()))
    {
      //do nothing
    }
    
    Serial.println("");
    Serial.println("-----------------------------------");
    reactiontime = Slave.read();
    Serial.println(reactiontime);
    Serial.println("Slave 2");
    Serial.println("-----------------------------------");
    Serial.println("");      

    connect_2 = Slave.disconnect();
  }
  
  else if(pod == pod_ID)
  {
    Serial.println("");
    Serial.println("-----------------------------------");
    Serial.println("Master");
    Serial.println("-----------------------------------");
    Serial.println("");
  }

  delay(1000);
}
2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.