Move startup procedure into one loop

This commit is contained in:
Ollo 2020-12-08 21:51:14 +01:00
parent 10104b5d0e
commit 7e7a87f90a
2 changed files with 46 additions and 29 deletions

View File

@ -31,16 +31,20 @@ end
function syncTimeFromInternet() function syncTimeFromInternet()
--ptbtime1.ptb.de if (syncRunning == nil) then
syncRunning=true
sntp.sync(sntpserverhostname, sntp.sync(sntpserverhostname,
function(sec,usec,server) function(sec,usec,server)
print('sync', sec, usec, server) print('sync', sec, usec, server)
displayTime() displayTime()
syncRunning=nil
end, end,
function() function()
print('failed!') print('failed!')
syncRunning=nil
end end
) )
end
end end
briPercent = 50 briPercent = 50
@ -155,25 +159,34 @@ function normalOperation()
print("Loading " .. mod) print("Loading " .. mod)
mydofile(mod) mydofile(mod)
end end
tmr.alarm(2, 500, 0 ,function() setupCounter=5
syncTimeFromInternet()
end)
tmr.alarm(3, 2000, 0 ,function()
if (startTelnetServer ~= nil) then
startTelnetServer()
else
print("NO Telent found")
end
end)
-- Start the time Thread
tmr.alarm(1, 5000, 1 ,function() tmr.alarm(1, 5000, 1 ,function()
displayTime() if (setupCounter > 4) then
collectgarbage() syncTimeFromInternet()
end) setupCounter=setupCounter-1
elseif (setupCounter > 3) then
if (startTelnetServer ~= nil) then
startTelnetServer()
else
print("NO Telent found")
end
setupCounter=setupCounter-1
elseif (setupCounter > 2) then
if (startMqttClient ~= nil) then
startMqttClient()
else
print("NO Mqtt found")
end
setupCounter=setupCounter-1
else
displayTime()
end
collectgarbage()
end)
-- sync the time every 5 minutes -- sync the time every 5 minutes
tmr.alarm(4, 300000, 1 ,function() tmr.alarm(2, 300000, 1 ,function()
syncTimeFromInternet() syncTimeFromInternet()
displayTime() displayTime()
end) end)
@ -198,7 +211,7 @@ ws2812.init() -- WS2812 LEDs initialized on GPIO2
if ( file.open("config.lua") ) then if ( file.open("config.lua") ) then
--- Normal operation --- Normal operation
wifi.setmode(wifi.STATION) wifi.setmode(wifi.STATION)
dofile("config.lua") mydofile("config")
normalOperation() normalOperation()
else else
-- Logic for inital setup -- Logic for inital setup
@ -217,6 +230,7 @@ tmr.alarm(4, 500, 1 ,function()
ws2812.write(ledBuf) ws2812.write(ledBuf)
if (btnCounter >= 110) then if (btnCounter >= 110) then
file.remove("config.lua") file.remove("config.lua")
file.remove("config.lc")
node.restart() node.restart()
end end
end end

View File

@ -34,14 +34,17 @@ function startMqtt()
end) end)
end end
if (mqttServer ~= nil and mqttPrefix ~= nil) then function startMqttClient()
startMqtt() if (mqttServer ~= nil and mqttPrefix ~= nil) then
print "Started MQTT client" startMqtt()
oldBrightness=0 print "Started MQTT client"
tmr.alarm(5, 10000, 1 ,function() oldBrightness=0
if (oldBrightness ~= briPercent) then tmr.alarm(5, 10000, 1 ,function()
m:publish(mqttPrefix .. "/brightness", tostring(briPercent), 0, 0) if (oldBrightness ~= briPercent) then
end m:publish(mqttPrefix .. "/brightness", tostring(briPercent), 0, 0)
oldBrightness = briPercent m:publish(mqttPrefix .. "/heap", tostring(node.heap()), 0, 0)
end) end
end oldBrightness = briPercent
end)
end
end