-->
Page 1 of 1

Multiple DS18B20 issue

PostPosted: Thu Jun 16, 2016 2:45 pm
by fahimhaider
Dear ALL
i want to read 2--4 ds18b20 but get only one ds18b20 value. Below are the lua module i load. Please help me to reolve this issue
local modname = ...
local M = {}
_G[modname] = M
--------------------------------------------------------------------------------
-- Local used variables
--------------------------------------------------------------------------------
-- DS18B20 dq pin
local pin = nil
-- DS18B20 default pin
local defaultPin = 1
--------------------------------------------------------------------------------
-- 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
function setup(dq)
pin = dq
if(pin == nil) then
pin = defaultPin
end
ow.setup(pin)
end

function addrs()
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
end

function readNumber(addr, unit)
result = nil
setup(pin)
flag = false
if(addr == nil) then
ow.reset_search(pin)
count = 0
repeat
count = count + 1
addr = ow.search(pin)
tmr.wdclr()
until((addr ~= nil) or (count > 100))
ow.reset_search(pin)
end
if(addr == nil) then
return result
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
-- 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
t = (data:byte(1) + data:byte(2) * 256)
if (t > 32767) then
t = t - 65536
end

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
-- else
-- return nil
end
t = t / 10000
return t
end
tmr.wdclr()
else
-- print("Device family is not recognized.")
end
else
-- print("CRC is not valid!")
end
return result
end

function read(addr, unit)
t = readNumber(addr, unit)
if (t == nil) then
return nil
else
return t
end
end

-- Return module table
return M
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
And below are the reading code of ds
///////////////////////////////////////////////////////////////////////////////////
t=require("ds18b20_1")
pin = 1
t.setup(pin)
addrs = t.addrs()
if (addrs ~= nil) then
print("Total DS18B20 sensors: "..table.getn(addrs))
end

print("Temperature: "..t.read().."'C")
if (table.getn(addrs) >= 2) then
print("Second sensor: "..t.read(addrs[2],t.C).."'C")
end
t = nil
ds18b20 = nil
package.loaded["ds18b20_1"]=nil
/////////////////////////////////////////////////////////////////////////////////////////////////////////

Re: Multiple DS18B20 issue

PostPosted: Thu Jun 16, 2016 5:48 pm
by picstart
OK, I didn't look into the above code too deeply. For years ago I recall the one wire protocol can discover a single one wire device from among many. To discover all devices requires an entirely different routine. It was called discovery. To discover all devices the code needs to be written as either recursive or pseudo recursive. The devices sit on a binary tree and a single device is discovered by traversing the tree with a preference to go either left or right at each junction.Any device will at each branch either be a zero or a one. If there is more than one device then at some branch both a zero from at least one devices or a one from one from at least one device must occur. The recursive routine backs up and takes the 0 branch if it took the 1 branch backing up and down the tree until all branches are taken discovering every device that way.

Re: Multiple DS18B20 issue

PostPosted: Fri Jun 17, 2016 7:29 am
by xtal
Havn't looked to much at your code, but did notice you wre playing with some data bits.
When I get the raw data temperature
1> Save sign bit , if neg do 2's comp
2> Multiply by [I think .0625 see data sheet] to get Celcius degrees m then apply sign bit...

Bit banging is difficult, You might want to look into LinkUSB adapter [about $30 us]
It uses serial commands, the bit banging is done in the adapter.....

To see all the DS18B20's you must do a 1-wire search ......
You may also to build a 1-wire sniffer , its bailed me out on 1-wire slave's I made...

Re: Multiple DS18B20 issue

PostPosted: Mon Jul 25, 2016 11:54 am
by Jeepers01
I have 6x ds18b20 sensors working and posted the code here
http://www.esp8266.com/viewtopic.php?f=6&t=8066&p=40807#p40807

Hope this helps