forked from peter-kutak/NODEMCU-LUA-OTA-ESP8266
-
Notifications
You must be signed in to change notification settings - Fork 3
/
init.lua
executable file
·86 lines (75 loc) · 1.99 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
SCRIPTNAME_CLIENT = "client.lua"
function LoadX()
s = {ssid="", pwd="", host="", domain="", path="", err="",boot="",update=0}
if (file.open("s.txt","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
end
function SaveXY(sErr)
if (sErr) then
s.err = sErr
end
file.remove("s.txt")
file.open("s.txt","w+")
for k, v in pairs(s) do
file.writeline(k .. "=" .. v)
end
file.close()
collectgarbage()
end
function update()
conn=net.createConnection(net.TCP, 0)
conn:on("connection",function(conn, payload)
conn:send("GET /"..s.path.."/node.php?id="..id.."&update"..
" HTTP/1.1\r\n"..
"Host: "..s.domain.."\r\n"..
"Accept: */*\r\n"..
"User-Agent: Mozilla/4.0 (compatible; esp8266 Lua;)"..
"\r\n\r\n")
end)
conn:on("receive", function(conn, payload)
if string.find(payload, "UPDATE")~=nil then
s.boot=nil
SaveXY()
node.restart()
end
payload = nil
conn:close()
conn = nil
end)
conn:connect(80,s.host)
end
id = node.chipid()
print ("nodeID is: "..id)
print(collectgarbage("count").." kB used")
LoadX()
if (s.host~="") then
--if (s.host and s.domain and s.path) then
if (tonumber(s.update)>0) then
tmr.alarm (0, tonumber(s.update)*60000, 1, function()
print("checking for update")
update()
end)
end
local fileFound
local filenameBoot
if (s.boot~="") then
fileFound=io.open(s.boot, "r")
if (fileFound~=nil) then
filenameBoot = s.boot
else
filenameBoot = SCRIPTNAME_CLIENT
end
else
filenameBoot = SCRIPTNAME_CLIENT
end
dofile(filenameBoot)
else
dofile("server.lua")
end