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

View File

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