2014-12-08 09:22:38 +01:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- DS18B20 one wire module for NODEMCU
|
|
|
|
-- NODEMCU TEAM
|
|
|
|
-- LICENCE: http://opensource.org/licenses/MIT
|
|
|
|
-- Vowstar <vowstar@nodemcu.com>
|
2015-02-14 10:48:41 +01:00
|
|
|
-- 2015/02/14 sza2 <sza2trash@gmail.com> Fix for negative values
|
2014-12-08 09:22:38 +01:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- Set module name as parameter of require
|
|
|
|
local modname = ...
|
|
|
|
local M = {}
|
|
|
|
_G[modname] = M
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Local used variables
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- DS18B20 dq pin
|
|
|
|
local pin = nil
|
|
|
|
-- DS18B20 default pin
|
|
|
|
local defaultPin = 9
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Local used modules
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Table module
|
|
|
|
local table = table
|
|
|
|
-- String module
|
|
|
|
local string = string
|
|
|
|
-- One wire module
|
|
|
|
local ow = ow
|
|
|
|
-- Timer module
|
|
|
|
local tmr = tmr
|
|
|
|
-- Limited to local environment
|
|
|
|
setfenv(1,M)
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Implementation
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
C = 0
|
|
|
|
F = 1
|
|
|
|
K = 2
|
2015-01-26 11:16:55 +01:00
|
|
|
function setup(dq)
|
|
|
|
pin = dq
|
|
|
|
if(pin == nil) then
|
|
|
|
pin = defaultPin
|
|
|
|
end
|
|
|
|
ow.setup(pin)
|
2014-12-08 09:22:38 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function addrs()
|
2015-01-26 11:16:55 +01:00
|
|
|
setup(pin)
|
|
|
|
tbl = {}
|
|
|
|
ow.reset_search(pin)
|
|
|
|
repeat
|
|
|
|
addr = ow.search(pin)
|
|
|
|
if(addr ~= nil) then
|
|
|
|
table.insert(tbl, addr)
|
|
|
|
end
|
|
|
|
tmr.wdclr()
|
|
|
|
until (addr == nil)
|
|
|
|
ow.reset_search(pin)
|
|
|
|
return tbl
|
2014-12-08 09:22:38 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function readNumber(addr, unit)
|
|
|
|
result = nil
|
|
|
|
setup(pin)
|
|
|
|
flag = false
|
|
|
|
if(addr == nil) then
|
|
|
|
ow.reset_search(pin)
|
2015-01-26 11:16:55 +01:00
|
|
|
count = 0
|
2014-12-08 09:22:38 +01:00
|
|
|
repeat
|
2015-01-26 11:16:55 +01:00
|
|
|
count = count + 1
|
|
|
|
addr = ow.search(pin)
|
|
|
|
tmr.wdclr()
|
|
|
|
until((addr ~= nil) or (count > 100))
|
|
|
|
ow.reset_search(pin)
|
|
|
|
end
|
2014-12-08 09:22:38 +01:00
|
|
|
if(addr == nil) then
|
2015-01-26 11:16:55 +01:00
|
|
|
return result
|
2014-12-08 09:22:38 +01:00
|
|
|
end
|
|
|
|
crc = ow.crc8(string.sub(addr,1,7))
|
|
|
|
if (crc == addr:byte(8)) then
|
|
|
|
if ((addr:byte(1) == 0x10) or (addr:byte(1) == 0x28)) then
|
2015-01-26 11:16:55 +01:00
|
|
|
-- print("Device is a DS18S20 family device.")
|
|
|
|
ow.reset(pin)
|
|
|
|
ow.select(pin, addr)
|
|
|
|
ow.write(pin, 0x44, 1)
|
|
|
|
-- tmr.delay(1000000)
|
|
|
|
present = ow.reset(pin)
|
|
|
|
ow.select(pin, addr)
|
|
|
|
ow.write(pin,0xBE,1)
|
|
|
|
-- print("P="..present)
|
|
|
|
data = nil
|
|
|
|
data = string.char(ow.read(pin))
|
|
|
|
for i = 1, 8 do
|
|
|
|
data = data .. string.char(ow.read(pin))
|
|
|
|
end
|
|
|
|
-- print(data:byte(1,9))
|
|
|
|
crc = ow.crc8(string.sub(data,1,8))
|
|
|
|
-- print("CRC="..crc)
|
|
|
|
if (crc == data:byte(9)) then
|
2015-02-14 10:48:41 +01:00
|
|
|
t = (data:byte(1) + data:byte(2) * 256)
|
|
|
|
if (t > 32767) then
|
2015-02-21 17:05:16 +01:00
|
|
|
t = t - 65536
|
2015-02-14 10:48:41 +01:00
|
|
|
end
|
2015-11-16 13:32:20 +01:00
|
|
|
|
|
|
|
if (addr:byte(1) == 0x28) then
|
|
|
|
t = t * 625 -- DS18B20, 4 fractional bits
|
|
|
|
else
|
|
|
|
t = t * 5000 -- DS18S20, 1 fractional bit
|
|
|
|
end
|
|
|
|
|
|
|
|
if(unit == nil or unit == 'C') then
|
|
|
|
-- do nothing
|
|
|
|
elseif(unit == 'F') then
|
|
|
|
t = t * 1.8 + 320000
|
|
|
|
elseif(unit == 'K') then
|
|
|
|
t = t + 2731500
|
2015-01-26 11:16:55 +01:00
|
|
|
else
|
|
|
|
return nil
|
2014-12-08 09:22:38 +01:00
|
|
|
end
|
2015-02-01 18:20:45 +01:00
|
|
|
t = t / 10000
|
|
|
|
return t
|
2015-01-26 11:16:55 +01:00
|
|
|
end
|
|
|
|
tmr.wdclr()
|
2014-12-08 09:22:38 +01:00
|
|
|
else
|
2015-01-26 11:16:55 +01:00
|
|
|
-- print("Device family is not recognized.")
|
2014-12-08 09:22:38 +01:00
|
|
|
end
|
|
|
|
else
|
2015-01-26 11:16:55 +01:00
|
|
|
-- print("CRC is not valid!")
|
2014-12-08 09:22:38 +01:00
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
function read(addr, unit)
|
2015-02-01 18:20:45 +01:00
|
|
|
t = readNumber(addr, unit)
|
|
|
|
if (t == nil) then
|
2015-01-26 11:16:55 +01:00
|
|
|
return nil
|
|
|
|
else
|
2015-02-01 18:20:45 +01:00
|
|
|
return t
|
2015-01-26 11:16:55 +01:00
|
|
|
end
|
2014-12-08 09:22:38 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Return module table
|
2015-01-26 11:16:55 +01:00
|
|
|
return M
|