Chat freely about anything...

User avatar
By manjas51
#12631 hello,

I've been googling and sursing here a solution to connect to my Lan.

here is my code:

Code: Select allip = wifi.sta.getip();
if nil == ip then
        --wifi.setmode(wifi.STATIONAP);
      wifi.setmode(wifi.STATION);
        --wifi.setmode(wifi.SOFTAP);
      wifi.sta.config("mylan","mypsswd");
      wifi.sta.connect();
      --wifi.sta.autoconnect(1)
      tmr.delay (10000000);
        for n = 1, 15 do
                ip = wifi.sta.getip();
            print(ip);
                if nil ~= ip then
                        break;
                end
                print ("no IP yet...");
                tmr.delay (10000000);
        end
end

if nil == ip then
        print ("no IP");
else
        print ("Node MAC: " .. wifi.sta.getmac());
        print ("Node  IP: " .. ip);
end

print ("done");


I tried different combinaisons
wifi.setmode(wifi.STATIONAP);
wifi.setmode(wifi.STATION);
wifi.setmode(wifi.SOFTAP);

wifi.sta.connect();
wifi.sta.autoconnect(1);
or both in comment

but no way

here is always the same result:

Code: Select allNodeMCU 0.9.5 build 20150212  powered by Lua 5.1.4
nil
no IP yet...
nil
no IP yet...
nil
no IP yet...
nil
no IP yet..


but if I do it manually in lualoader: :twisted:
It works..
Code: Select allwifi.setmode(wifi.STATION)
> wifi.sta.config("mylan","mypsswd")
> = wifi.sta.getip()
192.168.1.7   255.255.255.0   192.168.1.1
> = wifi.sta.status()
5 GOT IP
>


Any help appracied
User avatar
By GerryKeely
#12633 Hi

i'm no expert but usual way to do it is to use a timer based function
Code: Select allfunction check_wifi()
 local ip = wifi.sta.getip()
 
 if(ip==nil) then
   print("Connecting...")
 else
  tmr.stop(0)
  print("Connected to AP!")
  print(ip)
 end
 
end
 
tmr.alarm(0,2000,1,check_wifi)


Above code is taken from here http://allaboutee.com/2015/03/24/es8266 ... d-nodemcu/

Gerry