2014-12-08 09:22:38 +01:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- DS18B20 one wire module for NODEMCU
|
2017-02-25 22:56:45 +01:00
|
|
|
-- by @voborsky, @devsaurus
|
|
|
|
-- encoder module is needed only for debug output; lines can be removed if no
|
|
|
|
-- debug output is needed and/or encoder module is missing
|
|
|
|
--
|
|
|
|
-- by default the module is for integer version, comment integer version and
|
|
|
|
-- uncomment float version part for float version
|
2014-12-08 09:22:38 +01:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2017-02-25 22:56:45 +01:00
|
|
|
return({
|
|
|
|
pin=3,
|
|
|
|
sens={},
|
|
|
|
temp={},
|
2017-03-07 07:06:50 +01:00
|
|
|
|
2017-02-25 22:56:45 +01:00
|
|
|
conversion = function(self)
|
|
|
|
local pin = self.pin
|
|
|
|
for i,s in ipairs(self.sens) do
|
|
|
|
if s.status == 0 then
|
2017-03-07 07:06:50 +01:00
|
|
|
print("starting conversion:", encoder.toHex(s.addr), s.parasite == 1 and "parasite" or " ")
|
2017-02-25 22:56:45 +01:00
|
|
|
ow.reset(pin)
|
|
|
|
ow.select(pin, s.addr) -- select the sensor
|
|
|
|
ow.write(pin, 0x44, 1) -- and start conversion
|
|
|
|
s.status = 1
|
|
|
|
if s.parasite == 1 then break end -- parasite sensor blocks bus during conversion
|
|
|
|
end
|
2015-01-26 11:16:55 +01:00
|
|
|
end
|
2017-02-25 22:56:45 +01:00
|
|
|
tmr.alarm(tmr.create(), 750, tmr.ALARM_SINGLE, function() self:readout() end)
|
|
|
|
end,
|
2017-03-07 07:06:50 +01:00
|
|
|
|
2017-02-25 22:56:45 +01:00
|
|
|
readTemp = function(self, cb, lpin)
|
|
|
|
local pin = self.pin
|
|
|
|
self.cb = cb
|
|
|
|
self.temp={}
|
|
|
|
if lpin then pin = lpin end
|
|
|
|
ow.setup(pin)
|
2017-03-07 07:06:50 +01:00
|
|
|
|
2017-02-25 22:56:45 +01:00
|
|
|
self.sens={}
|
2015-01-26 11:16:55 +01:00
|
|
|
ow.reset_search(pin)
|
2017-02-25 22:56:45 +01:00
|
|
|
-- ow.target_search(pin,0x28)
|
|
|
|
-- search the first device
|
|
|
|
local addr = ow.search(pin)
|
|
|
|
-- and loop through all devices
|
|
|
|
while addr do
|
|
|
|
-- search next device
|
|
|
|
local crc=ow.crc8(string.sub(addr,1,7))
|
|
|
|
if (crc==addr:byte(8)) and ((addr:byte(1)==0x10) or (addr:byte(1)==0x28)) then
|
|
|
|
ow.reset(pin)
|
|
|
|
ow.select(pin, addr) -- select the found sensor
|
|
|
|
ow.write(pin, 0xB4, 1) -- Read Power Supply [B4h]
|
|
|
|
local parasite = (ow.read(pin)==0 and 1 or 0)
|
|
|
|
table.insert(self.sens,{addr=addr, parasite=parasite, status=0})
|
2017-03-07 07:06:50 +01:00
|
|
|
print("contact: ", encoder.toHex(addr), parasite == 1 and "parasite" or " ")
|
2015-01-26 11:16:55 +01:00
|
|
|
end
|
2015-11-16 13:32:20 +01:00
|
|
|
|
2017-02-25 22:56:45 +01:00
|
|
|
addr = ow.search(pin)
|
|
|
|
tmr.wdclr()
|
|
|
|
end
|
2017-03-07 07:06:50 +01:00
|
|
|
|
2017-02-25 22:56:45 +01:00
|
|
|
-- place powered sensors first
|
|
|
|
table.sort(self.sens, function(a,b) return a.parasite<b.parasite end)
|
2017-03-07 07:06:50 +01:00
|
|
|
|
2017-02-25 22:56:45 +01:00
|
|
|
node.task.post(node.task.MEDIUM_PRIORITY, function() self:conversion() end)
|
|
|
|
end,
|
2017-03-07 07:06:50 +01:00
|
|
|
|
2017-02-25 22:56:45 +01:00
|
|
|
readout=function(self)
|
|
|
|
local pin = self.pin
|
|
|
|
local next = false
|
|
|
|
if not self.sens then return 0 end
|
|
|
|
for i,s in ipairs(self.sens) do
|
2017-03-07 07:06:50 +01:00
|
|
|
-- print(encoder.toHex(s.addr), s.status)
|
2017-02-25 22:56:45 +01:00
|
|
|
if s.status == 1 then
|
|
|
|
ow.reset(pin)
|
|
|
|
ow.select(pin, s.addr) -- select the sensor
|
|
|
|
ow.write(pin, 0xBE, 0) -- READ_SCRATCHPAD
|
|
|
|
data = ow.read_bytes(pin, 9)
|
2015-11-16 13:32:20 +01:00
|
|
|
|
2017-02-25 22:56:45 +01:00
|
|
|
local t=(data:byte(1)+data:byte(2)*256)
|
|
|
|
if (t > 0x7fff) then t = t - 0x10000 end
|
|
|
|
if (s.addr:byte(1) == 0x28) then
|
|
|
|
t = t * 625 -- DS18B20, 4 fractional bits
|
2015-01-26 11:16:55 +01:00
|
|
|
else
|
2017-02-25 22:56:45 +01:00
|
|
|
t = t * 5000 -- DS18S20, 1 fractional bit
|
2014-12-08 09:22:38 +01:00
|
|
|
end
|
2017-03-07 07:06:50 +01:00
|
|
|
|
2017-02-25 22:56:45 +01:00
|
|
|
-- integer version
|
|
|
|
local sgn = t<0 and -1 or 1
|
|
|
|
local tA = sgn*t
|
|
|
|
local tH=tA/10000
|
|
|
|
local tL=(tA%10000)/1000 + ((tA%1000)/100 >= 5 and 1 or 0)
|
2017-03-07 07:06:50 +01:00
|
|
|
|
2017-02-25 22:56:45 +01:00
|
|
|
if tH and (tH~=85) then
|
|
|
|
self.temp[s.addr]=(sgn<0 and "-" or "")..tH.."."..tL
|
2017-03-07 07:06:50 +01:00
|
|
|
print(encoder.toHex(s.addr),(sgn<0 and "-" or "")..tH.."."..tL)
|
2017-02-25 22:56:45 +01:00
|
|
|
s.status = 2
|
|
|
|
end
|
|
|
|
-- end integer version
|
|
|
|
-- -- float version
|
|
|
|
-- if t and (math.floor(t/10000)~=85) then
|
|
|
|
-- self.temp[s.addr]=t
|
2017-03-07 07:06:50 +01:00
|
|
|
-- print(encoder.toHex(s.addr), t)
|
2017-02-25 22:56:45 +01:00
|
|
|
-- s.status = 2
|
|
|
|
-- end
|
|
|
|
-- -- end float version
|
2015-01-26 11:16:55 +01:00
|
|
|
end
|
2017-02-25 22:56:45 +01:00
|
|
|
next = next or s.status == 0
|
|
|
|
end
|
2017-03-07 07:06:50 +01:00
|
|
|
if next then
|
2017-02-25 22:56:45 +01:00
|
|
|
node.task.post(node.task.MEDIUM_PRIORITY, function() self:conversion() end)
|
2014-12-08 09:22:38 +01:00
|
|
|
else
|
2017-02-25 22:56:45 +01:00
|
|
|
self.sens = nil
|
2017-03-07 07:06:50 +01:00
|
|
|
if self.cb then
|
|
|
|
node.task.post(node.task.MEDIUM_PRIORITY, function() self.cb(self.temp) end)
|
2017-02-25 22:56:45 +01:00
|
|
|
end
|
2014-12-08 09:22:38 +01:00
|
|
|
end
|
2017-03-07 07:06:50 +01:00
|
|
|
|
2015-01-26 11:16:55 +01:00
|
|
|
end
|
2017-02-25 22:56:45 +01:00
|
|
|
})
|