So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By mrlightsman
#74863 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.
User avatar
By mrlightsman
#74878 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.
User avatar
By rudy
#74879 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();
  }
}