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
dw = nil
collectgarbage()
print("dw: " .. tostring(node.heap()))
-- cleanup
i=nil

View File

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