Timer calls replaced

This commit is contained in:
Ollo 2021-01-31 20:22:32 +01:00
parent fc97df71ea
commit f72295e709
4 changed files with 45 additions and 43 deletions

View File

@ -13,7 +13,6 @@ end)
local blacklistfile="init.lua config.lua config.lua.new webpage.html" local blacklistfile="init.lua config.lua config.lua.new webpage.html"
function recompileAll() function recompileAll()
for i=0,5 do tmr.stop(i) end
-- compile all files -- compile all files
l = file.list(); l = file.list();
for k,_ in pairs(l) do for k,_ in pairs(l) do
@ -43,9 +42,8 @@ function mydofile(mod)
end end
end end
local initTimer = tmr.create()
tmr.alarm(1, 5000, 0, function() initTimer:register(5000, tmr.ALARM_SINGLE, function (t)
tmr.stop(2)
if ( if (
(file.open("main.lua")) or (file.open("main.lua")) or
(file.open("timecore.lua")) or (file.open("timecore.lua")) or
@ -68,5 +66,7 @@ tmr.alarm(1, 5000, 0, function()
else else
print("No Main file found") print("No Main file found")
end end
t:unregister()
end) end)
initTimer:start()
print("Init file end reached") print("Init file end reached")

View File

@ -1,10 +1,9 @@
-- Main Module -- Main Module
local looptimer = tmr.create()
displayword = {} displayword = {}
function startSetupMode() function startSetupMode()
tmr.stop(0)
tmr.stop(1)
-- start the webserver module -- start the webserver module
mydofile("webserver") mydofile("webserver")
@ -112,7 +111,8 @@ function normalOperation()
connect_counter=0 connect_counter=0
-- Wait to be connect to the WiFi access point. -- Wait to be connect to the WiFi access point.
tmr.alarm(0, 500, 1, function() local wifitimer = tmr.create()
wifitimer:register(5000, tmr.ALARM_SINGLE, function (t)
connect_counter=connect_counter+1 connect_counter=connect_counter+1
if wifi.sta.status() ~= 5 then if wifi.sta.status() ~= 5 then
print(connect_counter .. "/60 Connecting to AP...") print(connect_counter .. "/60 Connecting to AP...")
@ -148,7 +148,7 @@ function normalOperation()
ws2812.write(string.char(0,0,0):rep(114)) ws2812.write(string.char(0,0,0):rep(114))
end end
else else
tmr.stop(0) t:unregister()
print('IP: ',wifi.sta.getip()) print('IP: ',wifi.sta.getip())
-- Here the WLAN is found, and something is done -- Here the WLAN is found, and something is done
print("Solving dependencies") print("Solving dependencies")
@ -158,8 +158,9 @@ function normalOperation()
mydofile(mod) mydofile(mod)
end end
setupCounter=5 local setupCounter=5
tmr.alarm(1, 5000, 1 ,function() local alive=0
looptimer:register(5000, tmr.ALARM_AUTO, function (lt)
if (setupCounter > 4) then if (setupCounter > 4) then
syncTimeFromInternet() syncTimeFromInternet()
setupCounter=setupCounter-1 setupCounter=setupCounter-1
@ -178,17 +179,20 @@ function normalOperation()
displayTime() displayTime()
end end
setupCounter=setupCounter-1 setupCounter=setupCounter-1
elseif ((alive % 60) == 0) then
-- sync the time every 5 minutes
syncTimeFromInternet()
displayTime()
alive = alive + 1
else else
displayTime() displayTime()
alive = alive + 1
end end
collectgarbage() collectgarbage()
-- Feed the system watchdog.
tmr.wdclr()
end) end)
looptimer:start()
-- sync the time every 5 minutes
tmr.alarm(2, 300003, 1 ,function()
syncTimeFromInternet()
displayTime()
end)
end end
-- when no wifi available, open an accesspoint and ask the user -- when no wifi available, open an accesspoint and ask the user
@ -196,12 +200,8 @@ function normalOperation()
startSetupMode() startSetupMode()
end end
end) end)
wifitimer:start()
end
function stopWordclock()
for i=0,5,1 do tmr.stop(i) end
end end
-------------------main program ----------------------------- -------------------main program -----------------------------
@ -218,11 +218,12 @@ else
end end
----------- button --------- ----------- button ---------
gpio.mode(3, gpio.INPUT) gpio.mode(3, gpio.INPUT)
btnCounter=0 local btnCounter=0
-- Start the time Thread -- Start the time Thread handling the button
tmr.alarm(4, 500, 1 ,function() local btntimer = tmr.create()
btntimer:register(5000, tmr.ALARM_AUTO, function (t)
if (gpio.read(3) == 0) then if (gpio.read(3) == 0) then
tmr.stop(1) -- stop the LED thread looptimer:unregister()
print("Button pressed " .. tostring(btnCounter)) print("Button pressed " .. tostring(btnCounter))
btnCounter = btnCounter + 5 btnCounter = btnCounter + 5
local ledBuf= string.char(128,0,0):rep(btnCounter) .. string.char(0,0,0):rep(110 - btnCounter) local ledBuf= string.char(128,0,0):rep(btnCounter) .. string.char(0,0,0):rep(110 - btnCounter)
@ -234,3 +235,4 @@ tmr.alarm(4, 500, 1 ,function()
end end
end end
end) end)
btntimer:start()

View File

@ -96,7 +96,9 @@ function registerMqtt()
m=nil m=nil
t=nil t=nil
mqttConnected = false mqttConnected = false
for i=0,6,1 do tmr.stop(i) end if (looptimer ~= nil) then
looptimer:unregister()
end
collectgarbage() collectgarbage()
mydofile("telnet") mydofile("telnet")
if (startTelnetServer ~= nil) then if (startTelnetServer ~= nil) then
@ -120,14 +122,17 @@ function registerMqtt()
mqttConnected = true mqttConnected = true
-- subscribe topic with qos = 0 -- subscribe topic with qos = 0
client:subscribe(mqttPrefix .. "/cmd/#", 0) client:subscribe(mqttPrefix .. "/cmd/#", 0)
tmr.alarm(3, 1000, 0, function() local mytimer = tmr.create()
mytimer:register(1000, tmr.ALARM_SINGLE, function (t)
-- publish a message with data = hello, QoS = 0, retain = 0 -- publish a message with data = hello, QoS = 0, retain = 0
client:publish(mqttPrefix .. "/ip", tostring(wifi.sta.getip()), 0, 0) client:publish(mqttPrefix .. "/ip", tostring(wifi.sta.getip()), 0, 0)
local red = string.byte(colorBg,2) local red = string.byte(colorBg,2)
local green = string.byte(colorBg,1) local green = string.byte(colorBg,1)
local blue = string.byte(colorBg,3) local blue = string.byte(colorBg,3)
client:publish(mqttPrefix .. "/background", tostring(red) .. "," .. tostring(green) .. "," .. tostring(blue), 0, 0) client:publish(mqttPrefix .. "/background", tostring(red) .. "," .. tostring(green) .. "," .. tostring(blue), 0, 0)
t:unregister()
end) end)
mytimer:start()
end, end,
function(client, reason) function(client, reason)
print("failed reason: " .. reason) print("failed reason: " .. reason)
@ -147,7 +152,8 @@ function startMqttClient()
end end
oldBrightness=0 oldBrightness=0
oldTemp=0 oldTemp=0
tmr.alarm(5, 5001, 1 ,function() local mqtttimer = tmr.create()
mqtttimer:register(5001, tmr.ALARM_AUTO, function (t)
if (mqttConnected) then if (mqttConnected) then
local temp = nil local temp = nil
if (t ~= nil) then if (t ~= nil) then
@ -165,5 +171,6 @@ function startMqttClient()
oldBrightness = briPercent oldBrightness = briPercent
end end
end) end)
mqtttimer:start()
end end
end end

View File

@ -130,7 +130,6 @@ end
function stopWordclock() function stopWordclock()
print("Stop all Wordclock") print("Stop all Wordclock")
-- Stop all -- Stop all
for i=0,5 do tmr.stop(i) end
-- unload all other functions -- unload all other functions
-- grep function *.lua | grep -v webserver | grep -v init.lua | grep -v main.lua | cut -f 2 -d ':' | grep "^function" | sed "s/function //g" | grep -o "^[a-zA-Z0-9\_]*" -- grep function *.lua | grep -v webserver | grep -v init.lua | grep -v main.lua | cut -f 2 -d ':' | grep "^function" | sed "s/function //g" | grep -o "^[a-zA-Z0-9\_]*"
updateColor = nil updateColor = nil
@ -159,18 +158,6 @@ function startWebServer()
color=string.char(0,128,0) color=string.char(0,128,0)
end end
ws2812.write(string.char(0,0,0):rep(56) .. color:rep(2) .. string.char(0,0,0):rep(4) .. color:rep(2) .. string.char(0,0,0):rep(48)) ws2812.write(string.char(0,0,0):rep(56) .. color:rep(2) .. string.char(0,0,0):rep(4) .. color:rep(2) .. string.char(0,0,0):rep(48))
-- Start Time after 3 minute
tmr.alarm(5, 180000, 0 ,function()
dependModules = { "timecore" , "wordclock", "displayword" }
for _,mod in pairs(dependModules) do
print("Loading " .. mod)
mydofile(mod)
end
-- Start the time Thread again
tmr.alarm(1, 20000, 1 ,function()
displayTime()
end)
end)
if (sendPage ~= nil) then if (sendPage ~= nil) then
print("Sending webpage.html (" .. tostring(node.heap()) .. "B free) ...") print("Sending webpage.html (" .. tostring(node.heap()) .. "B free) ...")
-- Load the sendPagewebcontent -- Load the sendPagewebcontent
@ -285,18 +272,24 @@ function startWebServer()
print("Rename config") print("Rename config")
if (file.rename(configFile .. ".new", configFile)) then if (file.rename(configFile .. ".new", configFile)) then
print("Successfully") print("Successfully")
tmr.alarm(3, 20, 0 ,function() local mytimer = tmr.create()
mytimer:register(50, tmr.ALARM_SINGLE, function (t)
replaceMap=fillDynamicMap() replaceMap=fillDynamicMap()
replaceMap["$ADDITIONAL_LINE"]="<h2><font color=\"green\">New configuration saved</font></h2>" replaceMap["$ADDITIONAL_LINE"]="<h2><font color=\"green\">New configuration saved</font></h2>"
print("Send success to client") print("Send success to client")
sendPage(conn, "webpage.html", replaceMap) sendPage(conn, "webpage.html", replaceMap)
t:unregister()
end) end)
mytimer:start()
else else
tmr.alarm(3, 20, 0 ,function() local mytimer = tmr.create()
mytimer:register(50, tmr.ALARM_SINGLE, function (t)
replaceMap=fillDynamicMap() replaceMap=fillDynamicMap()
replaceMap["$ADDITIONAL_LINE"]="<h2><font color=\"red\">ERROR</font></h2>" replaceMap["$ADDITIONAL_LINE"]="<h2><font color=\"red\">ERROR</font></h2>"
sendPage(conn, "webpage.html", replaceMap) sendPage(conn, "webpage.html", replaceMap)
t:unregister()
end) end)
mytimer:start()
end end
else else
replaceMap=fillDynamicMap() replaceMap=fillDynamicMap()