-
• #2
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 yourconsole.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 :) -
• #3
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. -
• #4
I found a solution:
Use a SetInterval and wait for the IP address to show up.wifi.getIP(function(l,ip){console.log("IP= ",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)+"xjello"); 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(Serial, function(err) { -
• #5
wifi.getIP(function(l,ip){console.log("IP= ",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 usingsetInterval
?
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?
The output:
1 Attachment