No actions on low memory

This commit is contained in:
Ollo 2021-12-04 13:04:12 +01:00
parent b8f037d61c
commit fd85314d37
2 changed files with 72 additions and 52 deletions

View File

@ -11,7 +11,6 @@ function syncTimeFromInternet()
syncRunning=true syncRunning=true
sntp.sync(sntpserverhostname, sntp.sync(sntpserverhostname,
function(sec,usec,server) function(sec,usec,server)
print('sync', sec, usec, server)
syncRunning=nil syncRunning=nil
end, end,
function() function()
@ -129,10 +128,13 @@ function normalOperation()
end end
setupCounter=setupCounter-1 setupCounter=setupCounter-1
elseif ( (alive % 120) == 0) then elseif ( (alive % 120) == 0) then
-- sync the time every 5 minutes -- sync the time every 5 minutes
syncTimeFromInternet() local heapusage = node.heap()
if (heapusage > 12000) then
syncTimeFromInternet()
end
heapusage=nil
alive = alive + 1 alive = alive + 1
collectgarbage()
else else
displayTime() displayTime()
alive = alive + 1 alive = alive + 1
@ -158,6 +160,7 @@ function normalOperation()
ws2812.write(rgbBuffer) ws2812.write(rgbBuffer)
print("Fallback no time displayed") print("Fallback no time displayed")
end end
collectgarbage()
-- Feed the system watchdog. -- Feed the system watchdog.
tmr.wdclr() tmr.wdclr()
end) end)

113
mqtt.lua
View File

@ -1,18 +1,21 @@
-- Global variable -- Global Variables
local m=nil -- Display Temp
local mqttConnected = false tw=nil
-- Temp: tcol=nil
local t=nil -- Module Variables
local tw=nil -- Mqtt variable
local tcol=nil local mMqttClient=nil
local mMqttConnected = false
-- Read Temp
local mOldTemp=nil
function handleSingleCommand(client, topic, data) function handleSingleCommand(client, topic, data)
if (data == "ON") then if (data == "ON") then
briPer=100 briPer=100
m:publish(mqttPrefix .. "/clock", "ON", 0, 0) mMqttClient:publish(mqttPrefix .. "/clock", "ON", 0, 0)
elseif (data == "OFF") then elseif (data == "OFF") then
briPer=0 briPer=0
m:publish(mqttPrefix .. "/clock", "OFF", 0, 0) mMqttClient:publish(mqttPrefix .. "/clock", "OFF", 0, 0)
elseif ((data:sub(1,1) == "#" and data:len() == 7) or (string.match(data, "%d+,%d+,%d+"))) then elseif ((data:sub(1,1) == "#" and data:len() == 7) or (string.match(data, "%d+,%d+,%d+"))) then
local red=0 local red=0
local green=0 local green=0
@ -26,11 +29,11 @@ function handleSingleCommand(client, topic, data)
end end
colorBg=string.char(green * briPer / 100, red * briPer / 100, blue * briPer / 100) colorBg=string.char(green * briPer / 100, red * briPer / 100, blue * briPer / 100)
print("Updated BG: " .. tostring(red) .. "," .. tostring(green) .. "," .. tostring(blue) ) print("Updated BG: " .. tostring(red) .. "," .. tostring(green) .. "," .. tostring(blue) )
m:publish(mqttPrefix .. "/background", tostring(red) .. "," .. tostring(green) .. "," .. tostring(blue), 0, 0) mMqttClient:publish(mqttPrefix .. "/background", tostring(red) .. "," .. tostring(green) .. "," .. tostring(blue), 0, 0)
else else
if (tonumber(data) >= 0 and tonumber(data) <= 100) then if (tonumber(data) >= 0 and tonumber(data) <= 100) then
briPer=tonumber(data) briPer=tonumber(data)
m:publish(mqttPrefix .. "/clock", tostring(data), 0, 0) mMqttClient:publish(mqttPrefix .. "/clock", tostring(data), 0, 0)
else else
print "Unknown MQTT command" print "Unknown MQTT command"
end end
@ -54,7 +57,7 @@ function parseBgColor(data, row, per)
red, green, blue = string.match(data, "(%d+),(%d+),(%d+)") red, green, blue = string.match(data, "(%d+),(%d+),(%d+)")
end end
if ((red ~= nil) and (green ~= nil) and (blue ~= nil) ) then if ((red ~= nil) and (green ~= nil) and (blue ~= nil) ) then
m:publish(mqttPrefix .. "/"..row, tostring(red) .. "," .. tostring(green) .. "," .. tostring(blue), 0, 0) mMqttClient:publish(mqttPrefix .. "/"..row, tostring(red) .. "," .. tostring(green) .. "," .. tostring(blue), 0, 0)
if (per ~= nil) then if (per ~= nil) then
return string.char(green * per / 100, red * per / 100, blue * per / 100) return string.char(green * per / 100, red * per / 100, blue * per / 100)
else else
@ -67,9 +70,9 @@ end
function readTemp(ds18b20) function readTemp(ds18b20)
if (ds18b20 ~= nil) then if (ds18b20 ~= nil) then
addrs=ds18b20.addrs() local addrs=ds18b20.addrs()
-- Total DS18B20 numbers -- Total DS18B20 numbers
sensors=table.getn(addrs) local sensors=table.getn(addrs)
local temp1=0 local temp1=0
if (sensors >= 1) then if (sensors >= 1) then
temp1=ds18b20.read(addrs[0]) temp1=ds18b20.read(addrs[0])
@ -85,39 +88,47 @@ end
-- Connect or reconnect to mqtt server -- Connect or reconnect to mqtt server
function reConnectMqtt() function reConnectMqtt()
if (not mqttConnected) then if (not mMqttConnected) then
m:connect(mqttServer, 1883, false, function(c) mMqttClient:connect(mqttServer, 1883, false, function(c)
print("MQTT is connected") print("MQTT is connected")
mqttConnected = true mMqttConnected = true
-- subscribe topic with qos = 0 -- subscribe topic with qos = 0
m:subscribe(mqttPrefix .. "/cmd/#", 0) mMqttClient:subscribe(mqttPrefix .. "/cmd/#", 0)
local tmr1 = tmr.create() local tmr1 = tmr.create()
tmr1:register(1000, tmr.ALARM_SINGLE, function (dummyTemp) tmr1:register(1000, tmr.ALARM_SINGLE, function (dummyTemp)
-- publish a message with data = hello, QoS = 0, retain = 0 -- publish a message with data = hello, QoS = 0, retain = 0
m:publish(mqttPrefix .. "/ip", tostring(wifi.sta.getip()), 0, 0) mMqttClient: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)
m:publish(mqttPrefix .. "/background", tostring(red) .. "," .. tostring(green) .. "," .. tostring(blue), 0, 0) mMqttClient:publish(mqttPrefix .. "/background", tostring(red) .. "," .. tostring(green) .. "," .. tostring(blue), 0, 0)
tmr1:unregister() tmr1:unregister()
tmr1=nil
end) end)
tmr1:start() tmr1:start()
end, end,
function(client, reason) function(client, reason)
print("failed reason: " .. reason) print("failed reason: " .. reason)
mqttConnected = false mMqttConnected = false
end) end)
end end
end end
-- MQTT extension -- MQTT extension
function registerMqtt() function registerMqtt()
m = mqtt.Client("wordclock", 120) mMqttClient = mqtt.Client("wordclock", 120)
-- on publish message receive event -- on publish message receive event
m:on("message", function(client, topic, data) mMqttClient:on("message", function(client, topic, data)
print("MQTT " .. topic .. ":" ) collectgarbage()
if data ~= nil then if data ~= nil then
print(data) local heapusage = node.heap()
if (heapusage < 12000) then
print("Skip " .. topic .. ":" .. data .. "; heap:" .. heapusage)
heapusage=nil
return
end
heapusage=nil
print("MQTT " .. topic .. ":" .. data)
if (topic == (mqttPrefix .. "/cmd/single")) then if (topic == (mqttPrefix .. "/cmd/single")) then
handleSingleCommand(client, topic, data) handleSingleCommand(client, topic, data)
elseif (topic == (mqttPrefix .. "/cmd/temp")) then elseif (topic == (mqttPrefix .. "/cmd/temp")) then
@ -146,7 +157,7 @@ function registerMqtt()
print("Stop Mqtt and Temp") print("Stop Mqtt and Temp")
m=nil m=nil
t=nil t=nil
mqttConnected = false mMqttConnected = false
if (mlt ~= nil) then if (mlt ~= nil) then
mlt:unregister() mlt:unregister()
else else
@ -178,7 +189,6 @@ function registerMqtt()
for i=1,10,1 do for i=1,10,1 do
if (string.match(topic, "row".. tostring(i) .."$")) then if (string.match(topic, "row".. tostring(i) .."$")) then
rowbgColor[i] = parseBgColor(data, "row" .. tostring(i), briPer) rowbgColor[i] = parseBgColor(data, "row" .. tostring(i), briPer)
print("Updated row" .. tostring(i) )
return return
end end
end end
@ -186,40 +196,45 @@ function registerMqtt()
end end
end end
end) end)
m:on("offline", function(client) -- do something
mMqttClient:on("offline", function(client)
print("MQTT Disconnected") print("MQTT Disconnected")
mqttConnected = false mMqttConnected = false
end collectgarbage()
) end)
reConnectMqtt() reConnectMqtt()
end end
function connectedMqtt() function connectedMqtt()
return mqttConnected return mMqttConnected
end end
function startMqttClient() function startMqttClient()
if (mqttServer ~= nil and mqttPrefix ~= nil) then if (mqttServer ~= nil and mqttPrefix ~= nil) then
registerMqtt() registerMqtt()
print "Started MQTT client" print "Started MQTT client"
local dstimer = tmr.create() local lSetupTimer = tmr.create()
dstimer:register(123, tmr.ALARM_SINGLE, function (kTemp) lSetupTimer:register(123, tmr.ALARM_SINGLE, function (kTemp)
if (file.open("ds18b20_diet.lc")) then if (file.open("ds18b20_diet.lc")) then
t=true t=true
print "Setup temperatur" print "Setup temperatur"
end end
end) end)
dstimer:start() lSetupTimer:start()
local oldBrightness=0 local loldBrightness=0
oldTemp=0 mOldTemp=0
local mqtttimer = tmr.create() local lMqttTimer = tmr.create()
mqtttimer:register(6001, tmr.ALARM_AUTO, function (kTemp) -- Check every 12 seconds
if (mqttConnected) then lMqttTimer:register(12321, tmr.ALARM_AUTO, function (kTemp)
if (mMqttConnected) then
-- Cleanup the initialization stuff
lSetupTimer=nil
-- Do the action
local heapusage = node.heap() local heapusage = node.heap()
local temperatur = nil local temperatur = nil
if (oldBrightness ~= briPer) then if (loldBrightness ~= briPer) then
m:publish(mqttPrefix .. "/brightness", tostring(briPer), 0, 0) mMqttClient:publish(mqttPrefix .. "/brightness", tostring(briPer), 0, 0)
oldBrightness = briPer loldBrightness = briPer
else else
if ((t ~= nil) and (heapusage > 12000)) then if ((t ~= nil) and (heapusage > 12000)) then
local ds18b20=require("ds18b20_diet") local ds18b20=require("ds18b20_diet")
@ -233,16 +248,18 @@ function startMqttClient()
else else
collectgarbage() collectgarbage()
end end
if (temperatur ~= nil and temperatur ~= oldTemp) then if (temperatur ~= nil and temperatur ~= mOldTemp) then
oldTemp = temperatur mOldTemp = temperatur
m:publish(mqttPrefix .. "/temp", tostring(temperatur/100)..".".. tostring(temperatur%100), 0, 0) mMqttClient:publish(mqttPrefix .. "/temp", tostring(temperatur/100)..".".. tostring(temperatur%100), 0, 0)
else else
m:publish(mqttPrefix .. "/heap", tostring(heapusage), 0, 0) mMqttClient:publish(mqttPrefix .. "/heap", tostring(heapusage), 0, 0)
end end
end end
heapusage=nil
temperatur=nil
end end
end) end)
mqtttimer:start() lMqttTimer:start()
end end
end end