Wifi.connect() not executing its callback function

Posted on
  • When modifying some simple example code to try and figure out what is going wrong, I noticed that the callback function inside wifi.connect is not executing and no console output is shown after console.log('start').

    console.log('start');
    
    var wifi = require("Wifi");
    
    function onConnect(err) {
      console.log('begin'); // does not output
      if(err) {
        console.log("An error has occured :( ", err.message); // does not output
      } else {
        console.log("Connected with IP : ", wifi.getIP().ip); // does not output
      }
      console.log('end'); // does not output
    }
    
    wifi.connect("KNOWN_NETWORK", { password: "KNOWN_PASS" }, onConnect);
    
  • Give this a try:

    //ESP32wifi.js
    // 5 Feb2018
    
    var ssid="ssid";
    var key="keykey";
    console.log('start');
    
    var wifi = require('Wifi'); 
    wifi.connect(ssid, {password: key}, 
    function onWiFiConnect(){
      console.log("xxx");
     
      var IPobject = wifi.getIP();
     
      var IP = IPobject.ip;
      var MAC = IPobject.mac;
     
      console.log("IP:");
      console.log(IP);
     
      console.log("MAC:");
      console.log(MAC);
    });
     
    

    Which produces:

     1v95 Copyright 2017 G.Williams
    Espruino is Open Source. Our work is supported
    only by sales of official boards and donations:
    http://espruino.com/Donate
    >start
    =undefined
    xxx
    IP:
    192.168.1.14
    MAC:
    24:0a:c4:00:97:2a
    > 
    

    At first your code wouldn't load and would complain the module wifi was not found.
    The cure was to exit WebIDE and uplug then start over.

  • Here's another one closer to your version:

    //ESP32wifi1.js
    // 5 Feb2018
    
    var ssid="sasas";
    var key="password";
    
    console.log('start');
    
    var wifi = require('Wifi'); 
    
    var onWiFiConnect=function(){
      var IPobject = wifi.getIP();
      var IP = IPobject.ip;
      var MAC = IPobject.mac;
      console.log("IP:");
      console.log(IP);
    // if IP is 0.0.0.0 failed to connect have to reset to clear 
      console.log("MAC:");
      console.log(MAC);
    };
     
    wifi.connect(ssid, {password: key},onWiFiConnect()); 
    

    And the output:

     1v95 Copyright 2017 G.Williams
    Espruino is Open Source. Our work is supported
    only by sales of official boards and donations:
    http://espruino.com/Donate
    >start
    IP:
    192.168.1.14
    MAC:
    24:0a:c4:00:97:2a
    =undefined
    > 
    
  • Terrific, thanks so much. I was never getting an error at all, certainly not one saying the Wifi module was missing. Is there anything I can do to enable error reporting or something similar? I hate to bother you guys for such simple issues.

  • Try this one with a valid ssid and key and then try it with a bogus ssid. Look for the disconnect message in the second case. Also there are console.log statements sprinkled in to clarify that the code executes in an order that you may not expect.

    //ESP32wifi.js
    // 6 Feb2018
    
    // http://www.espruino.com/Reference#Wifi
    
    var ssid="myssid";
    var key="mypassword";
    var port = 8080;
    
    console.log('start');
    
    var wifi = require('Wifi'); 
    console.log("1");
    
    // use browser at your connected IP address
    // http://192.168.1.14:8080/
    function processRequest (req, res) {
      console.log("XXX");
      res.writeHead(200);
      res.end('<text>Hello World');
    }
    console.log("2");
    
    var onWiFiConnect=function(){
      var IPobject= wifi.getIP();
    IPobject= wifi.getIP();
      var IP = IPobject.ip;
       var MAC = IPobject.mac;
       console.log("IP:");
       console.log(IP);
       console.log("MAC:");
       console.log(MAC);
    
        var http = require('http');
        http.createServer(processRequest).listenĀ­(port);
        console.log(`Web server running at http://${wifi.getIP().ip}:${port}`);
    //  });
    };
    console.log("3");
     
    wifi.connect(ssid, {password: key},onWiFiConnect()); 
    console.log("4");
    
    wifi.on('disconnected', function(details) { 
      console.log("disconnected");
    });
    console.log("5");
    console.log(wifi.getIP());
    

    Example output:

     1v95 Copyright 2017 G.Williams
    Espruino is Open Source. Our work is supported
    only by sales of official boards and donations:
    http://espruino.com/Donate
    >start
    1
    2
    3
    IP:
    192.168.1.14
    MAC:
    24:0a:c4:00:97:2a
    Web server running at http://192.168.1.14:8080
    4
    5
    {
      "ip": "192.168.1.14",
      "netmask": "255.255.255.0",
      "gw": "192.168.1.1",
      "mac": "24:0a:c4:00:97:2a"
     }
    =undefined
    XXX
    XXX
    > 
    
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Wifi.connect() not executing its callback function

Posted by Avatar for espftw @espftw

Actions