2016-06-18 19:35:22 +02:00
|
|
|
-- Main Module
|
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()
|
|
|
|
--ptbtime1.ptb.de
|
|
|
|
sntp.sync(sntpserverhostname,
|
|
|
|
function(sec,usec,server)
|
|
|
|
print('sync', sec, usec, server)
|
2016-07-03 18:41:32 +02:00
|
|
|
displayTime()
|
2016-06-19 00:08:21 +02:00
|
|
|
end,
|
|
|
|
function()
|
|
|
|
print('failed!')
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2016-06-19 18:14:28 +02:00
|
|
|
function displayTime()
|
2016-12-30 14:16:19 +01:00
|
|
|
sec, usec = rtctime.get()
|
|
|
|
-- Handle lazy programmer:
|
|
|
|
if (timezoneoffset == nil) then
|
|
|
|
timezoneoffset=0
|
|
|
|
end
|
2016-06-19 18:14:28 +02:00
|
|
|
time = getTime(sec, timezoneoffset)
|
|
|
|
print("Local time : " .. time.year .. "-" .. time.month .. "-" .. time.day .. " " .. time.hour .. ":" .. time.minute .. ":" .. time.second)
|
|
|
|
words = display_timestat(time.hour, time.minute)
|
2016-12-14 19:36:59 +01:00
|
|
|
|
|
|
|
if ((words.min1 == 1) and (color1 ~= nil)) then
|
|
|
|
color=color1
|
|
|
|
elseif ((words.min2 == 1) and (color2 ~= nil)) then
|
|
|
|
color=color2
|
|
|
|
elseif ((words.min3 == 1) and (color3 ~= nil)) then
|
|
|
|
color=color1
|
|
|
|
elseif ((words.min4 == 1) and (color4 ~= nil)) then
|
|
|
|
color=color4
|
|
|
|
end
|
|
|
|
|
2016-12-14 20:15:25 +01:00
|
|
|
ledBuf = generateLEDs(words, color, colorMode)
|
|
|
|
|
2016-06-19 18:14:28 +02:00
|
|
|
-- Write the buffer to the LEDs
|
2016-07-03 18:41:32 +02:00
|
|
|
ws2812.write(ledBuf)
|
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
|
|
|
|
ledBuf=nil
|
|
|
|
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
|
2016-06-19 00:30:09 +02:00
|
|
|
|
|
|
|
connect_counter=0
|
|
|
|
-- Wait to be connect to the WiFi access point.
|
2016-06-19 17:48:39 +02:00
|
|
|
tmr.alarm(0, 1000, 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...")
|
2016-06-19 00:30:09 +02:00
|
|
|
if (connect_counter % 2 == 0) then
|
2016-07-03 18:41:32 +02:00
|
|
|
ws2812.write(string.char((connect_counter % 6)*20,(connect_counter % 5)*20,(connect_counter % 3)*20):rep(114))
|
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")
|
|
|
|
local dependModules = { "timecore" , "wordclock", "displayword" }
|
|
|
|
for _,mod in pairs(dependModules) do
|
|
|
|
print("Loading " .. mod)
|
|
|
|
mydofile(mod)
|
|
|
|
end
|
2016-06-19 00:30:09 +02:00
|
|
|
|
|
|
|
tmr.alarm(2, 500, 0 ,function()
|
|
|
|
syncTimeFromInternet()
|
|
|
|
end)
|
|
|
|
tmr.alarm(3, 2000, 0 ,function()
|
2016-12-30 14:16:19 +01:00
|
|
|
print("Start webserver...")
|
2016-06-19 00:30:09 +02:00
|
|
|
mydofile("webserver")
|
|
|
|
startWebServer()
|
|
|
|
end)
|
2016-06-19 17:48:39 +02:00
|
|
|
|
2016-06-19 18:14:28 +02:00
|
|
|
displayTime()
|
2016-06-19 17:48:39 +02:00
|
|
|
-- Start the time Thread
|
|
|
|
tmr.alarm(1, 20000, 1 ,function()
|
2016-06-19 18:14:28 +02:00
|
|
|
displayTime()
|
2016-06-19 17:48:39 +02:00
|
|
|
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
|
|
|
|
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)
|
|
|
|
dofile("config.lua")
|
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
|