aboutsummaryrefslogtreecommitdiff
path: root/mqtt-bridge/wlan.lua
diff options
context:
space:
mode:
Diffstat (limited to 'mqtt-bridge/wlan.lua')
-rw-r--r--mqtt-bridge/wlan.lua35
1 files changed, 25 insertions, 10 deletions
diff --git a/mqtt-bridge/wlan.lua b/mqtt-bridge/wlan.lua
index 03fd923..5f45c1b 100644
--- a/mqtt-bridge/wlan.lua
+++ b/mqtt-bridge/wlan.lua
@@ -1,18 +1,33 @@
-local function is_connected()
- local ipAddr = wifi.sta.getip()
- return ipAddr ~= nil and ipAddr ~= "0.0.0.0"
+local function wait_for_connection(timeout_ms)
+ local start = tmr.now()
+ local now
+ local timeout = timeout_ms * 1000
+
+ repeat
+ local ip = wifi.sta.getip()
+
+ print("ip="..tostring(ip))
+
+ if ip then -- and ip ~= "0.0.0.0" then
+ return ip
+ end
+
+ tmr.wdclr()
+ now = tmr.now()
+ until (now - start) > timeout
+ return false
end
-function wlan_init()
- print("WLAN: Configuring")
+function wlan_init(ssid, password)
+ print("WLAN: connecting to SSID:"..ssid..", password="..password)
wifi.setmode(wifi.STATION)
- wifi.sta.config("", "")
+ wifi.sta.config(ssid, password)
- repeat
- tmr.wdclr()
- until (is_connected())
+ local ip = wait_for_connection(10 * 1000)
+ if not ip then
+ panic("Could not establish wlan connection, restarting")
+ end
- local ip = wifi.sta.getip()
print("WLAN: Configured, ip="..ip)
end