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.