ESP32 - One Client - 30 Servers - Data transfer send and receive - Need Advice
VigneshVicky97 opened this issue · comments
Vignesh S commented
I am able to make a Basic Master Slave structure to send message from Master and receive ACK message from Slave when the push button is pressed. But, my approach is very much limited to only one Slave device. How can I extend this approach for a maximum of 30 devices. I will attach my Master and Slave codes for you reference.
Master.cpp
void loop()
{
mb.task();
delay(10);
if (Serial.available())
{
String str = Serial.readStringUntil ('\n');
char msg[10];
char buf[30];
str.toCharArray(buf, 20);
int i = 0;
char* token = strtok(buf, " , ");
// Keep printing tokens while one of the
// delimiters present in str[].
while (token != NULL)
{
if (i == 0)
{
// to_ID = (uint8_t)atoi(token);
to_ID = (int)atoi(token);
}
else if (i == 1)
{
data = (uint16_t)atoi(token);
// String to_msg_str = String(token);
// to_msg_str.toCharArray(msg, 10);
}
token = strtok(NULL, " , ");
i++;
}
// rs485.send(to_ID, (uint8_t *)msg, strlen(msg));
if (!mb.slave())
{
// mb.writeHreg(1, REG1, 99, cbWrite);
// uint16_t data = 3;
Serial.print("Data: ");
Serial.println(data);
// mb.writeHreg(1, REG1, data, cbWrite);
mb.writeHreg(to_ID, REG1, data, cbWrite);
data_sent = true;
delay(50);
}
}
if (!mb.slave() && (data_sent == true))
{
mb.readHreg(1, REG2, &storing_data,1, cbWrite);
if(storing_data==1)//THIS NEVER PRINTS 55 ?
{
Serial.println("ACK from 1");
data_sent = false;
}
}
}
Slave.cpp
void loop()
{
button.loop(); // MUST call the loop() function first
mb.task();
delay(200);
if(mb.Hreg(REG1) != 1000)
{
// IF RECEIVED MESSAGE IS 99, DO SOME TASKS HERE AND THEN REPLY BACK TO MASTER BY PUTTING ANOTHER VALUE TO REG1
Serial.println(mb.Hreg(REG1));
uint16_t m = mb.Hreg(REG1);
if (m != 0)
{
tm1637.displayNumber(m);
digitalWrite(ACK_LED, HIGH);
}
mb.Hreg(REG1,1000); //PUT 55 IN REG1
mb.Hreg(REG2, 0);
}
if (button.isPressed())
{
Serial.println("Button Pressed!");
tm1637.clearTMDisplay();
digitalWrite(ACK_LED, LOW);
mb.Hreg(REG2, 1);
}
}
My Master and Slave devices Serial Monitor Output Screen:
Can you @emelianov help me with this?