Problem saving the IP address for later use.

Posted on
  • PICO shimmed to ESP8266.
    I use wifi.getIP() and print the resulting IP address, then assign it to a global variable.
    Printing the global variable MyIP doesn’t work.
    Later typing MyIP; into the left pane I get the IP address.
    So what’s going on and is there a solution?

    //MyIP.js
    //26Sep 2016
    //espruino board with ESP8266
    //PICO  with ESP8266
    //var Hardware=0; //Espruino board
    var Hardware =1; //PICO
    var SSID="ssis";
    var key= "routerpw";
    var Serial;
    var MyIP="";
    function test(){
    if(Hardware===1)Serial=Serial2;
    if(Hardware===0)Serial=Serial4;
    if(Hardware===1){
     digitalWrite(B9,1); // enable on Pico Shim V2
     Serial.setup(115200, { rx: A3, tx : A2 }); //Pico
    }
    if(Hardware===0)Serial.setup(115200, { rx: C11, tx : C10 }); 
    //espruino board
    console.log("Start connection process");
    var wifi = require("ESP8266WiFi_0v25").connect(Seri­al, function(err) {
      if (err)return 1;// throw err;
    console.log("Reset the ESP8266");
      wifi.reset(function(err) {
        if (err)return 1;// throw err;
        console.log("Connecting to WiFi");
        wifi.connect(SSID,key, function(err) {
          if (err)return 1;//throw err;
          wifi.getIP(function(l,ip){console.log("I­P= ",ip,"\n\r"+l);MyIP=ip;});
          console.log("Wi-Fi Connected");
          console.log("MyIP= ",MyIP,"XXX");
          console.log("MyIP= "+MyIP+"XXX");
    // Now you can do something, 
     var server = require("net").createServer(function(c) {
      // A new client as connected
      c.write("Hello");
      c.on('data', function(data) {
        console.log(">"+JSON.stringify(data));
      });
      c.end();
    });
    server.listen(1234);
        });
      });
    });
    }//end test
    var x;
    console.log("Start");
    x=test();
    if(x!==0)console.log("Try again");//error seen
    if(x===0) console.log("Exit Seen");
    

    The output:

    >echo(0);
    Start
    Start connection process
    Try again
    =undefined
    Reset the ESP8266
    Connecting to WiFi
    Wi-Fi Connected
    MyIP=   XXX
    MyIP= XXX
    IP=  192.168.1.3
    null
    >MyIP;
    ="192.168.1.3"
    >
    
    

    1 Attachment

  • I think that's as expected. wifi.getIP returns almost immediately, and only calls the callback when the ESP8266 responds with the IP address. It means that the callback won't have got called by the time your console.log calls execute, and the IP will stay empty.

    ... if we could get the IP address immediately, wifi.getIP could have returned it directly, rather than faffing with the callbacks :)

  • The assignment MyIP=ip; is done in the callback. That's what's puzzling. The console.log(ip) is also done in the callback before the assignment and displays the IP address. It's likely a timing issue but still puzzling.

    I've got UDP working (not this example code). I broadcast using IP=255.255.255.255 and send a keyword and local IP address. If the keyword matches, the other end broadcasts using a keyword and it's local IP address. So I need the local IP address from the getIP function to finish this.
    Msg1: Hi I'm Billy Bob at IPxxx, where are you Sally?
    Msg2: Hi I'm Sally at IPyyy.
    Then Billy Bob can use IPyyy to contact Sally.
    Right now I'm hard coding the local IP addresses, but that defeats the purpose since the next time my router resets the IP addresses will be reassigned.

  • I found a solution:
    Use a SetInterval and wait for the IP address to show up.

          wifi.getIP(function(l,ip){console.log("I­P= ",ip,"\n\r"+l);xIP=ip;});
    id=setInterval(function () {
      console.log("Hello World");
      if(xIP!==undefined){
       console.log("xIP= ",xIP);
      clearInterval(id);
          console.log("Wi-Fi Connected ");
    // Now you can do something, 
          var client = require("net").connect({host: IP, port: 1234,protocolVersion: 17}, function() {
      console.log('client connected');
      client.write("Jello"+xIP);
      client.on('data', function(data) {
        console.log(">"+JSON.stringify(data)+"xj­ello");
      client.write("Reply"+xIP);
      });
      client.on('end', function() {
        console.log('client disconnected');
      });
    
    //client.end("UTLEY");
    });
      }
    }, 1000);
    

    And the output:

    >echo(0);
    Start
    Start connection process
    Try again
    =undefined
    Reset the ESP8266
    Connecting to WiFi
    OKxx1
    OKxx2
    IP=  192.168.1.3
    null
    Hello World
    xIP=  192.168.1.3
    Wi-Fi Connected
    Client
    client connected
    Send 0 Jello192.168.1.3
    > 
    

    And the Billy Bob device

    >echo(0);
    Start
    Start connection process
    Try again
    =undefined
    Reset the ESP8266
    Connecting to WiFi
    OKxx1
    OKxx2
    Wi-Fi Connected
    Client
    client connected
    IP=  192.168.1.4
    null
    "192.168.1.4"
    Send 0 XX 192.168.1.4Jello
    >"Reply192.168.1.3"xjello
    >
    Disconnected
    

    I expect to post the UDP in a few days after some code cleanup.
    It modifies the ESP8266WiFi_0v25 module to allow UDP.
    var wifi = require("UDP").connect(Serial, function(err) {
    //var wifi = require("ESP8266WiFi_0v25").connect(Seri­al, function(err) {

  • wifi.getIP(function(l,ip){console.log("I­P= ",ip,"\n\r"+l);xIP=ip;});
    

    I don't see this is puzzling: You're logging ip, the parameter - so of course it's going to be right.

    You could always do what you want from the getIP callback itself, rather than using setInterval?

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

Problem saving the IP address for later use.

Posted by Avatar for ClearMemory041063 @ClearMemory041063

Actions