2016-06-18 19:35:22 +02:00
|
|
|
--TODO:
|
|
|
|
|
|
|
|
configFile="config.lua"
|
|
|
|
|
|
|
|
function startWebServer()
|
|
|
|
srv=net.createServer(net.TCP)
|
|
|
|
srv:listen(80,function(conn)
|
|
|
|
conn:on("receive", function(conn,payload)
|
2016-06-19 16:33:02 +02:00
|
|
|
|
2016-06-18 19:35:22 +02:00
|
|
|
if (payload:find("GET /") ~= nil) then
|
|
|
|
--here is code for handling http request from a web-browser
|
2016-06-19 16:33:02 +02:00
|
|
|
|
2016-06-19 17:48:39 +02:00
|
|
|
if (sendWebPage == nil) then
|
|
|
|
print("Loading webpage ...")
|
|
|
|
-- Load the webcontent
|
|
|
|
mydofile("webpage")
|
|
|
|
end
|
2016-06-19 16:33:02 +02:00
|
|
|
|
2016-06-18 19:35:22 +02:00
|
|
|
ssid, password, bssid_set, bssid = wifi.sta.getconfig()
|
|
|
|
sendWebPage(conn,1)
|
2016-06-19 16:33:02 +02:00
|
|
|
conn:on("sent", function(conn)
|
|
|
|
conn:close()
|
|
|
|
-- Clear the webpage generation
|
|
|
|
sendWebPage=nil
|
|
|
|
print("Clean webpage from RAM")
|
2016-06-19 17:48:39 +02:00
|
|
|
collectgarbage()
|
2016-06-19 16:33:02 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
|
2016-06-18 19:35:22 +02:00
|
|
|
else if (payload:find("POST /") ~=nil) then
|
|
|
|
--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
|
2016-06-19 16:33:02 +02:00
|
|
|
local postRequestData=string.sub(payload,postdatastart+1)
|
2016-06-18 19:35:22 +02:00
|
|
|
local _POST = {}
|
|
|
|
for i, j in string.gmatch(postRequestData, "(%w+)=([^&]+)&*") do
|
|
|
|
_POST[i] = j
|
|
|
|
end
|
|
|
|
postRequestData=nil
|
2016-06-18 23:19:39 +02:00
|
|
|
|
2016-06-19 18:09:33 +02:00
|
|
|
--- Do the magic!
|
|
|
|
if (_POST.action ~= nil and _POST.action == "Reboot") then
|
|
|
|
node.restart()
|
|
|
|
end
|
2016-06-19 16:33:02 +02:00
|
|
|
|
|
|
|
print("Inform user via Web")
|
|
|
|
if (sendWebPage == nil) then
|
|
|
|
print("Loading webpage ...")
|
|
|
|
-- Load the webcontent
|
|
|
|
mydofile("webpage")
|
|
|
|
end
|
|
|
|
|
|
|
|
conn:on("sent", function(conn)
|
|
|
|
conn:close()
|
|
|
|
-- Clear the webpage generation
|
|
|
|
sendWebPage=nil
|
|
|
|
print("Clean webpage from RAM")
|
|
|
|
end)
|
|
|
|
|
2016-06-19 22:48:44 +02:00
|
|
|
if ((_POST.ssid~=nil) and (_POST.sntpserver~=nil) and (_POST.timezoneoffset~=nil)) then
|
2016-06-19 16:33:02 +02:00
|
|
|
print("New config!")
|
2016-06-19 22:48:44 +02:00
|
|
|
if (_POST.password==nil) then
|
|
|
|
_, password, _, _ = wifi.sta.getconfig()
|
|
|
|
print("Restoring password : " .. password)
|
|
|
|
_POST.password = password
|
|
|
|
password = nil
|
|
|
|
end
|
2016-06-18 19:35:22 +02:00
|
|
|
-- Safe configuration:
|
2016-06-18 23:19:39 +02:00
|
|
|
file.remove(configFile .. ".new")
|
2016-06-19 16:33:02 +02:00
|
|
|
sec, _ = rtctime.get()
|
|
|
|
file.open(configFile.. ".new", "w+")
|
2016-06-18 23:19:39 +02:00
|
|
|
file.write("-- Config\n" .. "wifi.sta.config(\"" .. _POST.ssid .. "\",\"" .. _POST.password .. "\")\n" .. "sntpserverhostname=\"" .. _POST.sntpserver .. "\"\n" .. "timezoneoffset=\"" .. _POST.timezoneoffset .. "\"\n")
|
2016-12-14 18:37:56 +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)
|
2016-12-14 19:12:14 +01:00
|
|
|
color=string.char(green, red, blue)
|
2016-06-19 22:48:44 +02:00
|
|
|
end
|
2016-12-14 19:12:14 +01:00
|
|
|
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)
|
|
|
|
file.write("color1=string.char(" .. green .. "," .. red .. "," .. blue .. ")\n")
|
|
|
|
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)
|
|
|
|
file.write("color2=string.char(" .. green .. "," .. red .. "," .. blue .. ")\n")
|
|
|
|
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)
|
|
|
|
file.write("color3=string.char(" .. green .. "," .. red .. "," .. blue .. ")\n")
|
|
|
|
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)
|
|
|
|
file.write("color4=string.char(" .. green .. "," .. red .. "," .. blue .. ")\n")
|
|
|
|
end
|
|
|
|
time = getTime(sec, timezoneoffset)
|
|
|
|
file.write("color=string.char(" .. string.byte(color,1) .. "," .. string.byte(color, 2) .. "," .. string.byte(color, 3) .. ")\n")
|
|
|
|
file.write("print(\"Config from " .. time.year .. "-" .. time.month .. "-" .. time.day .. " " .. time.hour .. ":" .. time.minute .. ":" .. time.second .. "\")\n")
|
2016-06-19 23:09:43 +02:00
|
|
|
if (_POST.threequater ~= nil) then
|
|
|
|
file.write("threequater=true\n")
|
|
|
|
else
|
|
|
|
file.write("threequater=nil\n") -- unset threequater
|
|
|
|
end
|
2016-06-18 19:35:22 +02:00
|
|
|
file.close()
|
2016-06-19 16:33:02 +02:00
|
|
|
sec=nil
|
2016-06-18 23:19:39 +02:00
|
|
|
file.remove(configFile)
|
2016-06-19 16:33:02 +02:00
|
|
|
print("Rename config")
|
2016-06-18 23:19:39 +02:00
|
|
|
if (file.rename(configFile .. ".new", configFile)) then
|
2016-06-19 16:33:02 +02:00
|
|
|
print("Successfully")
|
2016-06-19 00:50:18 +02:00
|
|
|
dofile(configFile) -- load the new values
|
2016-06-18 23:19:39 +02:00
|
|
|
sendWebPage(conn,2) -- success
|
|
|
|
else
|
2016-06-19 16:33:02 +02:00
|
|
|
print("Error")
|
2016-06-18 23:19:39 +02:00
|
|
|
sendWebPage(conn,3) -- error
|
|
|
|
end
|
2016-06-19 16:33:02 +02:00
|
|
|
|
2016-06-18 19:35:22 +02:00
|
|
|
else
|
|
|
|
ssid, password, bssid_set, bssid = wifi.sta.getconfig()
|
2016-06-18 23:19:39 +02:00
|
|
|
sendWebPage(conn,4) -- not all parameter set
|
2016-06-18 19:35:22 +02:00
|
|
|
end
|
2016-06-19 16:33:02 +02:00
|
|
|
|
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
|
|
|
|
end
|
|
|
|
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)
|
|
|
|
node.output(nil) -- un-register the redirect output function, output goes to serial
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
end
|