diff --git a/http/connect_ap.lua b/http/connect_ap.lua new file mode 100644 index 0000000..1832256 --- /dev/null +++ b/http/connect_ap.lua @@ -0,0 +1,45 @@ +-- Author: moononournation +-- Notes by Marcos: This example could be improved quite a bit. +-- We should provide a way to return available access points as JSON, then populated +-- a drop down list using JavaScript every 5-10 seconds. I'm not sure it's worth it, +-- however. + +return function (connection, req, args) + dofile('httpserver-header.lc')(connection, 200, 'html') + + connection:send('
IP: ' .. ip .. '
') + end + connection:send('') + elseif req.method == 'POST' then + local rd = req.getRequestData() + + collectgarbage() + wifi.sta.config(rd['ssid'], rd['pwd']) + wifi.sta.connect() + local joinCounter = 0 + local joinMaxAttempts = 15 + tmr.alarm(0, 1000, 1, function() + local ip = wifi.sta.getip() + if ip == nil and joinCounter < joinMaxAttempts then + joinCounter = joinCounter + 1 + else + if joinCounter >= joinMaxAttempts then + connection:send('Failed to connect to WiFi Access Point.
') + else + connection:send('IP: ' .. ip .. '
') + end + tmr.stop(0) + joinCounter = nil + joinMaxAttempts = nil + collectgarbage() + end + end) + end + + connection:send('') +end