EspruinoWiFi - UDP / Broadcast not stable

Posted 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.

    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

  • "listening" is not implemented. Espruino code reduced to:

    /*
    ESPRUINO WIFI
    */
    var WIFI_NAME = "Paradise";
    var WIFI_OPTIONS = { password : "XYZ" };
    var PORT = 7077;
    var wifi = require("EspruinoWiFi");
    var dgram = require('dgram');
    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
            var client = dgram.createSocket("udp4");
    
            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);
        });
    });
    
  • Thanks for the steps to reproduce. Just tried it here and I got it to happen once, missing the last 2 characters. No corrupted IP though.

    Please can you try adding wifi.at.debug() to the end of your code, and post what happens when it crashes? I'm actually having trouble reproducing with the debug enabled!

  • Thanks Gordon

    Without the debug enabled, i get the error and crash within a few minutes.
    When debug enabled i was also not able to reproduce it with 20 minutes !?

    I enabled debug to my projectcode. It shows the wrong ip's, fragments of the udp messages and crashes very fast. I think it makes no sense to post the projectcode. It to big.

    Here is the output. I only add a console.log of the UDP ip, port and message received. Starting with "UPD Msg: "

    The correct source ip is 192.168.68.13

    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"ty­pe\":\"SYS\",\"name\":\"_M\",\"cfg\":187­}"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"ty­pe\":\"SYS\",\"name\":\"_M\",\"cfg\":187­}"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"ty­pe\":\"SYS\""
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS"
    UDP Msg: 44.34.110.97:25965 - "_M","cfg":187}
    ] "\r\n+IPD,0,36,192.16"
    ] "8.68.13,45962:{\"type\":\"SYS\",\"name\­":\"_M\",\"cfg\":187}"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"ty­pe\""
    UDP Msg: 192.168.68.13:45962 - {"type"
    UDP Msg: 58.34.83.89:8787 - name":"_M","cfg":187}
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"ty­pe\":\"SYS\",\"name\":\"_M\",\"cfg\":187­}"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"ty­pe\":\"SYS\",\"name\":\"_M\",\"cfg\":187­}"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"ty­pe\":\"SYS\",\"n"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","n
    UDP Msg: 97.109.101.34:8762 - ","cfg":187}
    ] "\r\n+IPD,0,36"
    ] ",192.168.68.13,45962:{\"type\":\"SYS\",­\"name\":\"_M\",\"cfg\":187}"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    ] "\r\n+IPD,0,36,192"
    ] ".168.68.13,45962:{\"type\":\"SYS\",\"na­me\":\"_M\",\"cfg\":187}"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    ] "\r\n+IPD,0,36,19"
    ] "2.168.68.13,45962:{\"type\":\"SYS\",\"n­ame\":\"_M\",\"cfg\":187}"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"ty­pe\":\"SYS\",\"name\":\""
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"
    UDP Msg: 95.77.34.44:25378 - ":187}
    ] "\r\n+IPD,0,36,1"
    ] "92.168.68.13,45962:{\"type\":\"SYS\",\"­name\":\"_M\",\"cfg\":187}"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"t"­
    UDP Msg: 192.168.68.13:45962 - {"t
    UDP Msg: 121.112.101.34:8762 - S","name":"_M","cfg":187}
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"ty­pe\":\"SYS\",\"name\":\"_M\",\"cfg\":187­}"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"ty­pe\":\"SYS\",\"name\":"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":
    UDP Msg: 34.95.77.34:8748 - g":187}
    ] "\r\n+IPD,0,36"
    ] ",192.168.68.13,45962:{\"type\":\"SYS\",­\"name\":\"_M\",\"cfg\":187}"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"ty­pe\":\"SYS\",\"nam"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","nam
    UDP Msg: 101.34.58.34:19807 - "cfg":187}
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"t"­
    UDP Msg: 192.168.68.13:45962 - {"t
    UDP Msg: 121.112.101.34:8762 - S","name":"_M","cfg":187}
    ] "\r\n+IPD"
    ] ",0,36,192.168.68.13,45962:{\"type\":\"S­YS\",\"name\":\"_M\",\"cfg\":187}"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"ty­pe\":\"SYS\",\"name\":\"_M\",\"cfg\":187­}"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"ty­pe\":\""
    UDP Msg: 192.168.68.13:45962 - {"type":"
    UDP Msg: 83.89.83.34:8748 - me":"_M","cfg":187}
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"ty­pe\":\"SYS\""
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS"
    UDP Msg: 44.34.110.97:25965 - "_M","cfg":187}
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"ty­pe\":\"SYS\",\"name\":\"_M\",\"cfg\":187­"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187
    > 
    // crashed
    
  • It's a bit frustrating. I waited so long for that feature ;-).

    My fealing is that there are two problems.

    First that something is mixed up resulting in wrong data. Wrong IP and trancated messages.
    I'm able to filter that out. I think this has not a crash in result.

    The second problem, truncating the last chars is resulting in a crash. Like on the last line above.
    It's the same that we get in the isolated code above without debug enabled.

    Sacha

  • I enabled turbomode in the isolated code and the mixed up data is shown up. I was able to catch debug code. Did not crash.

    ____                 _
    |  __|___ ___ ___ _ _|_|___ ___
    |  __|_ -| . |  _| | | |   | . |
    |____|___|  _|_| |___|_|_|_|___|
             |_| espruino.com
     1v96 (c) 2017 G.Williams
    >
    =undefined
    >save();
    =undefined
    Compacting Flash...
    Calculating Size...
    Writing..
    Compressed 114368 bytes to 26882
    pong
    Connected: 192.168.68.158
    ["AT+CIPSTART=0,\"UDP\",\"255.255.255.25­5\",7077,7077,2\r\n"
    ] "0,CONNECT\r\n\r\nOK\r\n"
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"ty­pe\":\"SYS\",\"name\":\"_M\",\"cfg\":187­}"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"ty­p"
    UDP Msg: 192.168.68.13:45962 - {"typ
    ERR: UPD packet not JSON, ignore
    UDP Msg: 101.34.58.34:22867 - ,"name":"_M","cfg":187}
    ERR: UPD packet not JSON, ignore
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"ty­pe\":\"SYS\",\"name\":\"_M\",\"cfg\":187­}"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"ty­pe\":\"SYS\",\"name\":\"_M\",\"cfg\":187­}"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    ] "\r\n+IP"
    ] "D,0,36,192.168.68.13,45962:{\"type\":\"­SYS\",\"name\":\"_M\",\"cfg\":187}"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"ty­pe\":\"SYS\",\"name\":\"_M\",\"cfg\":187­}"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"ty­pe\":\"SYS\",\"name\":\"_M\",\"cfg\":187­}"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"ty­pe\":\"SYS\",\"name\":\"_M\",\"cfg\":187­}"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    ] "\r\nIPD,0,36,192.168.68.13,45962:{\"typ­e\":\"SYS\",\"name\":\"_M\",\"cfg\":187}­"
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"ty­pe\":\"SYS\",\"name\":\"_M\",\"cfg\":187­}"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"ty­pe\":\"SYS\",\"name\":\"_M\",\"cfg\":187­}"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"ty­pe\":\"SYS\",\"name\":\"_M\",\"cfg\":187­}"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"ty­pe\":\"SYS\",\"name\":\"_M\",\"cfg\":187­}"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"ty­pe\":\"SYS\""
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS"
    ERR: UPD packet not JSON, ignore
    UDP Msg: 44.34.110.97:25965 - "_M","cfg":187}
    ERR: UPD packet not JSON, ignore
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"ty­pe\":\"SYS\",\"name\":\"_M\",\"cfg\":187­}"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"ty­pe\":\"SYS\",\"name\":\"_M\",\"cfg\":187­}"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"ty­pe\":\"SYS\",\"name\":\"_M\",\"cfg\":187­}"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"ty­pe\":\"SYS\",\"name\":\"_M\",\"cfg\":187­}"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"ty­pe\":\"SYS\",\"name\":\"_M\",\"cfg\":187­}"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"ty­pe\":\"SYS\",\"name\":\"_M\",\"cfg\":187­}"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"ty­pe\":\"SYS\",\"name\":\"_M\",\"cfg\":187­}"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"ty­pe\":\"SYS\",\"name\":\"_M\",\"cfg\":187­}"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    ] "\r\n+IPD,0,36,192.168.68.13,45962:{\"ty­pe\":\"SYS\",\"name\":\"_M\",\"cfg\":187­}"
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    
  • Next test in isolated code. Turbomode enabled debug not enabled. It crashed too.

    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M"
    ERR: UPD packet not JSON, ignore
    UDP Msg: 44.34.99.102:8807 - 87}
    ERR: UPD packet not JSON, ignore
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    UDP Msg: 192.168.68.13:45962 - {"t
    ERR: UPD packet not JSON, ignore
    UDP Msg: 121.112.101.34:8762 - S","name":"_M","cfg":187}
    ERR: UPD packet not JSON, ignore
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    UDP Msg: 192.168.68.13:45962 - {"type":"SYS","name":"_M","cfg":
    ERR: UPD packet not JSON, ignore
    -- crash --
    
  • Last message for today Gordon.
    I managed to capture a crash using the isolated code above with debug enabled.
    I changed the UDP server code to send a UDP package each 100ms and not every 10 seconds.

    ] "\r\n+IPD,0,36,1"
    ] "92.168.68.13,51158:{\"type\":\"SYS\",\"­name\":\"_M\",\"cfg\":187}"
    UDP Msg: 192.168.68.13:51158 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    ] "\r\n+IPD,0,36,192."
    ] "168.68.13,51158:{\"type\":\"SYS\",\"nam­e\":\"_M\",\"cfg\":187}"
    UDP Msg: 192.168.68.13:51158 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    ] "\r\n+IP"
    ] "D,0,36,192.168.68.13,51158:{\"type\":\"­SYS\",\"name\":\"_M\",\"cfg\":187}"
    UDP Msg: 192.168.68.13:51158 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    ] "\r\n+IPD,0,36,192.168.68.13"
    ] ",51158:{\"type\":\"SYS\",\"name\":\"_M\­",\"cfg\":187}"
    UDP Msg: 192.168.68.13:51158 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    ] "\r\n"
    ] "+IPD,0,36,192.168.68.13,51158:{\"type\"­:\"SYS\",\"name\":\"_M\",\"cfg\":187}"
    UDP Msg: 192.168.68.13:51158 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    ] "\r\n+"
    ] "IPD,0,36,192.168.68.13,51158:{\"type\":­\"SYS\",\"name\":\"_M\",\"cfg\":187}"
    UDP Msg: 192.168.68.13:51158 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    ] "\r\n+IPD,0,36,192.168.68."
    ] "13,51158:{\"type\":\"SYS\",\"name\":\"_­M\",\"cfg\":187}"
    UDP Msg: 192.168.68.13:51158 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    ] "\r\n+IPD,0,36"
    ] ",192.168.68.13,51158:{\"type\":\"SYS\",­\"name\":\"_M\",\"cfg\":187}"
    UDP Msg: 192.168.68.13:51158 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    ] "\r\n+IPD,0,36,192.168.68.13,"
    ] "51158:{\"type\":\"SYS\",\"name\":\"_M\"­,\"cfg\":187}"
    UDP Msg: 192.168.68.13:51158 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    ] "\r\n+IPD,0,36,192.1"
    ] "68.68.13,51158:{\"type\":\"SYS\",\"name­\":\"_M\",\"cfg\":187}"
    UDP Msg: 192.168.68.13:51158 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    ] "\r\n+IPD"
    ] ",0,36,192.168.68.13,51158:{\"type\":\"S­YS\",\"name\":\"_M\",\"cfg\":187}"
    UDP Msg: 192.168.68.13:51158 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    ] "\r"
    ] "\n+IPD,0,36,192.168.68.13,51158:{\"type­\":\"SYS\",\"name\":\"_M\",\"cfg\":187}"­
    UDP Msg: 192.168.68.13:51158 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    ] "\r\n+IPD,0,36,192.168"
    ] ".68.13,51158:{\"type\":\"SYS\",\"name\"­:\"_M\",\"cfg\":187}"
    UDP Msg: 192.168.68.13:51158 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    ] "\r\n+IPD,0,3"
    ] "6,192.168.68.13,51158:{\"type\":\"SYS\"­,\"name\":\"_M\",\"cfg\":187}"
    UDP Msg: 192.168.68.13:51158 - {"type":"SYS","name":"_M","cfg":187}
    OK:187
    ] "\r\n+IPD,0,36,192.168.68.13,51158:{\"ty­pe\":\"SYS\",\"name\":\"_M\",\"cfg"
    UDP Msg: 192.168.68.13:51158 - {"type":"SYS","name":"_M","cfg
    ERR: UPD packet not JSON, ignore
    --- crash ----
    

    The last message was truncated to. Not two chars.

    I think you should be abe to reproduce it with my isolated code. Just change the linux server to 100ms and enable debug on the espruino side.

  • Thanks for all your work checking this out! You seem to be having the exact same issues I had - that normal debug mode pretty much fixes it (I left it going for hours and it didn't crash). It was a great idea to try turbo mode.

    I've checked the code and I think I know what the problem is now. The actual C based UDP code always assumes that it has the whole UDP packet - but the JS code provides the data in chunks as it is received. The code handles the first packet totally fine, but then if there are only a few bytes left, when then remainder arrives there's a buffer overflow and it crashes.

    I'm working on a fix for this now that should improve the native UDP handling.

  • Ok, builds from http://www.espruino.com/binaries/travis/­master/ should now have the fix in.

    Seems to work reliably now and to always report the right host address, however there are still sometimes missed bytes from the end of UDP packets - but at least you can filter that out.

  • Hi Gordon
    Thank you very much. Will give you feedback as soon as i have tested it.
    Regards
    Sacha

  • Funny, I was just about to post a similar UDP issue and so far it looks like this fixes it!

  • Hello Gorden
    Tested it about 30 minutes. Crashing is fixed i think. A lot of garbage, that i will filter out.
    Here is the output:

    UDP Msg: 192.168.68.13:59683 - {"type":"SYS","name":"_M","cfg":187}
    UDP Msg: 192.168.68.13:59683 - {"type":"SYS","nam
    UDP Msg: 101.34.58.34:19807 - "cfg":187}
    UDP Msg: 192.168.68.13:59683 - {"type":"SYS","name":"_M","cfg":187}
    UDP Msg: 192.168.68.13:59683 - {"type":"SYS","name":
    UDP Msg: 34.95.77.34:8748 - g":187}
    UDP Msg: 192.168.68.13:59683 - {"type":"SYS","name":"_M","cfg":187}
    UDP Msg: 192.168.68.13:59683 - {"type":"SYS","name":"_M","cfg":187}
    UDP Msg: 192.168.68.13:59683 - {"type":"SYS","name":"_M","cfg":187}
    UDP Msg: 192.168.68.13:59683 - {"type":"SYS","name":"_M","cfg":187}
    UDP Msg: 192.168.68.13:59683 - {"type":"SYS","name":"_M","cfg":187}
    UDP Msg: 192.168.68.13:59683 - {"type":"SYS","name":"_M"
    UDP Msg: 44.34.99.102:8807 - 87}
    UDP Msg: 192.168.68.13:59683 - {"type":"SYS","name":"_M","cfg":187}
    UDP Msg: 192.168.68.13:59683 - {"type":"SYS","nam
    UDP Msg: 101.34.58.34:19807 - "cfg":187}
    UDP Msg: 192.168.68.13:59683 - {"type":"SYS","name":"_M","cfg":187}
    UDP Msg: 192.168.68.13:59683 - {"type":"SYS","name":"_M","cfg":187}
    UDP Msg: 192.168.68.13:59683 - {"type":"SYS","name":"_M","cfg":187}
    UDP Msg: 192.168.68.13:59683 - {"type":"SYS","name":"_M","cfg":187}
    UDP Msg: 192.168.68.13:59683 - {"type":
    UDP Msg: 34.83.89.83:11298 - ame":"_M","cfg":187}
    UDP Msg: 192.168.68.13:59683 - {"type"
    UDP Msg: 58.34.83.89:8787 - name":"_M","cfg":187}
    UDP Msg: 192.168.68.13:59683 - {"type":"SYS","name":"_M","cfg":187}
    UDP Msg: 192.168.68.13:59683 - {"type":"SYS","name":"_M","cfg":187}
    UDP Msg: 192.168.68.13:59683 - {"type":"SYS","n
    UDP Msg: 97.109.101.34:8762 - ","cfg":187}
    UDP Msg: 192.168.68.13:59683 - {"type":"SYS","name":"_M","cfg":187}
    UDP Msg: 192.168.68.13:59683 - {"type":"SYS","name":"_M","cfg":187}
    UDP Msg: 192.168.68.13:59683 - {"type":"SYS","name":"_M","cfg":187}
    UDP Msg: 192.168.68.13:59683 - {"type":"SYS","name":"_M","cfg":187}
    UDP Msg: 192.168.68.13:59683 - {"type":
    UDP Msg: 34.83.89.83:11298 - ame":"_M","cfg":187}
    UDP Msg: 192.168.68.13:59683 - {"type":"SYS","name":"
    UDP Msg: 95.77.34.44:25378 - ":187}
    UDP Msg: 192.168.68.13:59683 - {"type":"SYS","name":"_M","cfg":187}
    UDP Msg: 192.168.68.13:59683 - {"type":"SYS","name":"_M","cfg":187}
    UDP Msg: 192.168.68.13:59683 - {"type":"SYS","name":"_M","cfg":187}
    UDP Msg: 192.168.68.13:59683 - {"type"
    UDP Msg: 58.34.83.89:8787 - name":"_M","cfg":187}
    UDP Msg: 192.168.68.13:59683 - {"type":"
    UDP Msg: 83.89.83.34:8748 - me":"_M","cfg":187}
    UDP Msg: 192.168.68.13:59683 - {"ty
    UDP Msg: 112.101.34.58:21282 - ","name":"_M","cfg":187}
    

    Thanks Gorden for fixing the crash that fast.

    Sacha

  • Thanks - surprising you get the corruption that bad... For me it was 1 in 100 packets or so.

    I'm away at the moment but I'll see if I can figure out what's wrong when I return - probably next week.

  • That's fine Gordon.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

EspruinoWiFi - UDP / Broadcast not stable

Posted by Avatar for Sacha @Sacha

Actions