Move startup procedure into one loop
This commit is contained in:
parent
10104b5d0e
commit
7e7a87f90a
32
main.lua
32
main.lua
@ -31,16 +31,20 @@ 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
|
||||
@ -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
|
||||
|
5
mqtt.lua
5
mqtt.lua
@ -34,14 +34,17 @@ function startMqtt()
|
||||
end)
|
||||
end
|
||||
|
||||
if (mqttServer ~= nil and mqttPrefix ~= nil) then
|
||||
function startMqttClient()
|
||||
if (mqttServer ~= nil and mqttPrefix ~= nil) then
|
||||
startMqtt()
|
||||
print "Started MQTT client"
|
||||
oldBrightness=0
|
||||
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
|
Loading…
Reference in New Issue
Block a user