2015-01-10 10:58:08 +01:00
|
|
|
t = require("ds18b20")
|
2017-02-25 22:56:45 +01:00
|
|
|
pin = 3 -- gpio0 = 3, gpio2 = 4
|
2015-01-10 10:58:08 +01:00
|
|
|
|
2017-08-16 21:04:52 +02:00
|
|
|
local function readout(temp)
|
|
|
|
if t.sens then
|
|
|
|
print("Total number of DS18B20 sensors: ".. #t.sens)
|
|
|
|
for i, s in ipairs(t.sens) do
|
|
|
|
print(string.format(" sensor #%d address: %s%s", i, ('%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X'):format(s:byte(1,8)), s:byte(9) == 1 and " (parasite)" or ""))
|
|
|
|
end
|
|
|
|
end
|
2017-02-25 22:56:45 +01:00
|
|
|
for addr, temp in pairs(temp) do
|
2017-08-16 21:04:52 +02:00
|
|
|
print(string.format("Sensor %s: %s °C", ('%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X'):format(addr:byte(1,8)), temp))
|
2017-02-25 22:56:45 +01:00
|
|
|
end
|
2015-01-10 10:58:08 +01:00
|
|
|
|
2017-02-25 22:56:45 +01:00
|
|
|
-- Module can be released when it is no longer needed
|
2017-08-16 21:04:52 +02:00
|
|
|
--t = nil
|
|
|
|
--package.loaded["ds18b20"]=nil
|
2015-01-10 10:58:08 +01:00
|
|
|
end
|
|
|
|
|
2017-08-16 21:04:52 +02:00
|
|
|
t:enable_debug()
|
|
|
|
file.remove("ds18b20_save.lc") -- remove saved addresses
|
|
|
|
print("=============================================", node.heap())
|
|
|
|
print("first call, no addresses in flash, search is performed")
|
|
|
|
t:read_temp(readout, pin, t.C)
|
|
|
|
|
|
|
|
tmr.create():alarm(2000, tmr.ALARM_SINGLE, function()
|
|
|
|
print("=============================================", node.heap())
|
|
|
|
print("second readout, no new search, found addresses are used")
|
|
|
|
t:read_temp(readout, pin)
|
|
|
|
|
|
|
|
tmr.create():alarm(2000, tmr.ALARM_SINGLE, function()
|
|
|
|
print("=============================================", node.heap())
|
|
|
|
print("force search again")
|
|
|
|
t:read_temp(readout, pin, nil, true)
|
|
|
|
|
|
|
|
tmr.create():alarm(2000, tmr.ALARM_SINGLE, function()
|
|
|
|
print("=============================================", node.heap())
|
|
|
|
print("save search results")
|
|
|
|
t:read_temp(readout, pin, nil, false, true)
|
|
|
|
|
|
|
|
tmr.create():alarm(2000, tmr.ALARM_SINGLE, function()
|
|
|
|
print("=============================================", node.heap())
|
|
|
|
print("use saved addresses")
|
|
|
|
t.sens={}
|
|
|
|
t:read_temp(readout, pin)
|
|
|
|
end)
|
|
|
|
|
|
|
|
end)
|
|
|
|
|
|
|
|
end)
|
|
|
|
|
|
|
|
end)
|