• The Wifi module builtin since 1.96 (many thanks, Gordon) connects as expected.
    However, it seems to do an extra callback when it connects.
    The code below is supposed to pulse the green LED on success and the red one on wifi timeout.
    On connect both LEDs go on, at the same time.

    var addr=null;
    
    function cbwi(err) {   
        if (err) {
            digitalPulse(LED1, 1, 1000);
            cowi();   
        } else {
            digitalPulse(LED2, 1, 1000);
            wifi.getIP(function(a) {addr=a;});
            net.createServer((conn) => {
                conn.on('close', USB.setConsole);
                conn.pipe(LoopbackA);
                LoopbackA.pipe(conn);
                LoopbackB.setConsole();
            }).listen(23);   
        }
    }
    
    function cowi() {
          wifi.connect('HXNET1',{password:'xxx'}, function(err) {cbwi(err)});
    }
    
    cowi();
    

    While the connection definitely succeeds (ping replies are sent), the value of the variable addr is 'Timeout'. And the telnet server doesn't get startet.
    What's going on here?

  • That's odd. I'll check it out when i get back at the end of the week.

    However you might have some success trying to create the server from inside the callback of getIP.

    Since everything is async you're trying to do both at the same time. I thought it was fixed, but I've had very similar problems to that in the past - not that it explains the two LEDs lighting.

  • ... also i believe you're not using getIP correctly - the first argument is the callback is the error or null, second is the ip - as functions tend to be done in node.js

    ... so even if it did work, you would never see the IP in addr.

  • Ok, I just tried this and it would appear to be fine - apart from the IP thing.

    Here's the code I used:

    var wifi=require("Wifi");
    var net=require("net");
    var addr=null;
    function cbwi(err) {   
        if (err) {
            digitalPulse(LED1, 1, 1000);
            cowi();   
        } else {
            digitalPulse(LED2, 1, 1000);
            wifi.getIP(function(err,a) {
              addr=a;
              console.log("IP ",addr);
            });
            net.createServer((conn) => {
              conn.on('close', USB.setConsole);
              conn.pipe(LoopbackA);
              LoopbackA.pipe(conn);
              LoopbackB.setConsole();
            }).listen(23);
        }
    }
    function cowi() {
      console.log("Connecting");
      wifi.connect('...',{password:'...'}, function(err) {cbwi(err);});
    }
    cowi();
    

    I haven't seen a double callback at any point. If you're getting two callbacks it might be that cowi is getting called twice in the first place? The code you posted wasn't complete (eg. it was missing require("net")/etc) so is it possible that some of the other code you had in there was doing something?

    You could also try typing reset(true) - it's possible you had some code saved in the device already (via 'save on send?') that was trying to access WiFi.

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

EspruinoWifi: builtin wifi module calling callback twice?

Posted by Avatar for Steffen @Steffen

Actions