-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathinit.lua
More file actions
162 lines (153 loc) · 3.53 KB
/
Copy pathinit.lua
File metadata and controls
162 lines (153 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
--NodeMCU-WifiDoubleSwitch
--Libor Gabaj
local format = string.format
local floor = math.floor
--Configuration
cfg_init={
version="1.6.2",
nist_tzdelay=3600,
nist_refresh=60*15,
tmpl_page = "tmpl_page.html",
tmpl_err = "tmpl_err.html",
debug=true,
start=true,
tryout=0,
httpPort=80,
limitConn=30,
limitFile=1024,
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
local function compileFile(luaFile)
if file.open(luaFile)
then
file.close()
collectgarbage()
if cfg_init.debug then print("Compiling:", luaFile) end
node.compile(luaFile)
file.remove(luaFile)
collectgarbage()
end
end
for fileName, fileSize in pairs(file.list())
do
if fileName:match("%.lua$")
then
if fileName ~= "init.lua" then compileFile(fileName) end
end
end
compileFile=nil
collectgarbage()
--Initialization
require("nistclock")
dofile("config_switch.lc")
dofile("config_pins.lc")
for i, params in ipairs(cfg_pins)
do
gpio.mode(params.pin, gpio.OUTPUT)
gpio.write(params.pin, params.status)
end
--Wifi
wifi.setmode(wifi.STATION)
if cfg_init.debug
then
print("MAC: ", wifi.sta.getmac())
print("Chip: ", node.chipid())
print("Heap: ", node.heap())
end
dofile("config_creds.lc")
if cfg_credentials.ipconfig
then
wifi.sta.setip(cfg_credentials.ipconfig)
end
wifi.sta.config(cfg_credentials.wifiSSID, cfg_credentials.wifiPASW)
cfg_credentials.wifiSSID,cfg_credentials.wifiPASW,cfg_credentials.ipconfig=nil,nil,nil
collectgarbage()
--Callback function for NISTclock
function nistcb()
--Connect to wifi
if wifi.sta.getip()
then
if cfg_init.start
then
cfg_init.start = nil
if cfg_init.debug then print(wifi.sta.getip()) end
collectgarbage()
end
else
cfg_init.tryout = cfg_init.tryout + 1
if cfg_init.start and cfg_init.debug then print(format("Connecting to AP (%d)", cfg_init.tryout)) end
if cfg_init.tryout % cfg_init.limitConn == 0
then
wifi.sta.disconnect()
wifi.sta.connect()
end
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
srv=net.createServer(net.TCP)
if cfg_init.debug then print("Web server created") end
srv:listen(cfg_init.httpPort,
function(conn)
conn:on("receive",
function(client, request)
dofile("processing.lc")(client, request)
end
)
end
)