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,17 +31,21 @@ 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
function displayTime() function displayTime()
@ -156,24 +160,33 @@ function normalOperation()
mydofile(mod) mydofile(mod)
end end
tmr.alarm(2, 500, 0 ,function() setupCounter=5
tmr.alarm(1, 5000, 1 ,function()
if (setupCounter > 4) then
syncTimeFromInternet() syncTimeFromInternet()
end) setupCounter=setupCounter-1
tmr.alarm(3, 2000, 0 ,function() elseif (setupCounter > 3) then
if (startTelnetServer ~= nil) then if (startTelnetServer ~= nil) then
startTelnetServer() startTelnetServer()
else else
print("NO Telent found") print("NO Telent found")
end end
end) setupCounter=setupCounter-1
-- Start the time Thread elseif (setupCounter > 2) then
tmr.alarm(1, 5000, 1 ,function() if (startMqttClient ~= nil) then
startMqttClient()
else
print("NO Mqtt found")
end
setupCounter=setupCounter-1
else
displayTime() displayTime()
end
collectgarbage() collectgarbage()
end) 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,6 +34,7 @@ function startMqtt()
end) end)
end end
function startMqttClient()
if (mqttServer ~= nil and mqttPrefix ~= nil) then if (mqttServer ~= nil and mqttPrefix ~= nil) then
startMqtt() startMqtt()
print "Started MQTT client" print "Started MQTT client"
@ -41,7 +42,9 @@ if (mqttServer ~= nil and mqttPrefix ~= nil) then
tmr.alarm(5, 10000, 1 ,function() tmr.alarm(5, 10000, 1 ,function()
if (oldBrightness ~= briPercent) then if (oldBrightness ~= briPercent) then
m:publish(mqttPrefix .. "/brightness", tostring(briPercent), 0, 0) m:publish(mqttPrefix .. "/brightness", tostring(briPercent), 0, 0)
m:publish(mqttPrefix .. "/heap", tostring(node.heap()), 0, 0)
end end
oldBrightness = briPercent oldBrightness = briPercent
end) end)
end end
end