-->
Page 1 of 4

Need help with making multiple esp talk to each other

PostPosted: Fri Mar 23, 2018 7:04 pm
by mrlightsman
I am trying to set up a protocol which will allow one master esp to send commands to multiple esp receivers. I would like them all to receive the command, then process the command using an if statement.

I am successfully using unicast udp to speak to a single device, but since this requires a “target” ip this is less than ideal. The receiving devices will use dhcp and I do not want to have to do address reservations in my router or do static ip in the receivers.

I’ve tried multicast udp, but cannot gets receivers to hear commands on the multicast IP address. They still only receive to the esp actual up address (Wi-Fi.localIP()).

If I understand correctly two esp can not communicate with each other using mdns, although that would work nicely if I were sending from a computer.

Are there other protocol options available that I have not thought of? I do not want to have to reprogram the remote with a new udp target up every time the receivers get a new ip assignment from my router.

Ideally, multicast udp would have been perfect. I could point a single remote to multiple receivers using the multicast IP address and let them process only the commands pertinent to them, using if statements.

Thanks for any thoughts and help.

Re: Need help with making multiple esp talk to each other

PostPosted: Fri Mar 23, 2018 7:34 pm
by rudy
UDP broadcast packets

https://forum.arduino.cc/index.php?topic=462605.0

Do a search on this. Also send multiple times, like three. Multicast and broadcast transmissions are less reliable than unicast.

Re: Need help with making multiple esp talk to each other

PostPosted: Sat Mar 24, 2018 9:16 am
by mrlightsman
thank you for the reply. I did try this and had no success. My network is based on 192.168.0.xx so I tried 192.168.0.0 and 192.168.0.255. I’ve also tried 255.255.255.0 and 224.0.0.255. All with no avail.

Ive been using Udp.beginPacket(ip address, port). Is that still the right command for broadcast? Should I be looking at a different broadcast IP address?

Is it possible my router is blocking multi and broadcast packets? It’s a to-link archer c7 v2 with latest updates.

Thanks for helping.

Re: Need help with making multiple esp talk to each other

PostPosted: Sat Mar 24, 2018 10:00 am
by rudy
Yes it could be a router issue.

Here is some simple example code. First is the sender.

Code: Select all#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

WiFiUDP udp;

const int port = 8888;
int  count = 0;
 
void setup()
{
  WiFi.begin("ssid", "pass");
  udp.begin(port);
}

void loop()
{
  IPAddress ip = WiFi.localIP();
  ip[3] = 255;

  // transmit broadcast package
  udp.beginPacket(ip, port);
  udp.write("Hello from 27. ");
  udp.print(count);
  udp.write("\n");
  udp.endPacket();

  delay(2000);
  count++;
}




Code: Select all#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

const char* ssid = "ssid";
const char* password = "pass";

WiFiUDP Udp;
unsigned int localUdpPort = 8888;  // local port to listen on
char incomingPacket[255];  // buffer for incoming packets
char  replyPacket[] = "Hi there! Got the message :-)";  // a reply string to send back


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

  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println(" connected");

  Udp.begin(localUdpPort);
  Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_str(), localUdpPort);
}


void loop()
{
  int packetSize = Udp.parsePacket();
  if (packetSize)
  {
    // receive incoming UDP packets
    Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
    int len = Udp.read(incomingPacket, 255);
    if (len > 0)
    {
      incomingPacket[len] = 0;
    }
    Serial.printf("UDP packet contents: %s\n", incomingPacket);

    // send back a reply, to the IP address and port we got the packet from
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(replyPacket);
    Udp.endPacket();
  }
}