2016-06-18 19:35:22 +02:00
|
|
|
-- Main Module
|
2021-02-13 14:17:58 +01:00
|
|
|
mlt = tmr.create() -- Main loop timer
|
2021-02-09 19:58:59 +01:00
|
|
|
rowbgColor= {}
|
2021-03-14 14:13:46 +01:00
|
|
|
-- Buffer of the clock
|
|
|
|
rgbBuffer = ws2812.newBuffer(114, 3)
|
|
|
|
-- 110 Character plus one LED for each minute,
|
|
|
|
-- that cannot be displayed, as the clock as only a resolution of 5 minutes
|
2019-05-08 21:12:02 +02:00
|
|
|
|
2016-06-19 00:08:21 +02:00
|
|
|
function syncTimeFromInternet()
|
2020-12-08 21:51:14 +01:00
|
|
|
if (syncRunning == nil) then
|
|
|
|
syncRunning=true
|
2016-06-19 00:08:21 +02:00
|
|
|
sntp.sync(sntpserverhostname,
|
|
|
|
function(sec,usec,server)
|
2020-12-08 21:51:14 +01:00
|
|
|
syncRunning=nil
|
2016-06-19 00:08:21 +02:00
|
|
|
end,
|
|
|
|
function()
|
2021-02-11 21:23:32 +01:00
|
|
|
print('NTP failed!')
|
2020-12-08 21:51:14 +01:00
|
|
|
syncRunning=nil
|
2016-06-19 00:08:21 +02:00
|
|
|
end
|
|
|
|
)
|
2020-12-08 21:51:14 +01:00
|
|
|
end
|
2016-06-19 00:08:21 +02:00
|
|
|
end
|
2020-12-07 21:26:02 +01:00
|
|
|
|
2016-06-19 18:14:28 +02:00
|
|
|
function displayTime()
|
2021-02-09 20:18:24 +01:00
|
|
|
collectgarbage()
|
2019-04-27 18:34:57 +02:00
|
|
|
local sec, usec = rtctime.get()
|
2016-12-30 14:16:19 +01:00
|
|
|
-- Handle lazy programmer:
|
|
|
|
if (timezoneoffset == nil) then
|
|
|
|
timezoneoffset=0
|
|
|
|
end
|
2022-01-23 18:30:46 +01:00
|
|
|
local tc = require("timecore_diet")
|
2021-02-11 21:23:32 +01:00
|
|
|
if (tc == nil) then
|
|
|
|
return
|
2019-05-03 22:11:44 +02:00
|
|
|
end
|
2021-02-11 21:23:32 +01:00
|
|
|
local time = tc.getTime(sec, timezoneoffset)
|
|
|
|
tc = nil
|
2022-01-23 18:30:46 +01:00
|
|
|
timecore_diet=nil
|
|
|
|
package.loaded["timecore_diet"]=nil
|
|
|
|
|
2021-02-11 21:23:32 +01:00
|
|
|
collectgarbage()
|
2022-01-23 18:30:46 +01:00
|
|
|
local wc = require("wordclock_diet")
|
2021-02-11 21:23:32 +01:00
|
|
|
if (wc ~= nil) then
|
2021-11-01 19:02:12 +01:00
|
|
|
words = wc.timestat(time.hour, time.minute)
|
|
|
|
if ((dim ~= nil) and (dim == "on")) then
|
2021-02-11 21:48:05 +01:00
|
|
|
words.briPer=briPer
|
|
|
|
if (words.briPer ~= nil and words.briPer < 3) then
|
|
|
|
words.briPer=3
|
|
|
|
end
|
2021-11-01 19:02:12 +01:00
|
|
|
else
|
2021-02-11 21:48:05 +01:00
|
|
|
words.briPer=nil
|
2021-11-01 19:02:12 +01:00
|
|
|
end
|
2021-11-01 13:38:22 +01:00
|
|
|
end
|
2021-02-11 21:23:32 +01:00
|
|
|
wc = nil
|
2022-01-23 18:30:46 +01:00
|
|
|
wordclock_diet=nil
|
|
|
|
package.loaded["wordclock_diet"]=nil
|
|
|
|
|
2021-02-11 21:23:32 +01:00
|
|
|
collectgarbage()
|
2021-02-11 21:33:06 +01:00
|
|
|
print("wc: " .. tostring(node.heap()))
|
2022-01-23 18:30:46 +01:00
|
|
|
local dw = require("displayword_diet")
|
2021-02-11 21:23:32 +01:00
|
|
|
if (dw ~= nil) then
|
2019-05-11 12:28:23 +02:00
|
|
|
--if lines 4 to 6 are inverted due to hardware-fuckup, unfuck it here
|
|
|
|
local invertRows=false
|
2021-02-11 22:14:04 +01:00
|
|
|
if ((inv46 ~= nil) and (inv46 == "on")) then
|
2019-05-11 12:28:23 +02:00
|
|
|
invertRows=true
|
2020-12-13 16:37:40 +01:00
|
|
|
end
|
2021-02-11 21:23:32 +01:00
|
|
|
local c = dw.countChars(words)
|
2021-03-19 19:55:15 +01:00
|
|
|
dw.generateLEDs(rgbBuffer, words, colorBg, color, color1, color2, color3, color4, invertRows, c)
|
2019-05-03 20:19:12 +02:00
|
|
|
end
|
2021-02-11 21:23:32 +01:00
|
|
|
dw = nil
|
2022-01-23 18:30:46 +01:00
|
|
|
displayword_diet=nil
|
|
|
|
package.loaded["displayword_diet"]=nil
|
|
|
|
|
2021-02-11 21:33:06 +01:00
|
|
|
collectgarbage()
|
2021-03-21 15:31:05 +01:00
|
|
|
|
2016-06-19 18:14:28 +02:00
|
|
|
-- cleanup
|
2021-03-14 14:13:46 +01:00
|
|
|
i=nil
|
2021-02-10 20:04:22 +01:00
|
|
|
briPer=words.briPer
|
2016-06-19 18:14:28 +02:00
|
|
|
words=nil
|
|
|
|
time=nil
|
|
|
|
collectgarbage()
|
|
|
|
end
|
|
|
|
|
2016-06-19 00:30:09 +02:00
|
|
|
function normalOperation()
|
2016-06-19 22:48:44 +02:00
|
|
|
-- use default color, if nothing is defined
|
|
|
|
if (color == nil) then
|
|
|
|
-- Color is defined as GREEN, RED, BLUE
|
|
|
|
color=string.char(0,0,250)
|
|
|
|
end
|
2021-02-11 21:33:06 +01:00
|
|
|
print("start: " , node.heap())
|
2021-02-11 21:48:05 +01:00
|
|
|
-------------------------------------------------------------
|
|
|
|
-- Define the main loop
|
|
|
|
local setupCounter=5
|
|
|
|
local alive=0
|
2021-04-04 19:17:15 +02:00
|
|
|
mlt:register(1000, tmr.ALARM_AUTO, function (lt)
|
2021-02-11 21:48:05 +01:00
|
|
|
if (setupCounter > 4) then
|
2021-03-21 17:30:50 +01:00
|
|
|
if (colorBg ~= nil) then
|
|
|
|
rgbBuffer:fill(string.byte(colorBg,1), string.byte(colorBg,2), string.byte(colorBg,3)) -- disable all LEDs
|
|
|
|
else
|
|
|
|
rgbBuffer:fill(0,0,0) -- disable all LEDs
|
|
|
|
end
|
2021-02-11 21:48:05 +01:00
|
|
|
syncTimeFromInternet()
|
|
|
|
setupCounter=setupCounter-1
|
|
|
|
alive = 1
|
2021-03-21 15:31:05 +01:00
|
|
|
rgbBuffer:set(19, color) -- N
|
|
|
|
rgbBuffer:set(31, color) -- T
|
|
|
|
if ((inv46 ~= nil) and (inv46 == "on")) then
|
|
|
|
rgbBuffer:set(45, color) -- P
|
|
|
|
else
|
|
|
|
rgbBuffer:set(55, color) -- P
|
|
|
|
end
|
2021-02-11 21:48:05 +01:00
|
|
|
elseif (setupCounter > 3) then
|
2022-12-09 22:32:40 +01:00
|
|
|
if (web == nil) then
|
2021-02-11 21:48:05 +01:00
|
|
|
-- Here the WLAN is found, and something is done
|
|
|
|
mydofile("mqtt")
|
2021-03-21 15:31:05 +01:00
|
|
|
rgbBuffer:fill(0,0,0) -- disable all LEDs
|
2021-02-11 21:48:05 +01:00
|
|
|
if (startMqttClient ~= nil) then
|
2021-03-21 15:31:05 +01:00
|
|
|
if ((inv46 ~= nil) and (inv46 == "on")) then
|
|
|
|
rgbBuffer:set(34, color) -- M
|
|
|
|
else
|
|
|
|
rgbBuffer:set(44, color) -- M
|
|
|
|
end
|
|
|
|
rgbBuffer:set(82, color) -- T
|
|
|
|
startMqttClient()
|
2021-02-11 21:48:05 +01:00
|
|
|
else
|
|
|
|
print("NO Mqtt found")
|
|
|
|
mydofile("telnet")
|
|
|
|
end
|
2022-12-09 22:32:40 +01:00
|
|
|
else
|
2022-12-09 23:22:27 +01:00
|
|
|
print("webserver prepared")
|
2022-12-09 22:32:40 +01:00
|
|
|
end
|
2021-02-11 21:48:05 +01:00
|
|
|
setupCounter=setupCounter-1
|
|
|
|
elseif (setupCounter > 2) then
|
2022-12-14 20:58:53 +01:00
|
|
|
if (web == nil) then
|
2021-02-11 21:48:05 +01:00
|
|
|
if (startTelnetServer ~= nil) then
|
|
|
|
startTelnetServer()
|
|
|
|
else
|
|
|
|
displayTime()
|
|
|
|
end
|
2022-12-14 20:58:53 +01:00
|
|
|
else
|
|
|
|
print("webserver supplant telnet")
|
|
|
|
end
|
2021-02-11 21:48:05 +01:00
|
|
|
setupCounter=setupCounter-1
|
2021-03-14 13:23:14 +01:00
|
|
|
elseif ( (alive % 120) == 0) then
|
2021-12-04 13:04:12 +01:00
|
|
|
-- sync the time every 5 minutes
|
|
|
|
local heapusage = node.heap()
|
|
|
|
if (heapusage > 12000) then
|
|
|
|
syncTimeFromInternet()
|
|
|
|
end
|
|
|
|
heapusage=nil
|
2021-03-21 15:31:05 +01:00
|
|
|
alive = alive + 1
|
2021-02-11 21:48:05 +01:00
|
|
|
else
|
2022-12-09 23:47:01 +01:00
|
|
|
if (colorBg ~= nil) then
|
|
|
|
rgbBuffer:fill(string.byte(colorBg,1), string.byte(colorBg,2), string.byte(colorBg,3)) -- disable all LEDs
|
|
|
|
else
|
|
|
|
rgbBuffer:fill(0,0,0) -- disable all LEDs
|
|
|
|
end
|
2021-02-11 21:48:05 +01:00
|
|
|
displayTime()
|
|
|
|
alive = alive + 1
|
|
|
|
end
|
2021-03-21 15:31:05 +01:00
|
|
|
if (rgbBuffer ~= nil) then
|
2021-03-21 18:00:07 +01:00
|
|
|
-- show Mqtt status
|
|
|
|
if (startMqttClient ~= nil) then
|
|
|
|
if (not connectedMqtt()) then
|
2021-03-21 21:05:27 +01:00
|
|
|
rgbBuffer:set(103, 0, 64,0)
|
2021-04-03 14:03:59 +02:00
|
|
|
-- check every thirty seconds, if reconnecting is necessary
|
|
|
|
if (((tmr.now() / 1000000) % 100) == 30) then
|
|
|
|
print("MQTT reconnecting... ")
|
|
|
|
reConnectMqtt()
|
|
|
|
end
|
2021-03-21 18:00:07 +01:00
|
|
|
end
|
|
|
|
end
|
2021-03-21 15:31:05 +01:00
|
|
|
ws2812.write(rgbBuffer)
|
|
|
|
else
|
|
|
|
-- set FG to fix value: RED
|
|
|
|
local color = string.char(255,0,0)
|
|
|
|
rgbBuffer:fill(0,0,0) -- disable all LEDs
|
|
|
|
for i=108,110, 1 do rgbBuffer:set(i, color) end
|
|
|
|
ws2812.write(rgbBuffer)
|
|
|
|
print("Fallback no time displayed")
|
|
|
|
end
|
2021-12-04 13:04:12 +01:00
|
|
|
collectgarbage()
|
2021-02-11 21:48:05 +01:00
|
|
|
-- Feed the system watchdog.
|
|
|
|
tmr.wdclr()
|
|
|
|
end)
|
|
|
|
|
|
|
|
-------------------------------------------------------------
|
|
|
|
-- Connect to Wifi
|
2021-02-10 19:45:28 +01:00
|
|
|
local connect_counter=0
|
2016-06-19 00:30:09 +02:00
|
|
|
-- Wait to be connect to the WiFi access point.
|
2021-01-31 20:22:32 +01:00
|
|
|
local wifitimer = tmr.create()
|
2021-03-21 18:00:07 +01:00
|
|
|
wifitimer:register(500, tmr.ALARM_AUTO, function (timer)
|
2016-06-19 00:30:09 +02:00
|
|
|
connect_counter=connect_counter+1
|
|
|
|
if wifi.sta.status() ~= 5 then
|
2016-06-19 17:48:39 +02:00
|
|
|
print(connect_counter .. "/60 Connecting to AP...")
|
2021-03-14 16:52:53 +01:00
|
|
|
rgbBuffer:fill(0,0,0) -- clear all LEDs
|
2019-05-03 23:18:27 +02:00
|
|
|
if (connect_counter % 5 ~= 4) then
|
2017-03-01 19:53:55 +01:00
|
|
|
local wlanColor=string.char((connect_counter % 6)*20,(connect_counter % 5)*20,(connect_counter % 3)*20)
|
2019-05-03 23:18:27 +02:00
|
|
|
if ((connect_counter % 5) >= 1) then
|
2021-03-14 16:52:53 +01:00
|
|
|
rgbBuffer:set(7, wlanColor)
|
2019-05-03 23:18:27 +02:00
|
|
|
end
|
|
|
|
if ((connect_counter % 5) >= 3) then
|
2021-03-14 16:52:53 +01:00
|
|
|
rgbBuffer:set(15, wlanColor)
|
2019-05-03 23:18:27 +02:00
|
|
|
end
|
|
|
|
if ((connect_counter % 5) >= 2) then
|
2021-03-14 16:52:53 +01:00
|
|
|
rgbBuffer:set(16, wlanColor)
|
2019-05-03 23:18:27 +02:00
|
|
|
end
|
|
|
|
if ((connect_counter % 5) >= 0) then
|
2021-03-14 16:52:53 +01:00
|
|
|
rgbBuffer:set(17, wlanColor)
|
2019-05-03 23:18:27 +02:00
|
|
|
end
|
2016-06-19 00:30:09 +02:00
|
|
|
end
|
2021-03-14 14:13:46 +01:00
|
|
|
ws2812.write(rgbBuffer)
|
2016-06-19 00:30:09 +02:00
|
|
|
else
|
2021-03-18 21:38:10 +01:00
|
|
|
wifitimer:unregister()
|
2021-02-11 21:48:05 +01:00
|
|
|
wifitimer=nil
|
|
|
|
connect_counter=nil
|
2021-02-11 21:33:06 +01:00
|
|
|
print('IP: ',wifi.sta.getip(), " heap: ", node.heap())
|
2021-03-19 19:55:15 +01:00
|
|
|
rgbBuffer:fill(0,0,0) -- clear all LEDs
|
2021-03-20 15:49:54 +01:00
|
|
|
rgbBuffer:set(13, color) -- I
|
|
|
|
if ((inv46 ~= nil) and (inv46 == "on")) then
|
|
|
|
rgbBuffer:set(45, color) -- P
|
|
|
|
else
|
|
|
|
rgbBuffer:set(55, color) -- P
|
|
|
|
end
|
2021-03-19 19:55:15 +01:00
|
|
|
ws2812.write(rgbBuffer)
|
2021-02-13 14:17:58 +01:00
|
|
|
mlt:start()
|
2016-06-19 00:30:09 +02:00
|
|
|
end
|
2016-06-18 19:46:23 +02:00
|
|
|
end)
|
2021-01-31 20:22:32 +01:00
|
|
|
wifitimer:start()
|
2016-06-18 19:46:23 +02:00
|
|
|
|
2020-12-08 20:45:28 +01:00
|
|
|
end
|
|
|
|
|
2016-07-03 18:41:32 +02:00
|
|
|
-------------------main program -----------------------------
|
2021-02-10 20:04:22 +01:00
|
|
|
briPer = 50 -- Default brightness is set to 50%
|
2016-07-03 18:41:32 +02:00
|
|
|
ws2812.init() -- WS2812 LEDs initialized on GPIO2
|
2016-05-04 23:23:49 +02:00
|
|
|
|
2019-04-19 22:05:00 +02:00
|
|
|
----------- button ---------
|
|
|
|
gpio.mode(3, gpio.INPUT)
|
2021-01-31 20:22:32 +01:00
|
|
|
local btnCounter=0
|
|
|
|
-- Start the time Thread handling the button
|
|
|
|
local btntimer = tmr.create()
|
2022-12-09 21:48:43 +01:00
|
|
|
btntimer:register(500, tmr.ALARM_AUTO, function (t)
|
2019-04-19 22:05:00 +02:00
|
|
|
if (gpio.read(3) == 0) then
|
2021-02-13 14:17:58 +01:00
|
|
|
mlt:unregister()
|
2019-04-19 22:05:00 +02:00
|
|
|
print("Button pressed " .. tostring(btnCounter))
|
|
|
|
btnCounter = btnCounter + 5
|
2022-12-09 23:22:27 +01:00
|
|
|
|
|
|
|
if ((web ~= nil) and (btnCounter < 50)) then
|
|
|
|
for i=1,btnCounter do rgbBuffer:set(i, 128, 0, 0) end
|
|
|
|
else
|
|
|
|
for i=1,btnCounter do rgbBuffer:set(i, 0, 128, 0) end
|
|
|
|
end
|
2021-03-14 14:13:46 +01:00
|
|
|
ws2812.write(rgbBuffer)
|
2019-04-19 22:05:00 +02:00
|
|
|
if (btnCounter >= 110) then
|
|
|
|
file.remove("config.lua")
|
2020-12-08 21:51:14 +01:00
|
|
|
file.remove("config.lc")
|
2019-04-19 22:05:00 +02:00
|
|
|
node.restart()
|
2022-12-09 23:22:27 +01:00
|
|
|
elseif (btnCounter == 10) then
|
|
|
|
-- start the webserver module
|
|
|
|
mydofile("webserver")
|
2019-04-19 22:05:00 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
2021-01-31 20:22:32 +01:00
|
|
|
btntimer:start()
|