Photon can send but not recieve udp packets

I’m trying to use a Node.js server with a photon running a UDP client to send messages back and forth. It doesn’t look like the photon is receiving the messages because it doesn’t respond and it’s supposed to publish something to the particle device console which it doesn’t do either.

Photon code:

UDP Udp;

IPAddress nodeServer(172,20,10,2);
int port = 8888;

void setup() {
    Udp.begin(port);
    
    while (!WiFi.ready()) {
	    delay(10);
	    Particle.process();
	    if (WiFi.ready()) {
	        break;
	    }
	}
	
	Particle.publish("wifi ready");
}

void loop() {
    if (Udp.parsePacket() > 0) {
        
        Particle.publish("we got a udp");
        
        char data[50];
        int c = 0;
        
        while (Udp.available()) { // read data sent into data array
            data[c] = Udp.read();
            c++;
        }
        
        char cmd[5];

        for (int i = 0; i < strlen(data); i++) {
            cmd[i] = data[i];
        }
        
        if (strcmp(cmd, "ping") == 0) {
            Udp.sendPacket("got ping", 8, nodeServer, port);
        }
    }
}

nodejs server to receive data:

var PORT = 8888;
var HOST = '172.20.10.2'; 

const dgram = require('dgram');
const server = dgram.createSocket('udp4');

server.on('listening', function() {
    var address = server.address();
    console.log('UDP server listening on ' + address.address + ':' + address.port);
});

server.on('message', function(message, remote) {
    console.log(remote.address + ':' + remote.port + ' - ' + message);
})

server.bind(PORT, HOST);

nodejs to send the initial ping

var PORT = 8888;
var HOST = '172.20.10.3';

var dgram = require('dgram');
var message = 'ping';

var client = dgram.createSocket('udp4');

client.send(message, 0, message.length, PORT, HOST, function(err, bytes) {
    if (err) throw err;
    console.log('UDP message sent to ' + HOST +':'+ PORT);
    client.close();
});

I’m not sure if the particle.publish statements aren’t working or the code isn’t running or the particle device console is just broken because I don’t get any output, even the messages that come after a successful flash. I can still ping the photon from the cloud and it says it’s online, and the LED is breathing cyan, but no events.

I can ping the photon by running ping 172.20.10.3 in my terminal and that works, and I know I can send data off of the photon using UDP because I have a program that does that. I tested it after I started getting this issue and it still works, so I figure it must be sending data to the photon that’s the issue.

I tried changing the port from 33333 to 8888, but neither worked. Any ideas?

Thanks in advance for any help.

update: I installed wireshark and looked at the UDP packets being sent. I can see the packets going from my computer to the photon, and when I run the working code that sends packets from the photon I can see those as well, but the photon never sends anything back.

Although I don't quite see why this question could not have been added to your other thread, one thing I noticed immediately

You are sending via

client.send(message, 0, message.length, PORT, HOST, function(err, bytes)

(message.length of var message = 'ping' will be 4)

and then copy the incoming packet (which is only four bytes long as the zero-terminator is not sent) like that

        for (int i = 0; i < strlen(data); i++) {
            cmd[i] = data[i];
        }

So even if data was containing the zero-terminator (which it most likely won't due to lacking initialisation) you'd only copy four characters (again without the zero-terminator) and hence

        if (strcmp(cmd, "ping") == 0) {

will most likely not signal equality.

Some basic debugging steps (like adding Serial.println(data) and Serial.println(cmd)) should help testing this hypothesis in no time (and wouldn't be too much to ask for before consulting others).

BTW, failing to initialise local variables was also a point in this thread of yours

1 Like