When the ESP8266 is just started it receives the multicast traffic ok, but after a few seconds nothing is received anymore. But when sending a packet with unicast to the ESP8266 then it is received and displayed on the serial console.
Here is the default example code I found and use to test multicast (btw the ssid and key are stored in eeprom so not defined in the example below):
/*
* 31 mar 2015
* This sketch display UDP packets coming from an UDP client.
* On a Mac the NC command can be used to send UDP. (nc -u 192.168.1.101 2390).
*
* Configuration : Enter the ssid and password of your Wifi AP. Enter the port number your server is listening on.
*
13 apr 2015. Partly working. Packets are received and displayed, but the confirmation packet is not received by the client.
Tested with Mac as cleint using netcat (nc -u 192.168.1.101 2390)
*/
#include <ESP8266WiFi.h>
#include <WiFiUDP.h>
int status = WL_IDLE_STATUS;
unsigned int localPort = 2390; // 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(239, 255, 255, 250);
unsigned int portMulti = 1900; // local port to listen on
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(115200);
// setting up Station AP
WiFi.begin();
// 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();
Serial.println("Connected to wifi");
Serial.print("Udp Multicast server started at : ");
Serial.print(ipMulti);
Serial.print(":");
Serial.println(portMulti);
Udp.beginMulticast(WiFi.localIP(), ipMulti, portMulti);
}
void loop()
{
int noBytes = Udp.parsePacket();
if ( noBytes ) {
Serial.print(millis() / 1000);
Serial.print(":Packet of ");
Serial.print(noBytes);
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,noBytes); // read the packet into the buffer
// display the packet contents in HEX
for (int i=1;i<=noBytes;i++){
Serial.print(packetBuffer[i-1],HEX);
if (i % 32 == 0){
Serial.println();
}
else Serial.print(' ');
} // end for
Serial.println();
// send a reply, to the IP address and port that sent us the packet we received
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write("UDP packet received");
Udp.endPacket();
} // end if
}
Here are the linux commands for sending the multicast traffic (although there is already SSDP traffic in this setup to receive):
echo "MULTICASTTEST" | netcat -4u -w1 239.255.255.250 1900
And I use this linux command for sending unicast traffic to the esp8266:
echo 'UNICASTTEST' | netcat -4u -w1 172.20.0.155 1900