• Setup is as documented (and required to start with saved code after power on:

    var WIFI_NAME = "mySSID";
    var WIFI_OPTIONS = { password : "mySSIDPassword" };
    var wifi = null;
    
    function onInit() {
      wifi = require("EspruinoWiFi");
      wifi.connect(WIFI_NAME, WIFI_OPTIONS, function(err) {
        if (err) {
          console.log("Connection error: "+err);
          return;
        }
        console.log("Connected!");
        console.log("getIP() returned: ",wifi.getIP(function(val){ console.log("getIP() in callback: ",val); }));
      });
    }
    

    Console:

     _____                 _
    |   __|___ ___ ___ _ _|_|___ ___
    |   __|_ -| . |  _| | | |   | . |
    |_____|___|  _|_| |___|_|_|_|___|
              |_| http://espruino.com
     1v94 Copyright 2016 G.Williams
    >
    =undefined
    >save()
    =undefined
    Erasing Flash...
    Writing..........................
    Compressed 114368 bytes to 23555
    Checking...
    Done!
    Running onInit()...
    Connected!
    getIP() returned:  undefined
    getIP() in callback:  null
    > 
    

    Even executed much later, I get the same undefined / null.

  • That's as you'd expect I think?

    /* Get the IP and MAC address when connected to an AP and call 
    `callback(err, { ip : ..., mac : ...})`. If err isn't null,
    it contains a string describing the error. This doesn't work
    when only in AP mode (the IP address is always 192.168.4.1)
    */
    exports.getIP = function (callback) { ... }
    

    So it's that there are two arguments returned by the callback - err and ip. It's odd, but it's just the way node.js tended to handle callback functions that can return an error, so I wanted to keep things compliant with them.

    Since on EspruinoWiFi the WiFi is basically async, getIP can't immediately return an IP address.

    Unfortunately when running on ESP8266 the callback format is different - it's something I'd like to change to bring everything more in-line really.

  • doooh... me... sorry bothering you for the callback... now I recall that the first argument is the error, and if first argument - error - is null, second argument is the actual data...

    I adjusted my code and it does what I needed. THANKS...

    Adjusted with 'assuming' I get no err(or) in getIP():

    ...
      wifi.getIP(function(err,data){ console.log("getIP() in callback: ",data.ip); }));
    ...
    

    with console output:

    ...
    Running onInit()...
    Connected!
    getIP() in callback:  192.168.0.113
    >
    
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Espruino/Wifi - getIP() returns undefined but callback receives null,ip

Posted by Avatar for allObjects @allObjects

Actions