2016-06-18 19:35:22 +02:00
|
|
|
--TODO:
|
|
|
|
configFile="config.lua"
|
|
|
|
|
2019-04-03 20:23:07 +02:00
|
|
|
httpSending=false
|
2016-12-21 23:56:37 +01:00
|
|
|
sentBytes=0
|
2016-12-29 21:57:12 +01:00
|
|
|
function sendPage(conn, nameOfFile, replaceMap)
|
2017-01-03 13:48:24 +01:00
|
|
|
collectgarbage()
|
2017-01-03 14:27:30 +01:00
|
|
|
print("Sending " .. nameOfFile .. " " .. sentBytes .. "B already; " .. node.heap() .. "B in heap")
|
2016-12-29 21:01:15 +01:00
|
|
|
conn:on("sent", function(conn)
|
|
|
|
if (sentBytes == 0) then
|
|
|
|
conn:close()
|
2016-12-29 21:27:27 +01:00
|
|
|
print("Page sent")
|
2017-01-03 13:48:24 +01:00
|
|
|
collectgarbage()
|
2019-04-03 20:23:07 +02:00
|
|
|
httpSending=false
|
2016-12-30 15:41:19 +01:00
|
|
|
else
|
2019-04-03 20:23:07 +02:00
|
|
|
collectgarbage()
|
2016-12-29 21:57:12 +01:00
|
|
|
sendPage(conn, nameOfFile, replaceMap)
|
2016-12-29 21:01:15 +01:00
|
|
|
end
|
|
|
|
end)
|
2016-12-17 02:09:23 +01:00
|
|
|
|
2016-12-29 21:01:15 +01:00
|
|
|
if file.open(nameOfFile, "r") then
|
2016-12-29 21:27:27 +01:00
|
|
|
local buf=""
|
|
|
|
if (sentBytes <= 0) then
|
|
|
|
buf=buf .. "HTTP/1.1 200 OK\r\n"
|
|
|
|
buf=buf .. "Content-Type: text/html\r\n"
|
|
|
|
buf=buf .. "Connection: close\r\n"
|
|
|
|
buf=buf .. "Date: Thu, 29 Dec 2016 20:18:20 GMT\r\n"
|
|
|
|
buf=buf .. "\r\n\r\n"
|
|
|
|
end
|
2016-12-29 21:01:15 +01:00
|
|
|
-- amount of sent bytes is always zero at the beginning (so no problem)
|
|
|
|
file.seek("set", sentBytes)
|
|
|
|
|
|
|
|
local line = file.readline()
|
2016-12-29 21:57:12 +01:00
|
|
|
|
2016-12-17 02:09:23 +01:00
|
|
|
while (line ~= nil) do
|
2016-12-29 21:57:12 +01:00
|
|
|
-- all placeholder begin with a $, so search for it in the current line
|
|
|
|
if (line:find("$") ~= nil) then
|
|
|
|
-- Replace the placeholder with the dynamic content
|
|
|
|
if (replaceMap ~= nil) then
|
|
|
|
for key,value in pairs(replaceMap)
|
|
|
|
do
|
|
|
|
line = string.gsub(line, key, value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-04-03 20:23:07 +02:00
|
|
|
|
|
|
|
|
2019-03-29 18:35:07 +01:00
|
|
|
-- increase the amount of sent bytes
|
|
|
|
sentBytes=sentBytes+string.len(line)
|
|
|
|
|
2016-12-17 02:09:23 +01:00
|
|
|
buf = buf .. line
|
2016-12-29 21:57:12 +01:00
|
|
|
|
2019-03-23 00:29:18 +01:00
|
|
|
-- Sent after 500 bytes data
|
|
|
|
if ( (string.len(buf) >= 500) or (node.heap() < 2000) ) then
|
2016-12-17 02:09:23 +01:00
|
|
|
line=nil
|
2016-12-29 21:01:15 +01:00
|
|
|
conn:send(buf)
|
2016-12-30 15:41:19 +01:00
|
|
|
print("Sent part of " .. sentBytes .. "B")
|
2016-12-21 23:56:37 +01:00
|
|
|
-- end the function, this part is sent
|
|
|
|
return
|
2016-12-17 02:09:23 +01:00
|
|
|
else
|
|
|
|
-- fetch the next line
|
2016-12-29 21:01:15 +01:00
|
|
|
line = file.readline()
|
2016-12-17 02:09:23 +01:00
|
|
|
end
|
|
|
|
end
|
2016-12-29 21:01:15 +01:00
|
|
|
--reset amount of sent bytes, as we reached the end
|
|
|
|
sentBytes=0
|
|
|
|
-- send the rest
|
|
|
|
conn:send(buf)
|
2016-12-30 15:41:19 +01:00
|
|
|
print("Sent rest")
|
2016-12-17 02:09:23 +01:00
|
|
|
end
|
|
|
|
|
2016-12-29 23:20:40 +01:00
|
|
|
end
|
2016-12-17 02:09:23 +01:00
|
|
|
|
2017-01-03 14:27:30 +01:00
|
|
|
function fillDynamicMap()
|
2016-12-29 23:20:40 +01:00
|
|
|
replaceMap = {}
|
|
|
|
ssid, _ = wifi.sta.getconfig()
|
|
|
|
|
|
|
|
if (ssid == nil) then
|
|
|
|
ssid="Not set"
|
|
|
|
end
|
|
|
|
if (sntpserverhostname == nil) then
|
|
|
|
sntpserverhostname="ptbtime1.ptb.de"
|
|
|
|
end
|
|
|
|
if (timezoneoffset == nil) then
|
|
|
|
timezoneoffset=1
|
|
|
|
end
|
|
|
|
-- Set the default color, if nothing is set
|
|
|
|
if (color == nil) then
|
|
|
|
color=string.char(0,0,250)
|
|
|
|
end
|
|
|
|
if (color1 == nil) then
|
|
|
|
color1=color
|
|
|
|
end
|
|
|
|
if (color2 == nil) then
|
|
|
|
color2=color
|
|
|
|
end
|
|
|
|
if (color3 == nil) then
|
|
|
|
color3=color
|
|
|
|
end
|
|
|
|
if (color4 == nil) then
|
|
|
|
color4=color
|
|
|
|
end
|
2017-01-03 14:38:17 +01:00
|
|
|
if (colorBg == nil) then
|
|
|
|
colorBg=string.char(0,0,0) -- black is the default background color
|
|
|
|
end
|
2017-01-04 21:31:57 +01:00
|
|
|
local hexColor = "#" .. string.format("%02x",string.byte(color,2)) .. string.format("%02x",string.byte(color,1)) .. string.format("%02x",string.byte(color,3))
|
|
|
|
local hexColor1 = "#" .. string.format("%02x",string.byte(color1,2)) .. string.format("%02x",string.byte(color1,1)) .. string.format("%02x",string.byte(color1,3))
|
|
|
|
local hexColor2 = "#" .. string.format("%02x",string.byte(color2,2)) .. string.format("%02x",string.byte(color2,1)) .. string.format("%02x",string.byte(color2,3))
|
|
|
|
local hexColor3 = "#" .. string.format("%02x",string.byte(color3,2)) .. string.format("%02x",string.byte(color3,1)) .. string.format("%02x",string.byte(color3,3))
|
|
|
|
local hexColor4 = "#" .. string.format("%02x",string.byte(color4,2)) .. string.format("%02x",string.byte(color4,1)) .. string.format("%02x",string.byte(color4,3))
|
|
|
|
local hexColorBg = "#" .. string.format("%02x",string.byte(colorBg,2)) .. string.format("%02x",string.byte(colorBg,1)) .. string.format("%02x",string.byte(colorBg,3))
|
2016-12-29 23:20:40 +01:00
|
|
|
|
|
|
|
replaceMap["$SSID"]=ssid
|
|
|
|
replaceMap["$SNTPSERVER"]=sntpserverhostname
|
|
|
|
replaceMap["$TIMEOFFSET"]=timezoneoffset
|
|
|
|
replaceMap["$THREEQUATER"]=(threequater and "checked" or "")
|
|
|
|
replaceMap["$ADDITIONAL_LINE"]=""
|
2017-01-03 14:27:30 +01:00
|
|
|
replaceMap["$HEXCOLORFG"]=hexColor
|
2016-12-29 23:20:40 +01:00
|
|
|
replaceMap["$HEXCOLOR1"]=hexColor1
|
|
|
|
replaceMap["$HEXCOLOR2"]=hexColor2
|
|
|
|
replaceMap["$HEXCOLOR3"]=hexColor3
|
|
|
|
replaceMap["$HEXCOLOR4"]=hexColor4
|
2017-01-03 14:38:17 +01:00
|
|
|
replaceMap["$HEXCOLORBG"]=hexColorBg
|
2016-12-29 23:20:40 +01:00
|
|
|
return replaceMap
|
2016-12-17 02:09:23 +01:00
|
|
|
end
|
|
|
|
|
2016-06-18 19:35:22 +02:00
|
|
|
function startWebServer()
|
|
|
|
srv=net.createServer(net.TCP)
|
|
|
|
srv:listen(80,function(conn)
|
|
|
|
conn:on("receive", function(conn,payload)
|
2019-04-03 20:23:07 +02:00
|
|
|
if (httpSending) then
|
|
|
|
print("HTTP sending... be patient!")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-06-19 16:33:02 +02:00
|
|
|
|
2016-06-18 19:35:22 +02:00
|
|
|
if (payload:find("GET /") ~= nil) then
|
2019-04-17 20:13:25 +02:00
|
|
|
httpSending=true
|
|
|
|
--here is code for handling http request from a web-browser
|
2019-03-23 00:29:18 +01:00
|
|
|
collectgarbage()
|
2019-04-03 21:08:27 +02:00
|
|
|
-- Stop all
|
2019-04-10 21:23:15 +02:00
|
|
|
for i=0,5 do tmr.stop(i) end
|
2019-04-17 20:13:25 +02:00
|
|
|
-- unload all other functions
|
2019-04-17 20:39:54 +02:00
|
|
|
-- grep function *.lua | grep -v webserver | grep -v init.lua | grep -v main.lua | cut -f 2 -d ':' | grep "^function" | sed "s/function //g" | grep -o "^[a-zA-Z0-9\_]*"
|
2019-04-17 20:13:25 +02:00
|
|
|
updateColor = nil
|
|
|
|
drawLEDs = nil
|
|
|
|
round = nil
|
|
|
|
generateLEDs = nil
|
|
|
|
isSummerTime = nil
|
|
|
|
getUTCtime = nil
|
|
|
|
getTime = nil
|
|
|
|
display_timestat = nil
|
|
|
|
display_countcharacters_de = nil
|
|
|
|
display_countwords_de = nil
|
2019-04-16 19:20:01 +02:00
|
|
|
collectgarbage()
|
2019-04-10 21:23:15 +02:00
|
|
|
ws2812.write(string.char(0,0,0):rep(56) .. color:rep(2) .. string.char(0,0,0):rep(4) .. color:rep(2) .. string.char(0,0,0):rep(48))
|
2019-04-03 21:08:27 +02:00
|
|
|
-- Start Time after 1 minute
|
2019-04-17 20:13:25 +02:00
|
|
|
tmr.alarm(5, 60000, 0 ,function()
|
2019-04-17 20:39:54 +02:00
|
|
|
dependModules = { "timecore" , "wordclock", "displayword" }
|
|
|
|
for _,mod in pairs(dependModules) do
|
|
|
|
print("Loading " .. mod)
|
|
|
|
mydofile(mod)
|
|
|
|
end
|
|
|
|
-- Start the time Thread
|
|
|
|
displayTime()
|
|
|
|
-- Start the time Thread
|
2019-04-03 21:08:27 +02:00
|
|
|
tmr.alarm(1, 20000, 1 ,function()
|
2019-04-17 20:39:54 +02:00
|
|
|
displayTime()
|
2019-04-03 21:08:27 +02:00
|
|
|
end)
|
|
|
|
end)
|
2019-04-17 20:13:25 +02:00
|
|
|
-- send response after 100ms
|
2019-04-17 20:39:54 +02:00
|
|
|
tmr.alarm(4, 100, 0 ,function()
|
2019-04-17 20:13:25 +02:00
|
|
|
if (sendPage ~= nil) then
|
|
|
|
print("Sending webpage.html (" .. tostring(node.heap()) .. "B free) ...")
|
|
|
|
-- Load the sendPagewebcontent
|
|
|
|
replaceMap=fillDynamicMap()
|
|
|
|
sendPage(conn, "webpage.html", replaceMap)
|
|
|
|
end
|
|
|
|
end)
|
2016-06-18 19:35:22 +02:00
|
|
|
else if (payload:find("POST /") ~=nil) then
|
2016-12-29 23:20:40 +01:00
|
|
|
--code for handling the POST-request (updating settings)
|
|
|
|
_, postdatastart = payload:find("\r\n\r\n")
|
|
|
|
--Next lines catches POST-requests without POST-data....
|
|
|
|
if postdatastart==nil then postdatastart = 1 end
|
|
|
|
local postRequestData=string.sub(payload,postdatastart+1)
|
|
|
|
local _POST = {}
|
|
|
|
for i, j in string.gmatch(postRequestData, "(%w+)=([^&]+)&*") do
|
|
|
|
_POST[i] = j
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Do the magic!
|
|
|
|
if (_POST.action ~= nil and _POST.action == "Reboot") then
|
|
|
|
node.restart()
|
2017-01-03 13:38:56 +01:00
|
|
|
return
|
2016-12-29 23:20:40 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
if ((_POST.ssid~=nil) and (_POST.sntpserver~=nil) and (_POST.timezoneoffset~=nil)) then
|
|
|
|
print("New config!")
|
|
|
|
if (_POST.password==nil) then
|
|
|
|
_, password, _, _ = wifi.sta.getconfig()
|
|
|
|
print("Restoring password : " .. password)
|
|
|
|
_POST.password = password
|
|
|
|
password = nil
|
|
|
|
end
|
|
|
|
-- Safe configuration:
|
|
|
|
file.remove(configFile .. ".new")
|
|
|
|
sec, _ = rtctime.get()
|
|
|
|
file.open(configFile.. ".new", "w+")
|
2019-04-03 20:23:07 +02:00
|
|
|
file.write("-- Config\n" .. "station_cfg={}\nstation_cfg.ssid=\"" .. _POST.ssid .. "\"\nstation_cfg.pwd=\"" .. _POST.password .. "\"\nstation_cfg.save=false\nwifi.sta.config(station_cfg)\n")
|
|
|
|
file.write("sntpserverhostname=\"" .. _POST.sntpserver .. "\"\n" .. "timezoneoffset=\"" .. _POST.timezoneoffset .. "\"\n".. "inv46=\"" .. tostring(_POST.inv46) .. "\"\n")
|
2018-12-23 21:53:25 +01:00
|
|
|
|
2016-12-29 23:20:40 +01:00
|
|
|
if ( _POST.fcolor ~= nil) then
|
|
|
|
-- color=string.char(_POST.green, _POST.red, _POST.blue)
|
|
|
|
print ("Got fcolor: " .. _POST.fcolor)
|
|
|
|
local hexColor=string.sub(_POST.fcolor, 4)
|
|
|
|
local red = tonumber(string.sub(hexColor, 1, 2), 16)
|
|
|
|
local green = tonumber(string.sub(hexColor, 3, 4), 16)
|
|
|
|
local blue = tonumber(string.sub(hexColor, 5, 6), 16)
|
2017-01-04 21:31:57 +01:00
|
|
|
file.write("color=string.char(" .. green .. "," .. red .. "," .. blue .. ")\n")
|
2017-01-03 14:27:30 +01:00
|
|
|
-- fill the current values
|
2017-01-04 21:31:57 +01:00
|
|
|
color=string.char(green, red, blue)
|
2016-12-29 23:20:40 +01:00
|
|
|
end
|
|
|
|
if ( _POST.colorMin1 ~= nil) then
|
|
|
|
local hexColor=string.sub(_POST.colorMin1, 4)
|
|
|
|
local red = tonumber(string.sub(hexColor, 1, 2), 16)
|
|
|
|
local green = tonumber(string.sub(hexColor, 3, 4), 16)
|
|
|
|
local blue = tonumber(string.sub(hexColor, 5, 6), 16)
|
2017-01-04 21:31:57 +01:00
|
|
|
file.write("color1=string.char(" .. green .. "," .. red .. "," .. blue .. ")\n")
|
2017-01-03 14:27:30 +01:00
|
|
|
-- fill the current values
|
2017-01-04 21:31:57 +01:00
|
|
|
color1=string.char(green, red, blue)
|
2016-12-29 23:20:40 +01:00
|
|
|
end
|
|
|
|
if ( _POST.colorMin2 ~= nil) then
|
|
|
|
local hexColor=string.sub(_POST.colorMin2, 4)
|
|
|
|
local red = tonumber(string.sub(hexColor, 1, 2), 16)
|
|
|
|
local green = tonumber(string.sub(hexColor, 3, 4), 16)
|
|
|
|
local blue = tonumber(string.sub(hexColor, 5, 6), 16)
|
2017-01-04 21:31:57 +01:00
|
|
|
file.write("color2=string.char(" .. green .. "," .. red .. "," .. blue .. ")\n")
|
2017-01-03 14:27:30 +01:00
|
|
|
-- fill the current values
|
2017-01-04 21:31:57 +01:00
|
|
|
color2=string.char(green, red, blue)
|
2016-12-29 23:20:40 +01:00
|
|
|
end
|
|
|
|
if ( _POST.colorMin3 ~= nil) then
|
|
|
|
local hexColor=string.sub(_POST.colorMin3, 4)
|
|
|
|
local red = tonumber(string.sub(hexColor, 1, 2), 16)
|
|
|
|
local green = tonumber(string.sub(hexColor, 3, 4), 16)
|
|
|
|
local blue = tonumber(string.sub(hexColor, 5, 6), 16)
|
2017-01-04 21:31:57 +01:00
|
|
|
file.write("color3=string.char(" .. green .. "," .. red .. "," .. blue .. ")\n")
|
2017-01-03 14:27:30 +01:00
|
|
|
-- fill the current values
|
2017-01-04 21:31:57 +01:00
|
|
|
color3=string.char(green, red, blue)
|
2016-12-29 23:20:40 +01:00
|
|
|
end
|
|
|
|
if ( _POST.colorMin4 ~= nil) then
|
|
|
|
local hexColor=string.sub(_POST.colorMin4, 4)
|
|
|
|
local red = tonumber(string.sub(hexColor, 1, 2), 16)
|
|
|
|
local green = tonumber(string.sub(hexColor, 3, 4), 16)
|
|
|
|
local blue = tonumber(string.sub(hexColor, 5, 6), 16)
|
2017-01-04 21:31:57 +01:00
|
|
|
file.write("color4=string.char(" .. green .. "," .. red .. "," .. blue .. ")\n")
|
2017-01-03 14:27:30 +01:00
|
|
|
-- fill the current values
|
2017-01-04 21:31:57 +01:00
|
|
|
color4=string.char(green, red, blue)
|
2016-12-29 23:20:40 +01:00
|
|
|
end
|
2017-01-03 14:38:17 +01:00
|
|
|
if ( _POST.bcolor ~= nil) then
|
|
|
|
local hexColor=string.sub(_POST.bcolor, 4)
|
|
|
|
local red = tonumber(string.sub(hexColor, 1, 2), 16)
|
|
|
|
local green = tonumber(string.sub(hexColor, 3, 4), 16)
|
|
|
|
local blue = tonumber(string.sub(hexColor, 5, 6), 16)
|
2017-01-04 21:31:57 +01:00
|
|
|
file.write("colorBg=string.char(" .. green .. "," .. red .. "," .. blue .. ")\n")
|
2017-01-03 14:38:17 +01:00
|
|
|
-- fill the current values
|
2017-01-04 21:31:57 +01:00
|
|
|
colorBg=string.char(green, red, blue)
|
2017-01-03 14:38:17 +01:00
|
|
|
end
|
2017-01-03 13:38:56 +01:00
|
|
|
if (getTime ~= nil) then
|
|
|
|
time = getTime(sec, timezoneoffset)
|
|
|
|
file.write("print(\"Config from " .. time.year .. "-" .. time.month .. "-" .. time.day .. " " .. time.hour .. ":" .. time.minute .. ":" .. time.second .. "\")\n")
|
|
|
|
end
|
2016-12-29 23:20:40 +01:00
|
|
|
if (_POST.threequater ~= nil) then
|
|
|
|
file.write("threequater=true\n")
|
2017-01-03 14:27:30 +01:00
|
|
|
-- fill the current values
|
|
|
|
threequater=true
|
2016-12-29 23:20:40 +01:00
|
|
|
else
|
|
|
|
file.write("threequater=nil\n") -- unset threequater
|
2017-01-03 14:27:30 +01:00
|
|
|
-- fill the current values
|
|
|
|
threequater=nil
|
2016-12-29 23:20:40 +01:00
|
|
|
end
|
|
|
|
file.close()
|
2016-12-30 14:23:34 +01:00
|
|
|
collectgarbage()
|
2016-12-29 23:20:40 +01:00
|
|
|
sec=nil
|
|
|
|
file.remove(configFile)
|
|
|
|
print("Rename config")
|
|
|
|
if (file.rename(configFile .. ".new", configFile)) then
|
|
|
|
print("Successfully")
|
2017-01-03 14:27:30 +01:00
|
|
|
tmr.alarm(3, 20, 0 ,function()
|
2016-12-30 15:41:19 +01:00
|
|
|
replaceMap=fillDynamicMap()
|
|
|
|
replaceMap["$ADDITIONAL_LINE"]="<h2><font color=\"green\">New configuration saved</font></h2>"
|
|
|
|
print("Send success to client")
|
|
|
|
sendPage(conn, "webpage.html", replaceMap)
|
|
|
|
end)
|
2016-12-29 23:20:40 +01:00
|
|
|
else
|
2017-01-03 14:27:30 +01:00
|
|
|
tmr.alarm(3, 20, 0 ,function()
|
2016-12-30 15:41:19 +01:00
|
|
|
replaceMap=fillDynamicMap()
|
|
|
|
replaceMap["$ADDITIONAL_LINE"]="<h2><font color=\"red\">ERROR</font></h2>"
|
|
|
|
sendPage(conn, "webpage.html", replaceMap)
|
|
|
|
end)
|
2016-12-29 23:20:40 +01:00
|
|
|
end
|
|
|
|
else
|
|
|
|
replaceMap=fillDynamicMap()
|
|
|
|
replaceMap["$ADDITIONAL_LINE"]="<h2><font color=\"orange\">Not all parameters set</font></h2>"
|
|
|
|
sendPage(conn, "webpage.html", replaceMap)
|
|
|
|
end
|
2016-06-18 19:35:22 +02:00
|
|
|
else
|
2016-06-19 17:48:39 +02:00
|
|
|
print("Hello via telnet")
|
2016-06-18 19:35:22 +02:00
|
|
|
--here is code, if the connection is not from a webbrowser, i.e. telnet or nc
|
|
|
|
global_c=conn
|
|
|
|
function s_output(str)
|
|
|
|
if(global_c~=nil)
|
|
|
|
then global_c:send(str)
|
|
|
|
end
|
2016-12-30 15:41:19 +01:00
|
|
|
end
|
2016-06-18 19:35:22 +02:00
|
|
|
node.output(s_output, 0)
|
|
|
|
global_c:on("receive",function(c,l)
|
|
|
|
node.input(l)
|
|
|
|
end)
|
|
|
|
global_c:on("disconnection",function(c)
|
|
|
|
node.output(nil)
|
|
|
|
global_c=nil
|
|
|
|
end)
|
2016-06-18 23:19:39 +02:00
|
|
|
print("Welcome to Word Clock")
|
2016-06-18 19:35:22 +02:00
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
conn:on("disconnection", function(c)
|
2016-12-30 15:41:19 +01:00
|
|
|
print("Goodbye")
|
2016-06-18 19:35:22 +02:00
|
|
|
node.output(nil) -- un-register the redirect output function, output goes to serial
|
2019-04-03 21:08:27 +02:00
|
|
|
collectgarbage()
|
2016-12-29 21:27:27 +01:00
|
|
|
--reset amount of sent bytes, as we reached the end
|
|
|
|
sentBytes=0
|
2016-06-18 19:35:22 +02:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
end
|
2019-04-03 20:23:07 +02:00
|
|
|
--FileView done.
|