Que tal, estoy trabajando en un proyecto de domótica con módulos Esp8266 con comunicación UDP, tengo un maestro y 3 esclavos. Mi problema es que el maestro solo se comunica con un modulo y los otros no logran recibir la información, entiendo que es porque se establece la comunicación por un solo puerto y es por eso que los otros esclavos quedan sin recibir los datos, si alguien pudiera ayudarme a resolver el problema para que todos reciban la información. Dicha información que reciben los módulos solo son 1 y 0 para encender o apagar. Anexo los códigos de Maestro y esclavo
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
const char *ssid = "circuits4you";
const char *pass = "password";
unsigned int localPort = 2000; // local port to listen for UDP packets
IPAddress ServerIP(192,168,4,1);
IPAddress ClientIP(192,168,4,2);
IPAddress ClientIP2(192,168,4,3);
// A UDP instance to let us send and receive packets over UDP
WiFiUDP udp;
char packetBuffer[9]; //Where we get the UDP data
//=======================================================================
// Setup
//=======================================================================
void setup()
{
Serial.begin(9600);
Serial.println();
WiFi.softAP(ssid, pass); //Create Access point
//Start UDP
Serial.println("Starting UDP");
udp.begin(localPort);
Serial.print("Local port: ");
Serial.println(udp.localPort());
}
//======================================================================
// MAIN LOOP
//======================================================================
void loop()
{
int cb = udp.parsePacket();
if (!cb)
{
//If serial data is recived send it to UDP
if(Serial.available()>0)
{
udp.beginPacket(ClientIP, 2000);
//Send UDP requests are to port 2000
char a[1];
a[0]=char(Serial.read()); //Serial Byte Read
udp.write(a,1); //Send one byte to ESP8266
udp.endPacket();
}
}
else {
// We've received a UDP packet, send it to serial
udp.read(packetBuffer, 1); // read the packet into the buffer, we are reading only one byte
Serial.print(packetBuffer);
delay(20);
}
}
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
const char *ssid = "circuits4you";
const char *pass = "password";
unsigned int localPort = 2000; // local port to listen for UDP packets
IPAddress ServerIP(192, 168, 4, 1);
IPAddress ClientIP2(192, 168, 4, 3);
// A UDP instance to let us send and receive packets over UDP
WiFiUDP udp;
char packetBuffer[9]; //Where we get the UDP data
char replyPacket[] = "";
//======================================================================
// Setup
//======================================================================
void setup()
{
pinMode(2, OUTPUT);
pinMode(0, OUTPUT);
Serial.begin(9600);
Serial.println();
WiFi.begin(ssid, pass); //Connect to access point
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
digitalWrite(0,LOW);
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
//Start UDP
Serial.println("Starting UDP");
udp.begin(localPort);
Serial.print("Local port: ");
Serial.println(udp.localPort());
digitalWrite(0,HIGH);
}
//======================================================================
// MAIN LOOP
//======================================================================
void loop()
{
int cb = udp.parsePacket();
if (!cb)
{
//If serial data is recived send it to UDP
if (Serial.available() > 0)
{
udp.beginPacket(ServerIP, 2000); //Send Data to Master unit
//Send UDP requests are to port 2000
char a[1];
a[0] = char(Serial.read()); //Serial Byte Read
udp.write(a, 1); //Send one byte to ESP8266
udp.endPacket();
}
}
else {
// We've received a UDP packet, send it to serial
udp.read(packetBuffer, 1); // read the packet into the buffer, we are reading only one byte
char valor = packetBuffer[0];
if (valor == '2')
{
digitalWrite(2,HIGH);
}
else if (valor == '3')
{
digitalWrite(2,LOW);
}
else
{
Serial.print(valor);
udp.endPacket();
}
}
delay(20);
}
//=======================================================================