UDP join multicast not working

I am unable to get the UDP multicast to receive data. The below script is roughly from the Udp library documentation.

// UDP Port used for two way communication
unsigned int localPort = 1900;

// An UDP instance to let us send and receive packets over UDP
UDP Udp;

void setup() {
  // start the UDP
  Udp.begin(localPort);
  IPAddress multicastAddress(239, 255, 255, 250);
  waitUntil(WiFi.ready);
  Udp.joinMulticast(multicastAddress);

  // Print your device IP Address via serial
  Serial.begin(9600);
  Serial.println(WiFi.localIP());
}

void loop() {
  // Check if data has been received
  if (Udp.parsePacket() > 0) {
    Serial.println("Parsing UDP packet.");
    Particle.publish("Parsing UDP packet");

    // Read first char of data received
    char c = Udp.read();

    // Ignore other chars
    while(Udp.available())
      Udp.read();

    // Store sender ip and port
    IPAddress ipAddress = Udp.remoteIP();
    int port = Udp.remotePort();

    // Echo back data to sender
    Udp.beginPacket(ipAddress, port);
    Udp.write(c);
    Udp.endPacket();
  }
}

I’ve tried various flavors of netcat to send data to the IP used when joining the multicast…

nc  -vu -w 5 -s 192.168.0.20 239.255.255.250 1900 < test.txt

I have a Photon that I received in January of 2015 and have tried several versions of firmware from 0.4.5 and up. Any help on this would be greatly appreciated. Thank you.

I’m not sure why it isn’t working for you. This is what I tested and this worked for me:

Photon program, tested on 0.6.0:

#include "Particle.h"

const size_t UDP_BUFFER_SIZE = 513;
const int UDP_PORT = 7123;
IPAddress multicastAddress(239, 1, 1, 1);

UDP udp;
uint8_t udpBuffer[UDP_BUFFER_SIZE];

void setup() {
	Serial.begin(9600);

	udp.begin(UDP_PORT);
	udp.joinMulticast(multicastAddress);

	Serial.printlnf("server=%s:%d", WiFi.localIP().toString().c_str(), UDP_PORT);
}

void loop() {
	// receivePacket() is preferable to parsePacket()/read() because it saves a buffering step but more importantly
	// it return an error code if an error occurs! This is important to reinitialize the listener on error.
	int count = udp.receivePacket(udpBuffer, UDP_BUFFER_SIZE - 1);
	if (count > 0) {
		udpBuffer[count] = 0;
		IPAddress remoteAddr = udp.remoteIP();

		Serial.printlnf("## packet size=%d addr=%s data=%s", count, remoteAddr.toString().c_str(), udpBuffer);
	}
	else
	if (count == 0) {
		// No packet
	}
	else {
		Serial.printlnf("** receivePacket error %d", count);
		udp.begin(UDP_PORT);
		udp.joinMulticast(multicastAddress);
	}
}

Test command:

echo "hello" | nc -vu 239.1.1.1 7123

Serial output:

$ particle serial monitor
Opening serial monitor for com port: "/dev/cu.usbmodemFD1161"
## packet size=6 addr=192.168.2.4 data=hello
1 Like

Thanks. Your example is different than what I was trying, I’ll give that a go this evening after work and see if I can’t get it working.

Don’t forget that your router can also be a problem. You might want to check multicast from two other hosts to make sure your packets get routed.

Will do. Could my laptop also be an issue? I have a macbook pro that I am testing with. When testing with a python script (running on said macbook pro) that does something similar, my Amazon Echo can discover that via UDP so I am thinking that it is not my router at the moment.

No dice. I have tried 2 different photons with your example code and they do not receive UDP over multicast. I can send directly to the photons IP via UDP no problem but when using the multicast 239.1.1.1 I get no serial output.

I have UPnP enabled on my router.

I’d guess a router issue, but I don’t know what it would be. Originally I tested with a MacPro over Ethernet to my Photon test access point (TP-LINK WR702N). I also just repeated the test using nc on a MacBookAir and Windows 10, both on a different Wi-Fi network, to the same Photon and test access point, and that also worked.

You may need to allow for more router hops for the multicast packets to go through all your routers.

I have a single router (which is my modem), arris brand. I went through the configuration and the only thing regarding UDP was allowing UPnP which was enabled. Beyond that it is unclear how to “allow for more router hops for the multicast packets to go through all your routers”.
Running a similar python implementation on my macbook pro I was able to receive multicast over UDP from another host on the same WiFi network. I attempted to send UDP multicast last night to two different photons (0.6.0) from two different hosts on the same network with no luck.
Would the version of the hardware make a difference (as far as I can tell I have a pretty recent photon, photonh)?

Usually TTL (or max hop count) is set by the originator of the packet, but I can’t find the place to set the TTL in the UDP object (I could have sworn I had used it before :confused:)

Another issue might be the multicast group you’re using. 239.x.x.x is reserved for scoping in Administrative Scoped IP Multicast. Try another multicast group IP, if this is tripping your router.

I think this will enable your router to be configured over UPnP which can be a bit of a security hole. I don’t think that is related to routing.

If @rickkas7 's code works for others but not for you and your setup, then I think you have to look at your setup more closely. If you are not already, I would start running Wire Shark in promiscuous mode to try to see more of the network traffic. If your router is blocking everything, then you still won’t see packets from the Photon, but given that you report that your other hosts are working, you might be able to see the difference between working and non-working.

1 Like

Alright so it was a router issue. I finally had some time to play with this again and I switched over from using the WiFi on my cable modem to my Timecapsule and sure enough I started receiving UDP multicast packets on the Photon. After digging through my modem settings I cannot find anything that seems to allow UDP to flow properly to the Photon, oh well, at least I know it works. Thank again to everyone who pushed me to take a deeper look at my setup!

4 Likes