diff --git a/init.lua b/init.lua index 0d0f1f8..2b7ce4e 100644 --- a/init.lua +++ b/init.lua @@ -13,7 +13,6 @@ end) local blacklistfile="init.lua config.lua config.lua.new webpage.html" function recompileAll() - for i=0,5 do tmr.stop(i) end -- compile all files l = file.list(); for k,_ in pairs(l) do @@ -43,9 +42,8 @@ function mydofile(mod) end end - -tmr.alarm(1, 5000, 0, function() - tmr.stop(2) +local initTimer = tmr.create() +initTimer:register(5000, tmr.ALARM_SINGLE, function (t) if ( (file.open("main.lua")) or (file.open("timecore.lua")) or @@ -68,5 +66,7 @@ tmr.alarm(1, 5000, 0, function() else print("No Main file found") end + t:unregister() end) +initTimer:start() print("Init file end reached") diff --git a/main.lua b/main.lua index c624ff0..2b97d2d 100644 --- a/main.lua +++ b/main.lua @@ -1,10 +1,9 @@ -- Main Module +local looptimer = tmr.create() displayword = {} function startSetupMode() - tmr.stop(0) - tmr.stop(1) -- start the webserver module mydofile("webserver") @@ -112,7 +111,8 @@ function normalOperation() connect_counter=0 -- 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 if wifi.sta.status() ~= 5 then print(connect_counter .. "/60 Connecting to AP...") @@ -148,7 +148,7 @@ function normalOperation() ws2812.write(string.char(0,0,0):rep(114)) end else - tmr.stop(0) + t:unregister() print('IP: ',wifi.sta.getip()) -- Here the WLAN is found, and something is done print("Solving dependencies") @@ -158,8 +158,9 @@ function normalOperation() mydofile(mod) end - setupCounter=5 - tmr.alarm(1, 5000, 1 ,function() + local setupCounter=5 + local alive=0 + looptimer:register(5000, tmr.ALARM_AUTO, function (lt) if (setupCounter > 4) then syncTimeFromInternet() setupCounter=setupCounter-1 @@ -178,17 +179,20 @@ function normalOperation() displayTime() end setupCounter=setupCounter-1 + elseif ((alive % 60) == 0) then + -- sync the time every 5 minutes + syncTimeFromInternet() + displayTime() + alive = alive + 1 else displayTime() + alive = alive + 1 end collectgarbage() + -- Feed the system watchdog. + tmr.wdclr() end) - - -- sync the time every 5 minutes - tmr.alarm(2, 300003, 1 ,function() - syncTimeFromInternet() - displayTime() - end) + looptimer:start() end -- when no wifi available, open an accesspoint and ask the user @@ -196,12 +200,8 @@ function normalOperation() startSetupMode() end end) + wifitimer:start() - -end - -function stopWordclock() - for i=0,5,1 do tmr.stop(i) end end -------------------main program ----------------------------- @@ -218,11 +218,12 @@ else end ----------- button --------- gpio.mode(3, gpio.INPUT) -btnCounter=0 --- Start the time Thread -tmr.alarm(4, 500, 1 ,function() +local btnCounter=0 +-- Start the time Thread handling the button +local btntimer = tmr.create() +btntimer:register(5000, tmr.ALARM_AUTO, function (t) if (gpio.read(3) == 0) then - tmr.stop(1) -- stop the LED thread + looptimer:unregister() print("Button pressed " .. tostring(btnCounter)) btnCounter = btnCounter + 5 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) +btntimer:start() diff --git a/mqtt.lua b/mqtt.lua index 6247e08..9784fe2 100644 --- a/mqtt.lua +++ b/mqtt.lua @@ -96,7 +96,9 @@ function registerMqtt() m=nil t=nil mqttConnected = false - for i=0,6,1 do tmr.stop(i) end + if (looptimer ~= nil) then + looptimer:unregister() + end collectgarbage() mydofile("telnet") if (startTelnetServer ~= nil) then @@ -120,14 +122,17 @@ function registerMqtt() mqttConnected = true -- subscribe topic with qos = 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 client:publish(mqttPrefix .. "/ip", tostring(wifi.sta.getip()), 0, 0) local red = string.byte(colorBg,2) local green = string.byte(colorBg,1) local blue = string.byte(colorBg,3) client:publish(mqttPrefix .. "/background", tostring(red) .. "," .. tostring(green) .. "," .. tostring(blue), 0, 0) + t:unregister() end) + mytimer:start() end, function(client, reason) print("failed reason: " .. reason) @@ -147,7 +152,8 @@ function startMqttClient() end oldBrightness=0 oldTemp=0 - tmr.alarm(5, 5001, 1 ,function() + local mqtttimer = tmr.create() + mqtttimer:register(5001, tmr.ALARM_AUTO, function (t) if (mqttConnected) then local temp = nil if (t ~= nil) then @@ -165,5 +171,6 @@ function startMqttClient() oldBrightness = briPercent end end) + mqtttimer:start() end end diff --git a/webserver.lua b/webserver.lua index d0e2d60..b61d2d3 100644 --- a/webserver.lua +++ b/webserver.lua @@ -130,7 +130,6 @@ end function stopWordclock() print("Stop all Wordclock") -- Stop all - for i=0,5 do tmr.stop(i) end -- 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\_]*" updateColor = nil @@ -159,18 +158,6 @@ function startWebServer() color=string.char(0,128,0) 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)) - -- 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 print("Sending webpage.html (" .. tostring(node.heap()) .. "B free) ...") -- Load the sendPagewebcontent @@ -285,18 +272,24 @@ function startWebServer() print("Rename config") if (file.rename(configFile .. ".new", configFile)) then print("Successfully") - tmr.alarm(3, 20, 0 ,function() + local mytimer = tmr.create() + mytimer:register(50, tmr.ALARM_SINGLE, function (t) replaceMap=fillDynamicMap() replaceMap["$ADDITIONAL_LINE"]="