From 4a97dbe0ca821d84dea5dc1dcce74454a99ec276 Mon Sep 17 00:00:00 2001 From: ollo Date: Sat, 18 Jun 2016 23:19:39 +0200 Subject: [PATCH] So so so! Clean code --- main.lua | 6 ++++- webserver.lua | 67 +++++++++++++++++++++++++++++++++++---------------- 2 files changed, 51 insertions(+), 22 deletions(-) diff --git a/main.lua b/main.lua index cbf8a17..b91a6b3 100644 --- a/main.lua +++ b/main.lua @@ -19,7 +19,6 @@ dofile("timecore.lua") dofile("wordclock.lua") dofile("displayword.lua") -timezoneoffset=1 ledPin=4 -- Color is defined as GREEN, RED, BLUE color=string.char(0,0,250) @@ -81,5 +80,10 @@ tmr.alarm(1, 15000, 1 ,function() end end end + -- cleanup + ledBuf=nil + words=nil + time=nil + collectgarbage() end) diff --git a/webserver.lua b/webserver.lua index 4ee3997..ef241b9 100644 --- a/webserver.lua +++ b/webserver.lua @@ -4,6 +4,7 @@ configFile="config.lua" function sendWebPage(conn,answertype) + collectgarbage() if (ssid == nil) then ssid="Not set" end @@ -14,20 +15,42 @@ function sendWebPage(conn,answertype) timezoneoffset=1 end buf="HTTP/1.1 200 OK\nServer: NodeMCU\nContent-Type: text/html\n\n" - buf = buf .. "\n" - buf = buf .. "

Welcome to the WordClock

" - buf = buf.. "

Configuration

" - buf = buf.. "
" - buf = buf.. "
" - buf = buf.. "
" - buf = buf.. "
" - buf = buf.. "
" - if answertype>1 then - buf = buf .. "

New configuration saved" + if (node.heap() < 10000) then + buf = buf .. "

Busy, please come later again

" + else + buf = buf .. "" + buf = buf .. "WordClock Setup Page" + if (bennyHack ~= nil) then + buf = buf .. "" + end + buf = buf .. "\n" + buf = buf .. "

Welcome to the WordClock

Please note that all settings are mandatory

" + buf = buf .."
" + 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
" + if answertype==2 then + buf = buf .. "

New configuration saved" + elseif answertype==3 then + buf = buf .. "

ERROR" + elseif answertype==4 then + buf = buf .. "

Not all parameters set" end buf = buf .. "\n" + end conn:send(buf) buf=nil + collectgarbage() end function startWebServer() @@ -51,21 +74,23 @@ function startWebServer() end postRequestData=nil if ((_POST.ssid~=nil) and (_POST.password~=nil) and (_POST.sntpserver~=nil) and (_POST.timezoneoffset~=nil)) then + -- Safe configuration: - file.remove(configFile) - file.open(configFile, "w+") - w = file.writeline - w("-- Config") - w("wifi.sta.config(\"" .. _POST.ssid .. "\",\"" .. _POST.password .. "\")") - w("sntpserverhostname=\"" .. _POST.sntpserver .. "\"") - w("timezoneoffset=\"" .. _POST.timezoneoffset .. "\"") + file.remove(configFile .. ".new") + 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.close() - sendWebPage(conn,2) + file.remove(configFile) + if (file.rename(configFile .. ".new", configFile)) then + sendWebPage(conn,2) -- success + else + sendWebPage(conn,3) -- error + end else ssid, password, bssid_set, bssid = wifi.sta.getconfig() - sendWebPage(conn,1) - conn:on("sent", function(conn) conn:close() end) + sendWebPage(conn,4) -- not all parameter set end + conn:on("sent", function(conn) conn:close() end) else --here is code, if the connection is not from a webbrowser, i.e. telnet or nc global_c=conn @@ -82,7 +107,7 @@ function startWebServer() node.output(nil) global_c=nil end) - print("Welcome to WS2812Clock") + print("Welcome to Word Clock") end end