Skip to content

Commit

Permalink
Applying version 1.6.2. to production line.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkale committed Feb 3, 2016
2 parents 9c80925 + 78b7b4d commit 67d594d
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 38 deletions.
62 changes: 44 additions & 18 deletions README.md
@@ -1,13 +1,16 @@
#NodeMCU-WifiDoubleSwitch<br><small>version 1.5.0</small>
NodeMCU based web server within ESP8266 for switching two pins from the browser through WiFi. The project contains a couple of configuration files, where each of them sets up particular aspect of the project.
#NodeMCU-WifiDoubleSwitch<br><small>version 1.6.x</small>
NodeMCU based web server within ESP8266 for switching two pins from the web browser through WiFi. The project contains a couple of configuration files, where each of them sets up particular aspect of the project (application).

A **simple HTML templating** mechanism is used for creating various versions of HTML pages based on placeholders. HTML pages employ *Twitter Bootstrap 3.3.6.* and periodical refreshing for displaying current status of the system.


<a id="dependency"></a>
##Dependency
- **NodeMCU 1.4+**: Used appropriate mechanizm for sending HTTP responses incompatible with framework version *0.9-*.
- **s2eta**: Module for converting elapsed seconds to time string with *days, hours, minutes, and seconds* components.
- **NodeMCU 1.4+**: Used appropriate mechanism for sending HTTP responses incompatible with framework version *0.9-*.
- **[s2eta](https://github.com/mrkale/NodeMCU-Modules/tree/master/s2eta)**: External module for converting elapsed seconds to a time string with *days, hours, minutes, and seconds* components.
- **[nistclock](https://github.com/mrkale/NodeMCU-Modules/tree/master/nistclock)**: External module for reading network date and time from NIST time servers.

*External modules listed above should be uploaded to the system alongside with scripts in the project.*


<a id="scripts"></a>
Expand All @@ -17,39 +20,46 @@ NodeMCU lua scripts. For better performance and memory efficiency all the progra

<a id="init"></a>
###init.lua
Initial script running at power cycle. It
Initial script running at power cycle (boot) or software restart. It
- automatically compiles all *.lua* files except itself
- creates connection to a Wifi network in station mode
- reconnects to Wifi network after connection lost
- launches configuration tables from configuration files
- measures uptime (from recent restart) of the system
- measures uptime (from recent restart) of the system with module [nistclock](#dependency)
- creates web server running in the ESP8266
- defines file names for used HTML templates
- contains tables with English names of week days months for generating HTTP header *Date*.


The script uses the separate external module nistclock.


###config_creds.lua
The script creates configuration table with credentials for preferred WiFi network as well as for web server basic HTTP authorization.
- The credential configuration table may containg IP configuration table for establishing static IP address.
- Update values according to your situation.


###config_pins.lua
The script creates configuration table with hardware configuration. It simply defines the order of pins in HTML templates, their NodeMCU numbers, and keeps current state of pins.

The state of pins in the script is considered as the initial state after power up the ESP8266 and is predefined for active low relays initially turned off, which are expected to be controlled by the ESP8266. The numbering of pins is suitable for ESP8266-01, which the project is primarily aimed to.

*In spite of just two pins defined in the configuration table, it can be extended by whatever number of other pins with corresponding updates in HTML templates in order to control more output devices.*


###config_switch.lua
The script creates the configuration table for rendering HTML pages with help of Bootstrap classes and the table for HTTP headers. The script alsa defines language independence symbols (characters) for *switching on* (I) and *switching off* (0).
The script creates the configuration table for rendering HTML pages with help of Bootstrap classes and the table for HTTP headers. The script also defines language independence symbols (characters) for *switching on* (I) and *switching off* (0).
This script should be updated only if you wish to change the visual and color appearance of HTML pages and/or *assign different gpio state to pin state*.

Current gpio state of pins is set for low active relays.


###processing.lua
The script processes HTTP requests, executes commands from them, and creates HTTP responses including HTTP headers from HTML templates by substituting placeholders in them with current state of pins and corresponding rendering.
Thanks to reading an HTML template at each HTTP request, the templates can be replaced on the system on the fly, which is substantially useful at tuning the HTML page.
At sending HTML pages the script uses callback function if running in framework major version 1 and on, but uses multiple send commands if running in previous major versions of the framework.
Thanks to reading an HTML template from the file system at each HTTP request, the templates can be replaced on the system on the fly, which is substantially useful at tuning the HTML page or where continues working is essential.

The script uses the separate module s2eta.
The script uses the separate external module s2eta.


[Back to scripts](#scripts)
Expand All @@ -59,18 +69,25 @@ At sending HTML pages the script uses callback function if running in framework
##Templates
HTML templates for the web server running within the ESP8266 module.


###tmpl_page.html
The default name of the regular HTML template for control of pins. Different template variation has to be renamed to this default name. The default name can be changed in the script [init.lua](#init).
The default name of the regular HTML template for control pins. Different template variation has to be renamed to this default name. The default name can be changed in the script [init.lua](#init).

- The template uses Bootstrap 3.3.6 framework imported from CDN.
- The template uses Twitter Bootstrap 3.3.6 framework imported from CDN.
- The template is fully responsive and is designed primarily for small devices.
- The templated does not imports Bootstrap javascripts.
- The placeholders of the template are based on command shell variable substitution (*${varname}*).
- Bellow the bottom line there is the uptime and current version (in braces) displayed.
- The template does not import Bootstrap javascripts.
- The placeholders of the template are based on linux command shell variable substitution (*${varname}*).
- Bellow the bottom line there is displayed this information:
- Uptime in AT format since start time
- Current system time
- Start time of recent system (re)start
- Version of the application


######Layout for medium and large devices
![medium and large devices](screenshot_medium_full.png)


######Layout for small and extra small devices
![small and extra small devices](screenshot_small_full.png)

Expand All @@ -96,28 +113,37 @@ The HTML template for failed authorization to the web server.
This folder contains other HTML template variations and their language mutations.

- HTML templates in languages with diacritical characters should be coded in UTF-8.
- Templates in this folder should be renamed to names aforementioned in chapter [templates](#templates) in order to use them in the application, i.e., variation and language suffixes should be removed from file names.


###tmpl_page_twofull_en.html
English mutation of the primary HTML template for control of two pins individually as well as both of them at once including toggling them.
English mutation of the primary HTML template for control two pins individually as well as both of them at once including toggling them.


###tmpl_page_twofull_sk.html
Slovak mutation of the previous template.


###tmpl_page_twosimple_en.html
English mutation of the simplified HTML template for control of two pins individually only, i.e., **without** control and toggle then at once.
English mutation of the simplified HTML template for control two pins individually only, i.e., **without** control and toggling them at once.


###tmpl_page_twosimple_sk.html
Slovak mutation of the previous template.


###tmpl_page_one_en.html
English mutation of the trivial HTML template for control of just one (the first) pin.
English mutation of the trivial HTML template for control just one (the first) pin.


###tmpl_page_one_sk.html
Slovak mutation of the previous template.


###tmpl_err_en.html
English mutation of the HTML template for failed authorization to the web server.


###tmpl_err_sk.html
Slovak mutation of the previous template.

Expand Down
70 changes: 64 additions & 6 deletions init.lua
@@ -1,10 +1,13 @@
--NodeMCU-WifiDoubleSwitch
--Libor Gabaj
local format = string.format
local floor = math.floor

--Configuration
cfg_init={
version="1.5.1",
version="1.6.2",
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
function nistcb()
--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
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"
.. "Date: " .. cfg_init.httpDate .. "\r\n"
if bodyLength
then
header = header .. "Content-Length: " .. tostring(bodyLength) .. "\r\n"
end
if dateString
then
header = header .. "Date: " .. dateString .. "\r\n"
end
return header
end
Expand Down
Binary file modified screenshot_medium_full.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified screenshot_small_full.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion templates/tmpl_page_one_en.html
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
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-right">Beží: ${uptime}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-right">Dátum: ${currDate}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-right">Štart: ${startDate}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-right">Verzia: ${version}</div></div>
</div>
</body>
</html>
5 changes: 4 additions & 1 deletion templates/tmpl_page_twofull_en.html
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
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-right">Beží: ${uptime}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-right">Dátum: ${currDate}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-right">Štart: ${startDate}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-right">Verzia: ${version}</div></div>
</div>
</body>
</html>
5 changes: 4 additions & 1 deletion templates/tmpl_page_twosimple_en.html
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
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-right">Beží: ${uptime}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-right">Dátum: ${currDate}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-right">Štart: ${startDate}</div></div>
<div class="row"><div class="col-sm-6 col-sm-offset-3 text-right">Verzia: ${version}</div></div>
</div>
</body>
</html>
5 changes: 4 additions & 1 deletion tmpl_page.html
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 67d594d

Please sign in to comment.