WiFiSwitch/init.lua

43 lines
1.0 KiB
Lua
Raw Normal View History

2015-09-10 15:28:09 +02:00
-- Constants
2015-09-11 09:51:54 +02:00
SSID = "MessWLAN"
APPWD = "UNVmpwbr6heQnMQ7ykXT"
CMDFILE = "mqtt.lua"
2015-09-10 15:28:09 +02:00
-- Some control variables
2015-09-11 09:51:54 +02:00
wifiTrys = 0
NUMWIFITRYS = 200
2015-09-10 15:28:09 +02:00
function launch()
print("Connected to WIFI!")
print("IP Address: " .. wifi.sta.getip())
2015-09-11 09:51:54 +02:00
tmr.alarm(0, 5000, 0, function() dofile(CMDFILE) end )
2015-09-10 15:28:09 +02:00
end
function checkWIFI()
if ( wifiTrys > NUMWIFITRYS ) then
print("Sorry. Not able to connect")
else
ipAddr = wifi.sta.getip()
if ( ( ipAddr ~= nil ) and ( ipAddr ~= "0.0.0.0" ) )then
tmr.alarm( 1 , 500 , 0 , launch )
else
tmr.alarm( 0 , 2500 , 0 , checkWIFI)
print("Checking WIFI..." .. wifiTrys)
wifiTrys = wifiTrys + 1
end
end
end
print("-- Starting up! ")
ipAddr = wifi.sta.getip()
if ( ( ipAddr == nil ) or ( ipAddr == "0.0.0.0" ) ) then
print("Configuring WIFI....")
wifi.setmode( wifi.STATION )
wifi.sta.config( SSID , APPWD)
print("Waiting for connection")
2015-09-11 09:51:54 +02:00
tmr.alarm( 0 , 2500 , 0 , checkWIFI )
2015-09-10 15:28:09 +02:00
else
launch()
end