So so so! Clean code

This commit is contained in:
ollo 2016-06-18 23:19:39 +02:00
parent fc8ddab368
commit 4a97dbe0ca
2 changed files with 51 additions and 22 deletions

View File

@ -19,7 +19,6 @@ dofile("timecore.lua")
dofile("wordclock.lua") dofile("wordclock.lua")
dofile("displayword.lua") dofile("displayword.lua")
timezoneoffset=1
ledPin=4 ledPin=4
-- Color is defined as GREEN, RED, BLUE -- Color is defined as GREEN, RED, BLUE
color=string.char(0,0,250) color=string.char(0,0,250)
@ -81,5 +80,10 @@ tmr.alarm(1, 15000, 1 ,function()
end end
end end
end end
-- cleanup
ledBuf=nil
words=nil
time=nil
collectgarbage()
end) end)

View File

@ -4,6 +4,7 @@ configFile="config.lua"
function sendWebPage(conn,answertype) function sendWebPage(conn,answertype)
collectgarbage()
if (ssid == nil) then if (ssid == nil) then
ssid="Not set" ssid="Not set"
end end
@ -14,20 +15,42 @@ function sendWebPage(conn,answertype)
timezoneoffset=1 timezoneoffset=1
end end
buf="HTTP/1.1 200 OK\nServer: NodeMCU\nContent-Type: text/html\n\n" buf="HTTP/1.1 200 OK\nServer: NodeMCU\nContent-Type: text/html\n\n"
buf = buf .. "<html><body>\n" if (node.heap() < 10000) then
buf = buf .. "<h1>Welcome to the WordClock</h1>" buf = buf .. "<h1>Busy, please come later again</h1>"
buf = buf.. "<h2>Configuration</h2><form action=\"\" method=\"POST\">" else
buf = buf.. "<label for=\"ssid\">WIFI-SSID: <input id=\"ssid\" name=\"ssid\" value=\"" .. ssid .. "\"></label><br/>" buf = buf .. "<html>"
buf = buf.. "<label for=\"password\">Password: <input id=\"password\" name=\"password\"></label><br/>" buf = buf .. "<head><title>WordClock Setup Page</title>"
buf = buf.. "<label for=\"sntpserver\">SNTP Server: <input id=\"sntpserver\" name=\"sntpserver\" value=\"" .. sntpserverhostname .. "\"></label><br/>" if (bennyHack ~= nil) then
buf = buf.. "<label for=\"timezoneoffset\">Offset from UTC: <input id=\"timezoneoffset\" name=\"timezoneoffset\" value=\"" .. timezoneoffset .. "\"></label><br/>" buf = buf .. "<style type=\"text/css\"> #table-6 { width: 100% border: 1px solid #B0B0B0; } "
buf = buf.. "<input type=\"submit\" value=\"Configure Clock\"></form>" buf = buf .. "#table-6 tbody { margin: 0; padding: 0; border: 0; outline: 0; font-size: 100%; vertical-align: baseline; background: transparent; } #table-6 thead { text-align: left; } "
if answertype>1 then buf = buf .. "#table-6 thead th { background: -moz-linear-gradient(top, #F0F0F0 0, #DBDBDB 100%);"
buf = buf .. "<h2>New configuration saved</h2\n>" buf = buf .. " background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #F0F0F0), color-stop(100%, #DBDBDB)); "
buf = buf .. "filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#F0F0F0', endColorstr='#DBDBDB', GradientType=0); border: 1px solid #B0B0B0; color: #444; font-size: 16px; font-weight: bold; padding: 3px 10px; } "
buf = buf .. "#table-6 td { padding: 3px 10px; }"
buf = buf .. " #table-6 tr:nth-child(even) { background: #F2F2F2; } </style>"
end
buf = buf .. "</head><body>\n"
buf = buf .. "<h1>Welcome to the WordClock</h1>Please note that all settings are mandatory<br /><br />"
buf = buf .."<form action=\"\" method=\"POST\">"
buf = buf .."<table id=\"table-6\">"
buf = buf .."<tr><th>WIFI-SSID</b></th><td><input id=\"ssid\" name=\"ssid\" value=\"" .. ssid .. "\"></td><td>SSID of the wireless network</td></tr>"
buf = buf .."<tr><th>WIFI-Password</th><td><input id=\"password\" name=\"password\"></td><td>Password of the wireless network</td></tr>"
buf = buf .."<tr><th>SNTP Server</th><td><input id=\"sntpserver\" name=\"sntpserver\" value=\"" .. sntpserverhostname .. "\"></td><td>Server to sync the time with. Only one ntp server is allowed.</tr>"
buf = buf .."<tr><th>Offset to UTC time</th><td><input id=\"timezoneoffset\" name=\"timezoneoffset\" value=\"" .. timezoneoffset .. "\"></td><td>Define the offset to UTC time in hours. For example +1 hour for Germany</tr>"
buf = buf .. "<tr><td colspan=\"3\"><div align=\"center\"><input type=\"submit\" value=\"Save Configuration\" onclick=\"this.value='Submitting ..';this.disabled='disabled'; this.form.submit();\"></div></td></tr>"
buf = buf .."</table></form>"
if answertype==2 then
buf = buf .. "<h2><font color=\"green\">New configuration saved</font></h2\n>"
elseif answertype==3 then
buf = buf .. "<h2><font color=\"red\">ERROR</font></h2\n>"
elseif answertype==4 then
buf = buf .. "<h2><font color=\"orange\">Not all parameters set</font></h2\n>"
end end
buf = buf .. "\n</body></html>" buf = buf .. "\n</body></html>"
end
conn:send(buf) conn:send(buf)
buf=nil buf=nil
collectgarbage()
end end
function startWebServer() function startWebServer()
@ -51,21 +74,23 @@ function startWebServer()
end end
postRequestData=nil postRequestData=nil
if ((_POST.ssid~=nil) and (_POST.password~=nil) and (_POST.sntpserver~=nil) and (_POST.timezoneoffset~=nil)) then if ((_POST.ssid~=nil) and (_POST.password~=nil) and (_POST.sntpserver~=nil) and (_POST.timezoneoffset~=nil)) then
-- Safe configuration: -- Safe configuration:
file.remove(configFile) file.remove(configFile .. ".new")
file.open(configFile, "w+") file.open(configFile.. ".new", "w")
w = file.writeline file.write("-- Config\n" .. "wifi.sta.config(\"" .. _POST.ssid .. "\",\"" .. _POST.password .. "\")\n" .. "sntpserverhostname=\"" .. _POST.sntpserver .. "\"\n" .. "timezoneoffset=\"" .. _POST.timezoneoffset .. "\"\n")
w("-- Config")
w("wifi.sta.config(\"" .. _POST.ssid .. "\",\"" .. _POST.password .. "\")")
w("sntpserverhostname=\"" .. _POST.sntpserver .. "\"")
w("timezoneoffset=\"" .. _POST.timezoneoffset .. "\"")
file.close() 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 else
ssid, password, bssid_set, bssid = wifi.sta.getconfig() ssid, password, bssid_set, bssid = wifi.sta.getconfig()
sendWebPage(conn,1) sendWebPage(conn,4) -- not all parameter set
conn:on("sent", function(conn) conn:close() end)
end end
conn:on("sent", function(conn) conn:close() end)
else else
--here is code, if the connection is not from a webbrowser, i.e. telnet or nc --here is code, if the connection is not from a webbrowser, i.e. telnet or nc
global_c=conn global_c=conn
@ -82,7 +107,7 @@ function startWebServer()
node.output(nil) node.output(nil)
global_c=nil global_c=nil
end) end)
print("Welcome to WS2812Clock") print("Welcome to Word Clock")
end end
end end