Skip to content

Commit

Permalink
Version 1.6.0 - Added NIST clock module for date and time.
Browse files Browse the repository at this point in the history
- Extended HTML templates with current and start date and time.
- Up-time calculated from current and start date and time.
- Added Date HTTP header.
  • Loading branch information
mrkale committed Jan 24, 2016
1 parent 042a6c5 commit 23d9768
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 20 deletions.
70 changes: 64 additions & 6 deletions init.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
--NodeMCU-WifiDoubleSwitch
--Libor Gabaj
local format = string.format
local floor = math.floor

--Configuration
cfg_init={
version="1.5.0",
version="1.6.0",
nist_tzdelay=3600,
nist_refresh=60*15,
tmpl_page = "tmpl_page.html",
tmpl_err = "tmpl_err.html",
debug=true,
Expand All @@ -16,6 +19,36 @@ cfg_init={
limitSend=1406,
limitString=3072,
uptime=tmr.now(),
startDate="",
currDate="",
httpDate="",
dateFormat="%02d.%02d.%4d %02d:%02d:%02d",
httpFormat="%s, %d %s %d %02d:%02d:%02d GMT",
}

days = {
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
}

months = {
"January",
"Febuary",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
}

--Compilation
Expand All @@ -41,6 +74,7 @@ compileFile=nil
collectgarbage()

--Initialization
require("nistclock")
dofile("config_switch.lc")
dofile("config_pins.lc")
for i, params in ipairs(cfg_pins)
Expand All @@ -66,9 +100,9 @@ wifi.sta.config(cfg_credentials.wifiSSID, cfg_credentials.wifiPASW)
cfg_credentials.wifiSSID,cfg_credentials.wifiPASW,cfg_credentials.ipconfig=nil,nil,nil
collectgarbage()

--Wifi Timer
cfg_init.uptime = math.floor(tmr.now() - cfg_init.uptime)/1000000
tmr.alarm(0, 1000, 1, function()
--Callback function for NISTclock
nistcb = function()
--Connect to wifi
if wifi.sta.getip()
then
if cfg_init.start
Expand All @@ -86,8 +120,32 @@ tmr.alarm(0, 1000, 1, function()
wifi.sta.connect()
end
end
cfg_init.uptime = cfg_init.uptime + 1
end)
--Uptime
local second, minute, hour, weekday, day, month, year = nistclock.getTime()
if second ~= nil
then
cfg_init.currDate = format(cfg_init.dateFormat, day, month, year, hour, minute, second)
second, minute, hour, weekday, day, month, year = nistclock.getTime(0)
cfg_init.httpDate = format(cfg_init.httpFormat, days[weekday]:sub(1,3), day, months[month]:sub(1,3), year, hour, minute, second)
if cfg_init.startDate == ""
then
cfg_init.startDate = cfg_init.currDate
nistclock.correctStartTime(floor((cfg_init.uptime - tmr.now())/1000000))
end
cfg_init.uptime = nistclock.getElapsedSecs()
end
end

--NISTclock timer
nistclock.setup{
timer = 0,
tzdelay = cfg_init.nist_tzdelay,
refresh = cfg_init.nist_refresh,
debug = cfg_init.debug,
tickcb = nistcb,
}
cfg_init.nist_tzdelay, cfg_init.nist_refresh = nil, nil
nistclock.start();

--Server
if srv then srv:close() end
Expand Down
11 changes: 4 additions & 7 deletions processing.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ CREDENTIALS:
Author: Libor Gabaj
GitHub: https://github.com/mrkale/NodeMCU-WifiDoubleSwitch.git
--]]
local floor = math.floor
local format = string.format

--Update input HTML template string by replacing placeholders with current values
local function updateTemplate(templateString)
--Project params
require("s2eta")
templateString=templateString:gsub("\${version}", cfg_init.version)
templateString=templateString:gsub("\${uptime}", s2eta.eta(cfg_init.uptime))
templateString=templateString:gsub("\${startDate}", cfg_init.startDate)
templateString=templateString:gsub("\${currDate}", cfg_init.currDate)
s2eta, package.loaded["s2eta"]=nil,nil
collectgarbage()
--Template constants
Expand Down Expand Up @@ -94,18 +94,15 @@ local function getHttpStatus(code)
end

--Create HTTP headers
local function getHttpHeaders(code, bodyLength, dateString)
local function getHttpHeaders(code, bodyLength)
local header = getHttpStatus(code)
.. "Content-Type: text/html; charset=UTF-8\r\n"
.. "Server: " .. cfg_header_cons.header_server .. "\r\n"
if bodyLength
then
header = header .. "Content-Length: " .. tostring(bodyLength) .. "\r\n"
end
if dateString
then
header = header .. "Date: " .. dateString .. "\r\n"
end
header = header .. "Date: " .. cfg_init.httpDate .. "\r\n"
return header
end

Expand Down
5 changes: 4 additions & 1 deletion templates/tmpl_page_one_en.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
</div>
</div>
<div class="row"><div class="col-sm-4 col-sm-offset-4"><hr></div></div>
<div class="row"><div class="col-sm-4 col-sm-offset-4 text-center">Uptime (v${version}): ${uptime}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-center">Uptime: ${uptime}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-center">Date: ${currDate}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-center">Start: ${startDate}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-center">Version: ${version}</div></div>
</div>
</body>
</html>
5 changes: 4 additions & 1 deletion templates/tmpl_page_one_sk.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
</div>
</div>
<div class="row"><div class="col-sm-4 col-sm-offset-4"><hr></div></div>
<div class="row"><div class="col-sm-4 col-sm-offset-4 text-center">Čas behu (v${version}): ${uptime}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-center">Beží: ${uptime}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-center">Dátum: ${currDate}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-center">Štart: ${startDate}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-center">Verzia: ${version}</div></div>
</div>
</body>
</html>
5 changes: 4 additions & 1 deletion templates/tmpl_page_twofull_en.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@
</div>
</div>
<div class="row"><div class="col-sm-6 col-sm-offset-3"><hr></div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-center">Uptime (v${version}): ${uptime}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-center">Uptime: ${uptime}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-center">Date: ${currDate}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-center">Start: ${startDate}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-center">Version: ${version}</div></div>
</div>
</body>
</html>
5 changes: 4 additions & 1 deletion templates/tmpl_page_twofull_sk.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@
</div>
</div>
<div class="row"><div class="col-sm-6 col-sm-offset-3"><hr></div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-center">Čas behu (v${version}): ${uptime}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-center">Beží: ${uptime}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-center">Dátum: ${currDate}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-center">Štart: ${startDate}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-center">Verzia: ${version}</div></div>
</div>
</body>
</html>
5 changes: 4 additions & 1 deletion templates/tmpl_page_twosimple_en.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
</div>
</div>
<div class="row"><div class="col-sm-4 col-sm-offset-4"><hr></div></div>
<div class="row"><div class="col-sm-4 col-sm-offset-4 text-center">Uptime (v${version}): ${uptime}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-center">Uptime: ${uptime}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-center">Date: ${currDate}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-center">Start: ${startDate}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-center">Version: ${version}</div></div>
</div>
</body>
</html>
5 changes: 4 additions & 1 deletion templates/tmpl_page_twosimple_sk.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
</div>
</div>
<div class="row"><div class="col-sm-4 col-sm-offset-4"><hr></div></div>
<div class="row"><div class="col-sm-4 col-sm-offset-4 text-center">Čas behu (v${version}): ${uptime}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-center">Beží: ${uptime}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-center">Dátum: ${currDate}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-center">Štart: ${startDate}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-center">Verzia: ${version}</div></div>
</div>
</body>
</html>
5 changes: 4 additions & 1 deletion tmpl_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@
</div>
</div>
<div class="row"><div class="col-sm-6 col-sm-offset-3"><hr></div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-center">Uptime (v${version}): ${uptime}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-center">Uptime: ${uptime}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-center">Date: ${currDate}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-center">Start: ${startDate}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-center">Version: ${version}</div></div>
</div>
</body>
</html>

0 comments on commit 23d9768

Please sign in to comment.