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