Hi Gordon
I try to use the new UDP/Broadcast feature. First to receive UDP packages/messages.
Here is the code on the serverside, nodejs on linux. It sends a small JSON string every 10 seconds:
Please replace "BROADCAST_ADDR" with yours.
var PORT = 7077;
var BROADCAST_ADDR = "192.168.68.255";
var dgram = require('dgram');
var server = dgram.createSocket("udp4");
server.bind(function() {
server.setBroadcast(true);
setInterval(broadcastNew, 10000);
});
function broadcastNew() {
var msg=JSON.stringify({ type: "SYS", name : "_M", cfg: 187 });
var message = new Buffer(msg);
server.send(message, 0, message.length, PORT, BROADCAST_ADDR, function() {
console.log("Sent '" + message + "'");
});
}
Please start it with node:
node test.js
And now, this is the code running on EspruinoWiFi. Please replace your WLAN SSID and PW:
/*
ESPRUINO WIFI
*/
var WIFI_NAME = "Paradise";
var WIFI_OPTIONS = { password : "XYZ" };
var PORT = 7077;
var wifi = require("EspruinoWiFi");
var dgram = require('dgram');
var client = dgram.createSocket("udp4");
wifi.connect(WIFI_NAME, WIFI_OPTIONS, function (err) {
if (err) {
console.log("Connection error: " + err);
}
wifi.getIP(function (f, ip) {
console.log("Connected: " + ip.ip);
// UDP Broadcast
client.on('listening', function () {
var address = client.address();
console.log('UDP Client listening on ' + address.address + ":" + address.port);
client.setBroadcast(true);
});
client.on('message', function (message, rinfo) {
console.log('UDP Msg: ' + rinfo.address + ':' + rinfo.port +' - ' + message);
try {
var u = JSON.parse(message);
console.log('OK:'+u.cfg);
}
catch(err) {
console.log('ERR: UPD packet not JSON, ignore');
}
});
client.bind(PORT);
});
});
Please type "save" on the EspruinoWiFi.
Espruino receives packets. They are often corrupt, not complete or even from a wrong IP Address ??
It's a question of time, then the espruino is crashing.
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Hi Gordon
I try to use the new UDP/Broadcast feature. First to receive UDP packages/messages.
Here is the code on the serverside, nodejs on linux. It sends a small JSON string every 10 seconds:
Please replace "BROADCAST_ADDR" with yours.
Please start it with node:
node test.js
And now, this is the code running on EspruinoWiFi. Please replace your WLAN SSID and PW:
Please type "save" on the EspruinoWiFi.
Espruino receives packets. They are often corrupt, not complete or even from a wrong IP Address ??
It's a question of time, then the espruino is crashing.
Thanks for your help.
Sacha