So, I'm testing and playing and get stuck on some strange behavior I can not solve.
- I'm using NodeMCU 0.9.5 build 20150318
- I use LuaLoader to program my Huzzah
I'm trying to build a simple program that will allow users to connect to the the device through a web interface, pick a network and supply a password, post that to the device and have it save the settings to a file. pretty straight forward.
If I copy the code below and use LuaLoader's "paste text" option to excute it on the device it works fine
If I save it to a file on the device and then load it using dofile() the device crashes and restarts without any apparent error output.
What am I missing?
print('starting web server')
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive",function(conn,request)
-- OUTPUT FULL REQUEST
print(request)
-- CHECK IF SSID WAS POSTED
if(string.match(request,'SSID=')) then
-- ADD CODE HERE TO SAVE WIFI SETTINGS TO FILE
conn:send("<h1>Rebooting....</h1>")
conn:close();
collectgarbage();
tmr.alarm(2, 1000, 0, function()
-- WAIT A SECOND, THEN REBOOT
print("Restarting...")
node.restart()
end)
else
-- SHOW LIST OFF SSID'S
conn:send("<h1>Scanning Access Points:</h1>")
wifi.sta.getap(function(table)
if table then
print('Wifi scan done')
conn:send('<form method="post">')
conn:send('network:<BR><select name="SSID">')
for k,v in pairs(table) do
network = trim(k)
conn:send('<option value="' .. network .. '">|' .. network .. '|</option>')
end
conn:send('</select><BR>')
conn:send('password:<BR><input type="text" name="password" value=""><BR>')
conn:send('<input type="submit" value="connect"><BR>')
conn:send('</form>')
else
print('Wifi scan done, nothing found')
conn:send("<h1>No networks found:</h1>")
end
conn:close();
collectgarbage();
end)
end
end)
end)