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)
|
|
|
|
print("Waiting in access point >wordclock< for Clients")
|
2016-06-18 19:46:23 +02:00
|
|
|
print("Please visit 192.168.4.1")
|
|
|
|
startWebServer()
|
|
|
|
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)
|
|
|
|
end,
|
|
|
|
function()
|
|
|
|
print('failed!')
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2016-06-19 00:30:09 +02:00
|
|
|
function normalOperation()
|
|
|
|
ledPin=4
|
|
|
|
-- Color is defined as GREEN, RED, BLUE
|
|
|
|
color=string.char(0,0,250)
|
|
|
|
|
|
|
|
connect_counter=0
|
|
|
|
-- Wait to be connect to the WiFi access point.
|
|
|
|
tmr.alarm(0, 500, 1, function()
|
|
|
|
connect_counter=connect_counter+1
|
|
|
|
if wifi.sta.status() ~= 5 then
|
|
|
|
print("Connecting to AP...")
|
|
|
|
if (connect_counter % 2 == 0) then
|
|
|
|
ws2812.write(ledPin, string.char(255,0,0):rep(114))
|
|
|
|
else
|
|
|
|
ws2812.write(ledPin, string.char(0,0,0):rep(114))
|
|
|
|
end
|
|
|
|
else
|
|
|
|
tmr.stop(0)
|
|
|
|
print('IP: ',wifi.sta.getip())
|
|
|
|
|
|
|
|
tmr.alarm(2, 500, 0 ,function()
|
|
|
|
syncTimeFromInternet()
|
|
|
|
end)
|
|
|
|
print("Start webserver...")
|
|
|
|
tmr.alarm(3, 2000, 0 ,function()
|
|
|
|
mydofile("webserver")
|
|
|
|
startWebServer()
|
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
-- when no wifi available, open an accesspoint and ask the user
|
|
|
|
if (connect_counter == 300) then -- 300 is 30 sec in 100ms cycle
|
|
|
|
startSetupmode()
|
|
|
|
end
|
2016-06-18 19:46:23 +02:00
|
|
|
end)
|
|
|
|
|
2016-06-19 00:30:09 +02:00
|
|
|
tmr.alarm(1, 15000, 1 ,function()
|
|
|
|
sec, usec = rtctime.get()
|
|
|
|
|
|
|
|
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)
|
|
|
|
ledBuf = generateLEDs(words, color)
|
|
|
|
-- Write the buffer to the LEDs
|
|
|
|
ws2812.write(ledPin, ledBuf)
|
2016-06-19 00:08:21 +02:00
|
|
|
|
2016-06-19 00:30:09 +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)
|
|
|
|
end
|
2016-05-04 23:23:49 +02:00
|
|
|
|
2016-06-18 19:12:31 +02:00
|
|
|
|
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
|
|
|
|
print("Solving dependencies")
|
|
|
|
dependModules = { "timecore" , "wordclock", "displayword" }
|
|
|
|
for _,mod in pairs(dependModules) do
|
|
|
|
print("Loading " .. mod)
|
|
|
|
mydofile(mod)
|
|
|
|
end
|
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
|