From d166fb68df11818d5d8de6d5061b1823c2a6fa98 Mon Sep 17 00:00:00 2001 From: ollo Date: Sun, 19 Jun 2016 22:48:44 +0200 Subject: [PATCH] Color can be set via the WebInterface --- main.lua | 7 +++++-- webpage.lua | 22 +++++++++++++++------- webserver.lua | 13 +++++++++++-- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/main.lua b/main.lua index cadf4b1..76ac6ac 100644 --- a/main.lua +++ b/main.lua @@ -67,8 +67,11 @@ function displayTime() end function normalOperation() - -- Color is defined as GREEN, RED, BLUE - color=string.char(0,0,250) + -- use default color, if nothing is defined + if (color == nil) then + -- Color is defined as GREEN, RED, BLUE + color=string.char(0,0,250) + end connect_counter=0 -- Wait to be connect to the WiFi access point. diff --git a/webpage.lua b/webpage.lua index 1cea9c8..84f54f9 100644 --- a/webpage.lua +++ b/webpage.lua @@ -10,6 +10,11 @@ function sendWebPage(conn,answertype) 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 + local buf="HTTP/1.1 200 OK\nServer: NodeMCU\nContent-Type: text/html\n\n" if (node.heap() < 8000) then buf = buf .. "

Busy, please come later again

" @@ -17,13 +22,16 @@ function sendWebPage(conn,answertype) buf = buf .. "" buf = buf .. "WordClock Setup Page" buf = buf .. "\n" - buf = buf .. "

Welcome to the WordClock

Please note that all settings are mandatory

" + buf = buf .. "

Welcome to the WordClock

" buf = buf .."
" - buf = buf .."" - buf = buf .."" - buf = buf .."" - buf = buf .."" - buf = buf .."" + buf = buf .."
WIFI-SSIDSSID of the wireless network
WIFI-PasswordPassword of the wireless network
SNTP ServerServer to sync the time with. Only one ntp server is allowed.
Offset to UTC timeDefine the offset to UTC time in hours. For example +1 hour for Germany
" + buf = buf .."" + buf = buf .."" + buf = buf .."" + buf = buf .."" + buf = buf .."" + buf = buf .."" + buf = buf .."" buf = buf .. "" buf = buf .. "" buf = buf .."
WIFI-SSID
WIFI-Password
SNTP Serverntp server to sync the time
Offset to UTC timeDefine the offset to UTC time in hours. E.g +1
Red
Green
BlueAll Colors: 0 - 255
" @@ -39,4 +47,4 @@ function sendWebPage(conn,answertype) conn:send(buf) buf=nil collectgarbage() -end \ No newline at end of file +end diff --git a/webserver.lua b/webserver.lua index b6d3bde..a49a678 100644 --- a/webserver.lua +++ b/webserver.lua @@ -58,14 +58,23 @@ function startWebServer() print("Clean webpage from RAM") end) - if ((_POST.ssid~=nil) and (_POST.password~=nil) and (_POST.sntpserver~=nil) and (_POST.timezoneoffset~=nil)) then + 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+") file.write("-- Config\n" .. "wifi.sta.config(\"" .. _POST.ssid .. "\",\"" .. _POST.password .. "\")\n" .. "sntpserverhostname=\"" .. _POST.sntpserver .. "\"\n" .. "timezoneoffset=\"" .. _POST.timezoneoffset .. "\"\n") - file.write("print(\"Config from " .. sec .. "\")\n") + if ( (_POST.red ~= nil) and (_POST.green ~= nil) and (_POST.blue ~= nil) ) then + color=string.char(_POST.red, _POST.green, _POST.blue) + end + file.write("color=string.char(" .. string.byte(color,1) .. "," .. string.byte(color, 2) .. "," .. string.byte(color, 3) .. ")\n print(\"Config from " .. sec .. "\")\n") file.close() sec=nil file.remove(configFile)