Add CRC check (#2992)

Also clean-up a nasty `ow` module example.
This commit is contained in:
Lukáš Voborský 2020-01-02 21:38:22 +01:00 committed by Marcel Stör
parent 27e127e7ea
commit cd9da6463c
2 changed files with 44 additions and 46 deletions

View File

@ -145,48 +145,42 @@ Issues a 1-Wire rom select command. Make sure you do the `ow.reset(pin)` first.
#### Example #### Example
```lua ```lua
-- 18b20 Example -- 18b20 Example
pin = 9 -- 18b20 Example
pin = 3
ow.setup(pin) ow.setup(pin)
count = 0 addr = ow.reset_search(pin)
repeat addr = ow.search(pin)
count = count + 1
addr = ow.reset_search(pin)
addr = ow.search(pin)
tmr.wdclr()
until (addr ~= nil) or (count > 100)
if addr == nil then if addr == nil then
print("No more addresses.") print("No device detected.")
else else
print(addr:byte(1,8)) print(addr:byte(1,8))
crc = ow.crc8(string.sub(addr,1,7)) local crc = ow.crc8(string.sub(addr,1,7))
if crc == addr:byte(8) then if crc == addr:byte(8) then
if (addr:byte(1) == 0x10) or (addr:byte(1) == 0x28) then if (addr:byte(1) == 0x10) or (addr:byte(1) == 0x28) then
print("Device is a DS18S20 family device.") print("Device is a DS18S20 family device.")
repeat tmr.create():alarm(1000, tmr.ALARM_AUTO, function()
ow.reset(pin) ow.reset(pin)
ow.select(pin, addr) ow.select(pin, addr)
ow.write(pin, 0x44, 1) ow.write(pin, 0x44, 1) -- convert T command
tmr.delay(1000000) tmr.create():alarm(750, tmr.ALARM_SINGLE, function()
present = ow.reset(pin) ow.reset(pin)
ow.select(pin, addr) ow.select(pin, addr)
ow.write(pin,0xBE,1) ow.write(pin,0xBE,1) -- read scratchpad command
print("P="..present) local data = ow.read_bytes(pin, 9)
data = nil print(data:byte(1,9))
data = string.char(ow.read(pin)) local crc = ow.crc8(string.sub(data,1,8))
for i = 1, 8 do print("CRC="..crc)
data = data .. string.char(ow.read(pin)) if crc == data:byte(9) then
end local t = (data:byte(1) + data:byte(2) * 256) * 625
print(data:byte(1,9)) local sgn = t<0 and -1 or 1
crc = ow.crc8(string.sub(data,1,8)) local tA = sgn*t
print("CRC="..crc) local t1 = math.floor(tA / 10000)
if crc == data:byte(9) then local t2 = tA % 10000
t = (data:byte(1) + data:byte(2) * 256) * 625 print("Temperature="..(sgn<0 and "-" or "")..t1.."."..t2.." Centigrade")
t1 = t / 10000 end
t2 = t % 10000 end)
print("Temperature="..t1.."."..t2.."Centigrade") end)
end
tmr.wdclr()
until false
else else
print("Device family is not recognized.") print("Device family is not recognized.")
end end

View File

@ -69,6 +69,7 @@ local function readout(self)
-- the DS18B20 family has 4 fractional bits and the DS18S20s, 1 fractional bit -- the DS18B20 family has 4 fractional bits and the DS18S20s, 1 fractional bit
t = ((t <= 32767) and t or t - 65536) * t = ((t <= 32767) and t or t - 65536) *
((addr:byte(1) == DS18B20FAMILY) and 625 or 5000) ((addr:byte(1) == DS18B20FAMILY) and 625 or 5000)
local crc, b9 = ow_crc8(string.sub(data,1,8)), data:byte(9)
if 1/2 == 0 then if 1/2 == 0 then
-- integer version -- integer version
@ -83,22 +84,22 @@ local function readout(self)
local tL=(tA%10000)/1000 + ((tA%1000)/100 >= 5 and 1 or 0) local tL=(tA%10000)/1000 + ((tA%1000)/100 >= 5 and 1 or 0)
if tH and (t~=850000) then if tH and (t~=850000) then
temp[addr]=(sgn<0 and "-" or "")..tH.."."..tL debugPrint(to_string(addr),(sgn<0 and "-" or "")..tH.."."..tL, crc, b9)
debugPrint(to_string(addr),(sgn<0 and "-" or "")..tH.."."..tL) if crc==b9 then temp[addr]=(sgn<0 and "-" or "")..tH.."."..tL end
status[i] = 2 status[i] = 2
end end
-- end integer version -- end integer version
else else
-- float version -- float version
if t and (math_floor(t/10000)~=85) then t = t / 10000
t = t / 10000 if math_floor(t)~=85 then
if unit == 'F' then if unit == 'F' then
t = t * 18/10 + 32 t = t * 18/10 + 32
elseif unit == 'K' then elseif unit == 'K' then
t = t + 27315/100 t = t + 27315/100
end end
self.temp[addr]=t debugPrint(to_string(addr), t, crc, b9)
debugPrint(to_string(addr), t) if crc==b9 then temp[addr]=t end
status[i] = 2 status[i] = 2
end end
-- end float version -- end float version
@ -123,19 +124,22 @@ conversion = (function (self)
if powered_only then if powered_only then
debugPrint("starting conversion: all sensors") debugPrint("starting conversion: all sensors")
ow_reset(pin) ow_reset(pin)
ow_skip(pin) -- select the sensor ow_skip(pin) -- skip ROM selection, talk to all sensors
ow_write(pin, CONVERT_T, MODE) -- and start conversion ow_write(pin, CONVERT_T, MODE) -- and start conversion
for i, _ in ipairs(sens) do status[i] = 1 end for i, _ in ipairs(sens) do status[i] = 1 end
else else
local started = false
for i, s in ipairs(sens) do for i, s in ipairs(sens) do
if status[i] == 0 then if status[i] == 0 then
local addr, parasite = s:sub(1,8), s:byte(9) local addr, parasite = s:sub(1,8), s:byte(9) == 1
debugPrint("starting conversion:", to_string(addr), parasite == 1 and "parasite" or " ") if parasite and started then break end -- do not start concurrent conversion of powered and parasite
debugPrint("starting conversion:", to_string(addr), parasite and "parasite" or "")
ow_reset(pin) ow_reset(pin)
ow_select(pin, addr) -- select the sensor ow_select(pin, addr) -- select the sensor
ow_write(pin, CONVERT_T, MODE) -- and start conversion ow_write(pin, CONVERT_T, MODE) -- and start conversion
status[i] = 1 status[i] = 1
if parasite == 1 then break end -- parasite sensor blocks bus during conversion if parasite then break end -- parasite sensor blocks bus during conversion
started = true
end end
end end
end end
@ -169,7 +173,6 @@ local function _search(self, lcb, lpin, search, save)
for i, _ in ipairs(sens) do status[i] = 0 end for i, _ in ipairs(sens) do status[i] = 0 end
end end
local function cycle() local function cycle()
debugPrint("cycle")
if addr then if addr then
local crc=ow_crc8(addr:sub(1,7)) local crc=ow_crc8(addr:sub(1,7))
if (crc==addr:byte(8)) and ((addr:byte(1)==DS1920FAMILY) or (addr:byte(1)==DS18B20FAMILY)) then if (crc==addr:byte(8)) and ((addr:byte(1)==DS1920FAMILY) or (addr:byte(1)==DS18B20FAMILY)) then
@ -177,8 +180,9 @@ local function _search(self, lcb, lpin, search, save)
ow_select(pin, addr) ow_select(pin, addr)
ow_write(pin, READ_POWERSUPPLY, MODE) ow_write(pin, READ_POWERSUPPLY, MODE)
local parasite = (ow_read(pin)==0 and 1 or 0) local parasite = (ow_read(pin)==0 and 1 or 0)
sens[#sens+1]= addr..string_char(parasite) -- {addr=addr, parasite=parasite, status=0} sens[#sens+1]= addr..string_char(parasite)
debugPrint("contact: ", to_string(addr), parasite == 1 and "parasite" or " ") status[#sens] = 0
debugPrint("contact: ", to_string(addr), parasite == 1 and "parasite" or "")
end end
addr = ow_search(pin) addr = ow_search(pin)
node_task_post(node_task_LOW_PRIORITY, cycle) node_task_post(node_task_LOW_PRIORITY, cycle)