Send and receive UDP packet

Hello,

I am trying to send a character from a photon to another photon using UPD. The sending photon uses this code:

UDP Udp;
char buffer1[] = "a";
IPAddress remoteIP(192, 168, 1, 104);
int port = 8888;
void setup() {
   Udp.begin(8888);
   Serial.begin(9600);
}
void loop(){ 
 if (Udp.sendPacket(buffer1, sizeof(buffer1), remoteIP, port) < 0) {
    Serial.println("Error");
}

The receiving photon runs this code:

unsigned int port = 8888;
UDP Udp;
void setup() {
    Serial.begin(9600);
    Udp.begin(port);
}
void loop(){
    if (Udp.parsePacket() > 0)    {
        Serial.println("character received");
        int i = Udp.read();
        Serial.println(i);
        Udp.flush();
    }
}

I don’t get an error but the receiving photon doens’t seem to receive anything. I am sure that I using the correct IP-address. What could be wrong? I assume UPP is supported with the particle photon (photons were bought one month ago).

Thanks in advance,
Bart

Have you double checked your receivers IP? e.g. via WiFi.localIP()

Yep, even triple checked. I assume the photon supports UDP

It does - if it wasn’t it would not be in the docs :wink:
Does your router allow UDP?
Can you use UDP with your computer on that network?

That was the problem. It works now!

Thanks for your advice!

I got UDP working from Photon to Photon(s) (to a broadcast IP) and can offer my hard-won lessons if you are still struggling with this - where I ended up looks very similar to your code, though, so you’re definitely on the right track.

However, I have my own problem where it works great for a while, and then devices just stop picking up the broadcast packets. I am going to start my own thread on that.

Anyway, post again to let us know how it turned out!

Hello Patrick,

The problem was that my access point wasn’t enabled for UDP-packets. Now it is and it seems to work.

Thanks for the reply,
Bart

1 Like