Added time synchronization again

This commit is contained in:
ollo 2016-06-19 00:30:09 +02:00
parent 6874d1545b
commit 7140717696
2 changed files with 81 additions and 72 deletions

151
main.lua
View File

@ -1,14 +1,18 @@
-- Main Module -- Main Module
function startSetupMode()
tmr.stop(0) function mydofile(mod)
tmr.stop(1)
-- start the webserver module
mod="webserver"
if (file.open(mod .. ".lua")) then if (file.open(mod .. ".lua")) then
dofile( mod .. ".lua") dofile( mod .. ".lua")
else else
dofile(mod .. ".lc") dofile(mod .. ".lc")
end end
end
function startSetupMode()
tmr.stop(0)
tmr.stop(1)
-- start the webserver module
mydofile("webserver")
wifi.setmode(wifi.SOFTAP) wifi.setmode(wifi.SOFTAP)
cfg={} cfg={}
@ -20,20 +24,6 @@ function startSetupMode()
startWebServer() startWebServer()
end end
print("Solving dependencies")
dependModules = { "timecore" , "wordclock", "displayword" }
for _,mod in pairs(dependModules) do
print("Loading " .. mod)
if (file.open(mod .. ".lua")) then
dofile( mod .. ".lua")
else
dofile(mod .. ".lc")
end
end
ledPin=4
-- Color is defined as GREEN, RED, BLUE
color=string.char(0,0,250)
function syncTimeFromInternet() function syncTimeFromInternet()
--ptbtime1.ptb.de --ptbtime1.ptb.de
@ -47,66 +37,85 @@ function syncTimeFromInternet()
) )
end end
connect_counter=0 function normalOperation()
-- Wait to be connect to the WiFi access point. ledPin=4
tmr.alarm(0, 500, 1, function() -- Color is defined as GREEN, RED, BLUE
connect_counter=connect_counter+1 color=string.char(0,0,250)
if wifi.sta.status() ~= 5 then
print("Connecting to AP...") connect_counter=0
if (connect_counter % 2 == 0) then -- Wait to be connect to the WiFi access point.
ws2812.write(ledPin, string.char(255,0,0):rep(114)) tmr.alarm(0, 500, 1, function()
else connect_counter=connect_counter+1
ws2812.write(ledPin, string.char(0,0,0):rep(114)) if wifi.sta.status() ~= 5 then
end print("Connecting to AP...")
else if (connect_counter % 2 == 0) then
tmr.stop(0) ws2812.write(ledPin, string.char(255,0,0):rep(114))
print('IP: ',wifi.sta.getip()) else
ws2812.write(ledPin, string.char(0,0,0):rep(114))
print("Start webserver...") end
tmr.alarm(2, 2000, 0 ,function() else
dofile("webserver.lua") tmr.stop(0)
startWebServer() 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
end) end)
tmr.alarm(1, 15000, 1 ,function()
sec, usec = rtctime.get()
-- Include the timezone
sec = sec + (timezoneoffset * 3600)
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)
end -- Used for debugging
-- when no wifi available, open an accesspoint and ask the user if (clockdebug ~= nil) then
if (connect_counter == 300) then -- 300 is 30 sec in 100ms cycle for key,value in pairs(words) do
startSetupmode() if (value > 0) then
end print(key,value)
end) end
end
tmr.alarm(1, 15000, 1 ,function()
sec, usec = rtctime.get()
-- Include the timezone
sec = sec + (timezoneoffset * 3600)
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)
-- Used for debugging
if (clockdebug ~= nil) then
for key,value in pairs(words) do
if (value > 0) then
print(key,value)
end
end end
end -- cleanup
-- cleanup ledBuf=nil
ledBuf=nil words=nil
words=nil time=nil
time=nil collectgarbage()
collectgarbage() end)
end) end
-- Logic
if ( file.open("config.lua") ) then if ( file.open("config.lua") ) then
--- Normal operation
print("Solving dependencies")
dependModules = { "timecore" , "wordclock", "displayword" }
for _,mod in pairs(dependModules) do
print("Loading " .. mod)
mydofile(mod)
end
wifi.setmode(wifi.STATION) wifi.setmode(wifi.STATION)
dofile("config.lua") dofile("config.lua")
normalOperation()
else else
-- Logic for inital setup
startSetupMode() startSetupMode()
end end

View File

@ -32,7 +32,7 @@ function sendWebPage(conn,answertype)
buf = buf .."</table></form>" buf = buf .."</table></form>"
if answertype==2 then if answertype==2 then
buf = buf .. "<h2><font color=\"green\">New configuration saved</font></h2\n>" buf = buf .. "<h2><font color=\"green\">New configuration saved</font></h2\n>"
elseif answertype==3 then elseif answertype==3 then
buf = buf .. "<h2><font color=\"red\">ERROR</font></h2\n>" buf = buf .. "<h2><font color=\"red\">ERROR</font></h2\n>"
elseif answertype==4 then elseif answertype==4 then
buf = buf .. "<h2><font color=\"orange\">Not all parameters set</font></h2\n>" buf = buf .. "<h2><font color=\"orange\">Not all parameters set</font></h2\n>"