Start DS18B20 initialization; uniq names before creating _diet files

This commit is contained in:
Ollo 2021-11-28 09:49:12 +01:00
parent 0bc446fc2a
commit 96b690d90b
2 changed files with 14 additions and 11 deletions

View File

@ -67,7 +67,6 @@ function displayTime()
end end
dw = nil dw = nil
collectgarbage() collectgarbage()
print("dw: " .. tostring(node.heap()))
-- cleanup -- cleanup
i=nil i=nil

View File

@ -73,9 +73,12 @@ function readTemp()
local temp1=0 local temp1=0
if (sensors >= 1) then if (sensors >= 1) then
temp1=t.read(addrs[0]) temp1=t.read(addrs[0])
else
print("No sensor DS18B20 found")
end end
return temp1 return temp1
else else
print("No DS18B20 lib")
return nil return nil
end end
end end
@ -200,7 +203,7 @@ function startMqttClient()
registerMqtt() registerMqtt()
print "Started MQTT client" print "Started MQTT client"
local dstimer = tmr.create() local dstimer = tmr.create()
dstimer:register(123, tmr.ALARM_SINGLE, function (t) dstimer:register(123, tmr.ALARM_SINGLE, function (kTemp)
if (file.open("ds18b20_diet.lc")) then if (file.open("ds18b20_diet.lc")) then
t=require("ds18b20_diet") t=require("ds18b20_diet")
t.setup(2) -- GPIO4 t.setup(2) -- GPIO4
@ -208,24 +211,25 @@ function startMqttClient()
print "Setup temperature" print "Setup temperature"
end end
end) end)
local oldBrightness=0 dstimer:start()
oldTemp=0 local oldBrightness=0
oldTemp=0
local mqtttimer = tmr.create() local mqtttimer = tmr.create()
mqtttimer:register(5001, tmr.ALARM_AUTO, function (t) mqtttimer:register(5001, tmr.ALARM_AUTO, function (kTemp)
if (mqttConnected) then if (mqttConnected) then
local temp = nil local temperatur = nil
if (t ~= nil) then if (t ~= nil) then
temp=readTemp() temperatur=readTemp()
end end
if (oldBrightness ~= briPer) then if (oldBrightness ~= briPer) then
m:publish(mqttPrefix .. "/brightness", tostring(briPer), 0, 0) m:publish(mqttPrefix .. "/brightness", tostring(briPer), 0, 0)
elseif (temp ~= nil and temp ~= oldTemp) then oldBrightness = briPer
oldTemp = temp elseif (temperatur ~= nil and temperatur ~= oldTemp) then
m:publish(mqttPrefix .. "/temp", tostring(temp/100).."."..tostring(temp%100), 0, 0) oldTemp = temperatur
m:publish(mqttPrefix .. "/temp", tostring(temperatur/100).."."..tostring(temperatur%100), 0, 0)
else else
m:publish(mqttPrefix .. "/heap", tostring(node.heap()), 0, 0) m:publish(mqttPrefix .. "/heap", tostring(node.heap()), 0, 0)
end end
oldBrightness = briPer
end end
end) end)
mqtttimer:start() mqtttimer:start()