2016-06-18 19:35:22 +02:00
|
|
|
-- Main Module
|
2016-06-19 00:30:09 +02:00
|
|
|
|
2019-05-08 21:12:02 +02:00
|
|
|
displayword = {}
|
|
|
|
|
2016-06-19 00:30:09 +02:00
|
|
|
function startSetupMode()
|
|
|
|
tmr.stop(0)
|
|
|
|
tmr.stop(1)
|
|
|
|
-- start the webserver module
|
|
|
|
mydofile("webserver")
|
2016-06-19 00:08:21 +02:00
|
|
|
|
2016-06-18 19:46:23 +02:00
|
|
|
wifi.setmode(wifi.SOFTAP)
|
2016-06-19 00:08:21 +02:00
|
|
|
cfg={}
|
|
|
|
cfg.ssid="wordclock"
|
|
|
|
cfg.pwd="wordclock"
|
|
|
|
wifi.ap.config(cfg)
|
2016-06-19 17:48:39 +02:00
|
|
|
|
|
|
|
-- Write the buffer to the LEDs
|
|
|
|
local color=string.char(0,128,0)
|
|
|
|
local white=string.char(0,0,0)
|
|
|
|
local ledBuf= white:rep(6) .. color .. white:rep(7) .. color:rep(3) .. white:rep(44) .. color:rep(3) .. white:rep(50)
|
2016-07-03 18:41:32 +02:00
|
|
|
ws2812.write(ledBuf)
|
2016-06-19 17:48:39 +02:00
|
|
|
color=nil
|
|
|
|
white=nil
|
|
|
|
ledBuf=nil
|
|
|
|
|
2016-06-19 00:08:21 +02:00
|
|
|
print("Waiting in access point >wordclock< for Clients")
|
2016-06-18 19:46:23 +02:00
|
|
|
print("Please visit 192.168.4.1")
|
|
|
|
startWebServer()
|
2016-06-19 17:48:39 +02:00
|
|
|
collectgarbage()
|
2016-06-18 19:46:23 +02:00
|
|
|
end
|
|
|
|
|
2016-05-04 23:23:49 +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)
|
|
|
|
print('sync', sec, usec, server)
|
2016-07-03 18:41:32 +02:00
|
|
|
displayTime()
|
2020-12-08 21:51:14 +01:00
|
|
|
syncRunning=nil
|
2016-06-19 00:08:21 +02:00
|
|
|
end,
|
|
|
|
function()
|
|
|
|
print('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
|
|
|
|
2019-05-03 21:26:10 +02:00
|
|
|
briPercent = 50
|
2016-06-19 18:14:28 +02:00
|
|
|
function displayTime()
|
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
|
2019-04-27 18:34:57 +02:00
|
|
|
local time = getTime(sec, timezoneoffset)
|
|
|
|
local words = display_timestat(time.hour, time.minute)
|
2019-05-03 22:11:44 +02:00
|
|
|
if ((dim ~= nil) and (dim == "on")) then
|
|
|
|
words.briPercent=briPercent
|
2020-12-08 20:30:43 +01:00
|
|
|
if (words.briPercent ~= nil and words.briPercent < 3) then
|
|
|
|
words.briPercent=3
|
|
|
|
end
|
2019-05-03 22:11:44 +02:00
|
|
|
else
|
|
|
|
words.briPercent=nil
|
|
|
|
end
|
2019-05-08 21:12:02 +02:00
|
|
|
dofile("displayword.lc")
|
|
|
|
if (displayword ~= 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
|
|
|
|
if ((inv46 ~= nil) and (inv46 == "on")) then
|
|
|
|
invertRows=true
|
2020-12-13 16:37:40 +01:00
|
|
|
end
|
|
|
|
local characters = displayword.countChars(words)
|
2021-01-23 19:17:30 +01:00
|
|
|
ledBuf = displayword.generateLEDs(words, colorBg, color, color1, color2, color3, color4, invertRows, characters)
|
2019-05-03 20:19:12 +02:00
|
|
|
end
|
2019-05-08 21:12:02 +02:00
|
|
|
displayword = nil
|
2019-05-03 20:19:12 +02:00
|
|
|
if (ledBuf ~= nil) then
|
2019-05-11 12:28:23 +02:00
|
|
|
ws2812.write(ledBuf)
|
2020-12-13 16:37:40 +01:00
|
|
|
else
|
2019-05-11 12:28:23 +02:00
|
|
|
if ((colorBg ~= nil) and (color ~= nil)) then
|
|
|
|
ws2812.write(colorBg:rep(107) .. color:rep(3))
|
|
|
|
else
|
|
|
|
local space=string.char(0,0,0)
|
|
|
|
-- set FG to fix value:
|
|
|
|
colorFg = string.char(255,0,0)
|
|
|
|
ws2812.write(space:rep(107) .. colorFg:rep(3))
|
|
|
|
end
|
2019-05-03 20:19:12 +02:00
|
|
|
end
|
2016-06-19 18:14:28 +02:00
|
|
|
-- Used for debugging
|
|
|
|
if (clockdebug ~= nil) then
|
|
|
|
for key,value in pairs(words) do
|
|
|
|
if (value > 0) then
|
|
|
|
print(key,value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- cleanup
|
2019-05-03 21:26:10 +02:00
|
|
|
briPercent=words.briPercent
|
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
|
2019-05-22 21:25:33 +02:00
|
|
|
print("Fg Color: " .. tostring(string.byte(color,1)) .. "x" .. tostring(string.byte(color,2)) .. "x" .. tostring(string.byte(color,3)) )
|
2016-06-19 00:30:09 +02:00
|
|
|
|
|
|
|
connect_counter=0
|
|
|
|
-- Wait to be connect to the WiFi access point.
|
2019-05-03 23:18:27 +02:00
|
|
|
tmr.alarm(0, 500, 1, function()
|
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...")
|
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)
|
|
|
|
local space=string.char(0,0,0)
|
2019-05-03 23:18:27 +02:00
|
|
|
local buf=space:rep(6)
|
|
|
|
if ((connect_counter % 5) >= 1) then
|
|
|
|
buf = buf .. wlanColor
|
|
|
|
else
|
|
|
|
buf = buf .. space
|
|
|
|
end
|
|
|
|
buf = buf .. space:rep(4)
|
|
|
|
buf= buf .. space:rep(3)
|
|
|
|
if ((connect_counter % 5) >= 3) then
|
|
|
|
buf = buf .. wlanColor
|
|
|
|
else
|
|
|
|
buf = buf .. space
|
|
|
|
end
|
|
|
|
if ((connect_counter % 5) >= 2) then
|
|
|
|
buf = buf .. wlanColor
|
|
|
|
else
|
|
|
|
buf = buf .. space
|
|
|
|
end
|
|
|
|
if ((connect_counter % 5) >= 0) then
|
|
|
|
buf = buf .. wlanColor
|
|
|
|
else
|
|
|
|
buf = buf .. space
|
|
|
|
end
|
|
|
|
buf = buf .. space:rep(100)
|
2017-03-01 19:53:55 +01:00
|
|
|
ws2812.write(buf)
|
2016-06-19 00:30:09 +02:00
|
|
|
else
|
2016-07-03 18:41:32 +02:00
|
|
|
ws2812.write(string.char(0,0,0):rep(114))
|
2016-06-19 00:30:09 +02:00
|
|
|
end
|
|
|
|
else
|
|
|
|
tmr.stop(0)
|
|
|
|
print('IP: ',wifi.sta.getip())
|
2016-06-19 17:48:39 +02:00
|
|
|
-- Here the WLAN is found, and something is done
|
|
|
|
print("Solving dependencies")
|
2021-01-15 18:02:17 +01:00
|
|
|
local dependModules = { "timecore" , "wordclock", "mqtt" }
|
2016-06-19 17:48:39 +02:00
|
|
|
for _,mod in pairs(dependModules) do
|
|
|
|
print("Loading " .. mod)
|
|
|
|
mydofile(mod)
|
|
|
|
end
|
2020-12-08 21:51:14 +01:00
|
|
|
|
|
|
|
setupCounter=5
|
2020-12-08 20:30:43 +01:00
|
|
|
tmr.alarm(1, 5000, 1 ,function()
|
2020-12-08 21:51:14 +01:00
|
|
|
if (setupCounter > 4) then
|
|
|
|
syncTimeFromInternet()
|
|
|
|
setupCounter=setupCounter-1
|
|
|
|
elseif (setupCounter > 3) then
|
2021-01-15 18:02:17 +01:00
|
|
|
if (startMqttClient ~= nil) then
|
|
|
|
startMqttClient()
|
2020-12-08 21:51:14 +01:00
|
|
|
else
|
2021-01-15 18:02:17 +01:00
|
|
|
print("NO Mqtt found")
|
|
|
|
mydofile("telnet")
|
2020-12-08 21:51:14 +01:00
|
|
|
end
|
|
|
|
setupCounter=setupCounter-1
|
|
|
|
elseif (setupCounter > 2) then
|
2021-01-15 18:02:17 +01:00
|
|
|
if (startTelnetServer ~= nil) then
|
|
|
|
startTelnetServer()
|
2020-12-08 21:51:14 +01:00
|
|
|
else
|
2021-01-15 18:14:39 +01:00
|
|
|
displayTime()
|
2020-12-08 21:51:14 +01:00
|
|
|
end
|
|
|
|
setupCounter=setupCounter-1
|
|
|
|
else
|
|
|
|
displayTime()
|
|
|
|
end
|
|
|
|
collectgarbage()
|
|
|
|
end)
|
2020-12-07 21:12:48 +01:00
|
|
|
|
|
|
|
-- sync the time every 5 minutes
|
2020-12-10 21:11:35 +01:00
|
|
|
tmr.alarm(2, 300003, 1 ,function()
|
2020-12-07 21:12:48 +01:00
|
|
|
syncTimeFromInternet()
|
|
|
|
displayTime()
|
|
|
|
end)
|
2016-06-19 00:30:09 +02:00
|
|
|
|
|
|
|
end
|
|
|
|
-- when no wifi available, open an accesspoint and ask the user
|
2016-06-19 17:48:39 +02:00
|
|
|
if (connect_counter >= 60) then -- 300 is 30 sec in 100ms cycle
|
|
|
|
startSetupMode()
|
2016-06-19 00:30:09 +02:00
|
|
|
end
|
2016-06-18 19:46:23 +02:00
|
|
|
end)
|
|
|
|
|
2016-06-19 00:08:21 +02:00
|
|
|
|
2016-06-19 00:30:09 +02:00
|
|
|
end
|
2016-05-04 23:23:49 +02:00
|
|
|
|
2020-12-08 20:45:28 +01:00
|
|
|
function stopWordclock()
|
|
|
|
for i=0,5,1 do tmr.stop(i) end
|
|
|
|
end
|
|
|
|
|
2016-07-03 18:41:32 +02:00
|
|
|
-------------------main program -----------------------------
|
|
|
|
ws2812.init() -- WS2812 LEDs initialized on GPIO2
|
2016-05-04 23:23:49 +02:00
|
|
|
|
2016-06-19 00:08:21 +02:00
|
|
|
if ( file.open("config.lua") ) then
|
2016-06-19 00:30:09 +02:00
|
|
|
--- Normal operation
|
2016-06-19 00:08:21 +02:00
|
|
|
wifi.setmode(wifi.STATION)
|
2020-12-08 21:51:14 +01:00
|
|
|
mydofile("config")
|
2016-06-19 00:30:09 +02:00
|
|
|
normalOperation()
|
2016-06-19 00:08:21 +02:00
|
|
|
else
|
2016-06-19 00:30:09 +02:00
|
|
|
-- Logic for inital setup
|
2016-06-19 00:08:21 +02:00
|
|
|
startSetupMode()
|
2016-06-19 17:04:30 +02:00
|
|
|
end
|
2019-04-19 22:05:00 +02:00
|
|
|
----------- button ---------
|
|
|
|
gpio.mode(3, gpio.INPUT)
|
|
|
|
btnCounter=0
|
|
|
|
-- Start the time Thread
|
|
|
|
tmr.alarm(4, 500, 1 ,function()
|
|
|
|
if (gpio.read(3) == 0) then
|
|
|
|
tmr.stop(1) -- stop the LED thread
|
|
|
|
print("Button pressed " .. tostring(btnCounter))
|
|
|
|
btnCounter = btnCounter + 5
|
|
|
|
local ledBuf= string.char(128,0,0):rep(btnCounter) .. string.char(0,0,0):rep(110 - btnCounter)
|
|
|
|
ws2812.write(ledBuf)
|
|
|
|
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()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|