From 967254d72ba5e116f3019937660c346d202af7f7 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 3 Oct 2015 17:14:41 +0200 Subject: wip --- mqtt-bridge/wlan.lua | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) (limited to 'mqtt-bridge/wlan.lua') 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 -- cgit v1.2.3