0

Im trying to detect when the module actually connects to my wifi AP, since .connect does not have a callback im doing something simple like this:

wifi.sta.config("SSID","password")
wifi.sta.connect()
tmr.delay(1000000)
i = 0
while(wifi.sta.status() ~= 5 and i < 10) do
  print("Waiting")
  print(wifi.sta.status())
  i = i + 1
  tmr.delay(1000000) 
end

But the output of .sta.status() is always 1 inside the loop. When it finish, if I send the command =wifi.sta.status() manually from the IDE it tells me 5. Why?

gre_gor
  • 6,669
  • 9
  • 47
  • 52
DomingoSL
  • 14,920
  • 24
  • 99
  • 173
  • If you run that exact code in the ide but add `print(wifi.sta.status())` at the end does the last line print the updated status? Or does it also print `1`? Are you running that in the ide directly or from a script? Could this be a cached property that gets updated in a main loop or something? – Etan Reisner Oct 22 '15 at 18:28

2 Answers2

3

If you use a recent dev firmware you can do something really event based :

wifi.setmode(wifi.STATION)
wifi.sta.config(SSID, PASSWORD)

function Success()
    tmr.stop(0)
    if (SERIAL_PRINT) then
        print("IP: " .. wifi.sta.getip())
    end
    wifi.sta.eventMonStop()
    wifi.sta.eventMonReg(wifi.STA_GOTIP, "unreg")
    dofile("mainProgram.lua")
end

function Failure()
    if (SERIAL_PRINT) then
        print("Unable to connect")
    end
    wifi.sta.eventMonStop()
    wifi.sta.eventMonReg(wifi.STA_GOTIP, "unreg")
    return 0
end

tmr.alarm(0,30000,0, function() Failure() end)
wifi.sta.connect()
wifi.sta.eventMonReg(wifi.STA_GOTIP, function() Success() end)
wifi.sta.eventMonStart()

EDIT: Please have a look to the documentation for a list of all events. If you wish to use this code, you'll have to handle the failure more cleanly.

seblucas
  • 825
  • 1
  • 7
  • 17
  • Nice because the code uses the event-based features available in the NodeMCU dev branch (since July or so). However, I see no point in stopping the connection attempts after a fixed time unless `wifi.STA_WRONGPWD` is reported. And what if the AP is restarted 2h after a successful connection? `wifi.sta.autoconnect()` is on by default by still might want to handle this event e.g. by blinking a WiFi-LED you might have attached to your device. Look into https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_en#wifistaeventmonreg for many more events you could, and probably should, register. – Marcel Stör Nov 21 '15 at 23:38
  • You're right, I should register to more events. Getting IP address is the most important and I'm an optimistic kind of guy ;). I'll rework my code. – seblucas Nov 22 '15 at 06:42
  • I don't think you necessarily need to rework the code here. It kinda solves the OP's problem. I just think a cautions warning in your post would be appropriate because your snippet doesn't represent a full-blown solution for stable WiFi connectivity. – Marcel Stör Nov 22 '15 at 09:53
1

Using tmr.delay doesnot let run the event loop, you should use a timer callback.

Then the code could be something like :

wifi.sta.config("SSID","password")
wifi.sta.connect()

i=0
tmr.alarm(1, 1000, 1, function()
    if (wifi.sta.status() ~= 5 and i < 10) then
       print("Status:"..wifi.sta.status())
       i = i + 1
    else
       tmr.stop(1)
       if (wifi.sta.status() == 5) then
          print("IP:"..wifi.sta.getip())
       else
          print("Status:"..wifi.sta.status())
       end
    end
end)
mpromonet
  • 11,326
  • 43
  • 62
  • 91