Wordclock/main.lua

90 lines
2.1 KiB
Lua
Raw Normal View History

-- Main Module
function startSetupMode()
tmr.stop(0)
dofile("webserver.lua")
wifi.setmode(wifi.SOFTAP)
wifi.ap.config({ssid='clock',pwd='clock'})
print("Waiting in access point >clock< for Clients")
print("Please visit 192.168.4.1")
startWebServer()
end
wifi.setmode(wifi.STATION)
if ( file.list()["config.lua"] ) then
dofile("config.lua")
else
startSetupMode()
end
2016-05-16 13:54:42 +02:00
dofile("timecore.lua")
2016-05-18 22:50:56 +02:00
dofile("wordclock.lua")
2016-06-15 21:24:40 +02:00
dofile("displayword.lua")
2016-05-16 13:54:42 +02:00
2016-06-18 17:07:49 +02:00
ledPin=4
2016-06-18 19:12:31 +02:00
-- Color is defined as GREEN, RED, BLUE
2016-06-18 17:07:49 +02:00
color=string.char(0,0,250)
2016-05-04 23:23:49 +02:00
2016-06-18 17:07:49 +02:00
connect_counter=0
2016-05-04 23:23:49 +02:00
-- Wait to be connect to the WiFi access point.
2016-06-18 17:07:49 +02:00
tmr.alarm(0, 500, 1, function()
connect_counter=connect_counter+1
2016-05-04 23:23:49 +02:00
if wifi.sta.status() ~= 5 then
print("Connecting to AP...")
2016-06-18 17:07:49 +02:00
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
2016-05-04 23:23:49 +02:00
else
tmr.stop(0)
print('IP: ',wifi.sta.getip())
2016-05-04 23:23:49 +02:00
print("Start webserver...")
tmr.alarm(2, 2000, 0 ,function()
dofile("webserver.lua")
startWebServer()
end)
2016-05-04 23:23:49 +02:00
--ptbtime1.ptb.de
sntp.sync(sntpserverhostname,
2016-05-04 23:23:49 +02:00
function(sec,usec,server)
print('sync', sec, usec, server)
end,
function()
print('failed!')
end
)
2016-06-18 17:07:49 +02:00
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()
2016-05-04 23:23:49 +02:00
end
end)
2016-06-18 19:12:31 +02:00
tmr.alarm(1, 15000, 1 ,function()
2016-05-04 23:23:49 +02:00
sec, usec = rtctime.get()
-- Include the timezone
sec = sec + (timezoneoffset * 3600)
2016-05-16 13:54:42 +02:00
time = getTime(sec, timezoneoffset)
print("Local time : " .. time.year .. "-" .. time.month .. "-" .. time.day .. " " .. time.hour .. ":" .. time.minute .. ":" .. time.second)
2016-05-18 22:50:56 +02:00
words = display_timestat(time.hour, time.minute)
2016-06-18 17:07:49 +02:00
ledBuf = generateLEDs(words, color)
-- Write the buffer to the LEDs
2016-06-18 17:07:49 +02:00
ws2812.write(ledPin, ledBuf)
2016-06-18 19:12:31 +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
2016-05-18 22:50:56 +02:00
end
2016-06-18 23:19:39 +02:00
-- cleanup
ledBuf=nil
words=nil
time=nil
collectgarbage()
2016-05-04 23:23:49 +02:00
end)