Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By gerardwr
#45184 [quote="picstart"]Multicast Arduino 1.6.5 will crash after few seconds in the loop using the sample code in this thread.
It does accept a UDP multicast and does reply within the time before it crashes. It works until it crashes.
[quote]

I just compiled the sketch in your post in Arduino 1.6.8 + Board Manager V2.1.0 for a Wemos-D1-Mini. The sketch is now running for 5 minutes and it still responds to UDP packets.

The sketch in my original post from 1 year ago also still runs OK after 5 minutes in Arduino 1.6.8 + Board Manager V2.1.0 for a Wemos-D1-Mini.
User avatar
By dannybackx
#72674 Can anyone confirm that this works ? I am using the 2.3.0 version of esp-arduino, I don't see packets either direction.
I've tested with iperf as follows :
iperf -s -u -B 239.0.0.57 -p 12345
iperf -c 239.0.0.57 -u -p 12345

The two iperf instances talk to each other, not to the ESP tgough.
User avatar
By rudy
#72680
dannybackx wrote:Can anyone confirm that this works ? I am using the 2.3.0 version of esp-arduino, I don't see packets either direction.
I've tested with iperf as follows :
iperf -s -u -B 239.0.0.57 -p 12345
iperf -c 239.0.0.57 -u -p 12345

The two iperf instances talk to each other, not to the ESP though.


I'm using the code from page 3. I'm not sure if it is any different from the original message. I changed the address and port to 224.0.0.251:5353 and commented out the reply packet send.

I use this to monitor mDNS traffic. I use Windows 10, and when I open Bonjour Browser I get the traffic between my computer and the mDNS devices I have powered up. I have also used this with other addresses and ports. I am not currently using 2.3.0 and I forget if there were any problems with it. (I don't think so)

If I had iperf I would have tried it with that, but I don't want to load any new software. I did try this before I posted the message.

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

const char* ssid = "ssid";  //  your network SSID (name)
const char* pass = "12345678";   // your network password

const char* WiFi_hostname = "com9.112";

unsigned int localPort = 12345; // local port to listen for UDP packets
byte packetBuffer[512]; //buffer to hold incoming and outgoing packets
//////////////////////////////////////////////////////////////////////
// A UDP instance to let us send and receive packets over UDP
//////////////////////////////////////////////////////////////////////
WiFiUDP Udp;

// Multicast declarations
IPAddress ipMulti(224, 0, 0, 251);

unsigned int portMulti = 5353; // local port to listen on

//=====================================================
void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  Serial.println("");
  Serial.println("mDNS-display.ino");

  WiFi.hostname(WiFi_hostname);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, pass);

  // Wait for connect to AP
  Serial.print("[Connecting]");
  Serial.print(ssid);
  int tries = 0;
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    tries++;
    if (tries > 30) {
      break;
    }
  }
  Serial.println();
  printWifiStatus();


  Serial.println("Connected to wifi");
  Serial.print("Udp Multicast mDNS listener started at : ");
  Serial.print(ipMulti);
  Serial.print(":");
  Serial.println(portMulti);
  Serial.println("=========================================================");
  Udp.beginMulticast(WiFi.localIP(), ipMulti, portMulti);
}

//=====================================================
void loop()
{
  int numBytes = Udp.parsePacket();    // number of bytes
  if ( numBytes )                      // if > 0
  {
    /////// UDP packet can be a multicast packet or specific to this device's own IP
    Serial.print(millis() / 1000);
    Serial.print("seconds :Packet of ");
    Serial.print(numBytes);
    Serial.print(" received from ");
    Serial.print(Udp.remoteIP());
    Serial.print(":");
    Serial.println(Udp.remotePort());
    //////////////////////////////////////////////////////////////////////
    // We've received a packet, read the data from it
    //////////////////////////////////////////////////////////////////////
    Udp.read(packetBuffer, numBytes); // read the packet into the buffer

    // display the packet contents in HEX
    for (int i = 1; i <= numBytes; i++) {

      if (packetBuffer[i - 1] < 16) {
        Serial.print("0"); // leading 0 if < 10h
      }
      Serial.print(packetBuffer[i - 1], HEX);
      if (i % 32 == 0) {
        Serial.println();
      }
      else Serial.print(' ');
    } // end for
    Serial.println();
    Serial.println("--------------------------");
    //////////////////////////////////////////////////////////////////////
    // send a reply, to the IP address and port that sent us the packet we received
    // the receipient will get a packet with this device's specific IP and port
    //////////////////////////////////////////////////////////////////////
    //Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    //Udp.write("My ChipId:");
    //Udp.print(ESP.getChipId());
    //Udp.endPacket();

  } // end if

  delay(20);

}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}