Post your best Lua script examples here

User avatar
By ohgary
#32596 I am trying to build a device that will connect to a local router, but if it fails then go into an AP mode.
I have each setup wifi and Setup AP working seperately but when I try to integrate them I am having issue.
My issue is around how to introduce a "sleep" while waiting for the wifi setup to establish.

Here is some mocked up code.
-- testing a loop
--Need to add a delay while waiting for wifi status.

trycount=0
badreturn=5

print ("setting up ssid")

tmr.alarm(1, 1000, 1, function()
if badreturn <= 4 then
print("IP unavaiable, Waiting try " .. trycount )
trycount = trycount + 1
if trycount >= 10 then
print ("setting up AP mode")
tmr.stop(1)
end -- end if trycount
else
tmr.stop(1)
end --end if status
end) --end tmr


print ("SHOW my IPs")


When I run it with badreturn=5, ie a good wifi setup it seems to work correctly
and I see on my debug window

setting up ssid
SHOW my IPs


When I set badreturn=4, ie a failed wifi setup, keep trying then go into ap mode. I see this

setting up ssid
SHOW my IPs
> IP unavaiable, Waiting try 0
IP unavaiable, Waiting try 1
IP unavaiable, Waiting try 2
IP unavaiable, Waiting try 3
IP unavaiable, Waiting try 4
IP unavaiable, Waiting try 5
IP unavaiable, Waiting try 6
IP unavaiable, Waiting try 7
IP unavaiable, Waiting try 8
IP unavaiable, Waiting try 9
setting up AP mode


Why is the SHOW my IPs running before the timer loop completes? How Can get a sleep/wait/delay into my script soemthing like this

-- testing a loop
--Need to add a delay while waiting for wifi status.

trycount=0
badreturn=4

print ("setting up ssid")


if badreturn <= 4 then
print("IP unavaiable, Waiting try " .. trycount )
trycount = trycount + 1
if trycount >= 10 then
print ("setting up AP mode")
end -- end if trycount
SLEEP(1) -- sleep 1 second

end --end if status
end) --end tmr


print ("SHOW my IPs")

Baring no solution, anyone have code that will try to connect to a WIFI and if it fails go into AP mode.

Many thanks. -gary
User avatar
By TerryE
#32790 Hi. do us all a favour and edit your post to use the code tag. You'll stand a far better chance of getting an answer if your q is readable.

You have to set your mode as STATION explicitly before you connect. If you have a dev build not only can you have far bigger programs, but you can also use the event monitor to make you connection code a lot simpler.