• 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.com
     1v96 (c) 2017 G.Williams
    >
    =undefined
    >save();
    =undefined
    Compacting Flash...
    Calculating Size...
    Writing..
    Compressed 114368 bytes to 27188
    Connected: 192.168.68.158
    UDP Msg: 192.168.68.13:39242 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    UDP Msg: 77.34.44.34:26211 - :187}
    ERR: UPD packet not JSON, ignore
    UDP Msg: 192.168.68.13:39242 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    UDP Msg: 192.168.68.13:39242 - {"type":"SYS","name":"_M","cfg":18
    ERR: UPD packet not JSON, ignore
    >
    

    Thanks for your help.

    Sacha

About

Avatar for Sacha @Sacha started