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

User avatar
By Tefi
#85085 Hi all
Maybe someone have a similary Problem and can help me.
I listen on a UPD port and periodically send a broadcast. I tested it with PacketSender.
- In the PacketSender I received all Broadcast Messages from all esp8266s's
- if I sende a braodcast message to the ESPs all received the Messages.
But the not the ESPs. What is wrong? the same with on or two UDPs
thanks for any tip.

Code: Select allWiFiUDP Udp;
WiFiUDP Udp1;
unsigned int localUdpPort = 1234;                           // 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
int loop_counter = 0;
char _buffer[255] = "";
unsigned long mc_millis = 0;
int len;
IPAddress mc_ip;


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");
   
  mc_ip = WiFi.localIP();
  mc_ip[3] = 255;

  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();
  }

  yield();
  if (millis()>mc_millis) {
    mc_millis = millis() + 2000;
    Serial.print(".");
    //len = sprintf(_buffer,"Node:%08X/%d", ESP.getChipId(),loop_counter);
    len = sprintf(_buffer,"Hallo");
    Udp1.beginPacket(mc_ip,localUdpPort); // mc_ip, localUdpPort);
    Udp1.write((uint8_t *)_buffer, len);
    Udp1.endPacket();
    ++loop_counter;
  }
}