Left here for archival purposes.

User avatar
By n1spx
#3355 If I call a function from init.lua, I'm having trouble then getting an IP.
However, if I wait a few seconds, then the same function works fine.

Both working and non-working wait 10 seconds until they call wifi.sta.getip()
(The count lines in the not-working part are just so it doesn't stay in the loop forever.)

Please let me know what I'm doing wrong, or what I can do to help solve the problem.

Thanks,
Chuck, N1SPX

This works:
wifi.lua
Code: Select allfunction wait_wifi()

  wifi_ip = "0.0.0.0"

  while wifi_ip == "0.0.0.0" do
    print("Waiting on Wifi...\n")
    print(wifi.sta.getip())
    wifi_ip = wifi.sta.getip()
    print("New IP = "..wifi_ip.."\n")
    if wifi_ip == "0.0.0.0" then
      tmr.delay(2000)
    end
  end

  print("Got IP "..wifi_ip.."\n")
end


init.lua
Code: Select alldofile("wifi.lua")

tmr.alarm(10000,0,wait_wifi)


However, the following don't work.

wifi.lua
Code: Select allfunction wait_wifi()

count = 1

  tmr.delay(9000000)

  wifi_ip = "0.0.0.0"

  while wifi_ip == "0.0.0.0" and count < 20 do
    print("Waiting on Wifi...\n")
    wifi_ip = wifi.sta.getip()
    print("New IP = "..wifi_ip.."\n")
    if wifi_ip == "0.0.0.0" then
      tmr.delay(2000000)
    end
    count = count + 1
  end

  print("Got IP "..wifi_ip.."\n")
end


init.lua
Code: Select alldofile("wifi.lua")

tmr.alarm(1000,0,wait_wifi)
User avatar
By igrr
#3441 Esp8266 sdk, which the lua firmware is based on, has a single-threaded environment. When one timer handler is called, other timers don't fire until the first one has executed completely.
When you call wait_wifi in 1 second, WiFi is not connected yet. But it can not connect while your function is being run. WiFi stack relies on timers that are not being called because your task is in an endless loop. Timers are not ISRs, they can not preempt other tasks.

Incidentally, I know how to fix this in firmware (I.e. how to implement a delay function that would transfer execution to other waiting tasks), but since the lua firmware is not open, this is not going to happen. I can only recommend you to use short timer handlers and avoid using delay for long waits (above 500ms) to keep WiFi and TCP stacks running.
User avatar
By n1spx
#3444 Thanks for the explanation. That seems like a reasonable explanation.

I think I can work out how to use tmr.alarm instead of tmr.delay to do what I want. I'll know after a few hours of sleep.

Thanks,
Chuck
User avatar
By n1spx
#3448 Decided to knock it out before sleep. The code below does exactly what I wanted, wait until Wifi is connected before continuing. It's a bit of a kludge, needing a second init file, but with scripting, isn't a big deal.

wifi.lua
Code: Select allfunction wait_wifi()
  count = count + 1
  wifi_ip = wifi.sta.getip()
  if wifi_ip == "0.0.0.0" and count < 20 then
    tmr.alarm(1000,0,wait_wifi)
  elseif count >= 20 then
    wifi_connected = false
    print("Wifi connect timed out.")
  else
    wifi_connected = true
    print("Got IP "..wifi_ip.."\n")
    dofile("init2.lua")
  end
end


init.lua
Code: Select alldofile("wifi.lua")
count = 0
wait_wifi()