- Mon May 04, 2015 5:35 am
#16444
good morning.
Code: Select allfunction SaveX(sErr)
if (sErr) then
print (sErr)
s.err = sErr
end
file.remove("x")
file.open("x","w+")
for k, v in pairs(s) do
file.writeline(k .. "=" .. v)
end
file.close()
collectgarbage()
end
--local iFail = 10
function wait_for_wifi_conn ( )
tmr.alarm (1, 1000, 1, function ( )
-- iFail = iFail -1
-- if (iFail == 0) then
-- SaveX("could not access "..s.ssid)
-- node.restart()
-- end
if wifi.sta.getip ( ) == nil then
--print(iFail)
print(s.ssid)
else
tmr.stop (1)
print ("ip: " .. wifi.sta.getip ( ))
sk=net.createConnection(net.TCP, 0)
sk:on("receive", NewLua)
sk:connect(80,s.host)
sGet = "GET /".. s.path .. " HTTP/1.1\r\nHost: " .. s.domain .. "\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n"
print(sGet)
sk:send(sGet)
end
collectgarbage()
end)
end
function NewLua(sck,c)
local nStart, nEnd = string.find(c, "\n\n")
if (nEnde == nil) then
nStart, nEnd = string.find(c, "\r\n\r\n")
end
c = string.sub(c,nEnd+1)
print("lua length: "..string.len(c))
if (string.sub(c,0,5) ~= "--lua") then
SaveX(s.domain.."/"..s.path .. " does not begin with --lua")
node.restart()
return
end
file.remove("do.lua")
file.open("do.lua","w+")
file.writeline(c)
file.close()
node.compile("do.lua")
dofile("do.lua")
dofile("do.lc")
collectgarbage()
end
print(collectgarbage("count").." kB used")
s = {ssid="", pwd="", host="", domain="", path="", err=""}
if (file.open("x","r")) then
local sF = file.read()
--print("setting: "..sF)
file.close()
for k, v in string.gmatch(sF, "([%w.]+)=([%S ]+)") do
s[k] = v
print(k .. ": " .. v)
end
end
if ((s.err == "") and s.host and s.domain and s.path) then
wifi.setmode (wifi.STATION)
wifi.sta.config(s.ssid, s.pwd)
wifi.sta.autoconnect (1)
wait_for_wifi_conn ( )
else
wifi.setmode(wifi.SOFTAP)
wifi.ap.config({ssid="node_"..node.chipid(), pwd=""})
srv=net.createServer(net.TCP)
print(wifi.sta.getip())
srv:listen(80,function(conn)
conn:on("receive", function(client,request)
local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
if (vars ~= nil)then
for k, v in string.gmatch(vars, "(%w+)=([^&]*)&*") do
s[k],n = string.gsub(v,"%%2F","/")
print(k .. " = " .. s[k])
end
SaveX()
if ((s.err == "") and s.host and s.domain and s.path) then
node.restart()
end
end
file.open("y.htm","r")
local sH = file.read()
for k, v in pairs(s) do
sH,n = string.gsub(sH,"_"..k,v)
end
print(sH)
file.close()
client:send(sH)
client:close()
collectgarbage()
end)
end)
end
have moved the html into a template:
Code: Select all<html><body><form>
error: <input name=err value='_err'/><br/>
<h2>access point:</h2>
SSID: <input name=ssid value='_ssid'/><br/>
pwd: <input name=pwd value='_pwd'/><br/>
<h2>fetch lua:</h2>
ip: <input name=host value='_host'/><br/>
domain: <input name=domain value='_domain'/><br/>
path: <input name=path value='_path'/><br/>
<input type=submit value='submit'/>
</form></body></html>
the "x" setting file looks like this:
Code: Select allhost=81.169.145.148
path=do.lua
ssid=G3_1234
pwd=password
err=x
domain=robosoft.de
It is created when the form is posted. But always nice to add a "x" to err and let ESPlorer upload it to go back to access point mode
Terry you are a nice guy but you should take for granted that i am not.
collectgarbage("count") yields 11 kB. Don't know why i am so close to outOfMemory.
Yesterday it took me hours to flash the firmeware (NodeMCU 0.9.5 build 20150318) and get the ESPlorer running :-(
I wrongly used the ssid+pwd of my smartphone hotspot (LG G3) on my ESP-201 in the first time.
And even so my G3 is turned off, i have flashed the 201 several times and there is no such ssid in the lua code any longer, the 201 still and only shows up with the ssid+pwd of my smartphone - what a messy behavior :-(
i guess, the init.lua could have a timer to look for a new download "once a day" while the downloaded lua file is running :-)
I post the code here so someone with more expertise might be happy to NOT help me
but make the world a better place.
yet in this ego driven toy world.. everyone only works for himself..