Update DS18b20 examples (#1851)
- Remove old non-async examples from `lua-examples`. - Rename `ds18b20.EN.md` to `README.md` - Change remaining `toBase64` calls to the more standard `toHex` call. - Fix some spelling and markdown formatting issues in README file. Addresses issue #1841
This commit is contained in:
parent
d53ac8db90
commit
784fefebe7
|
@ -1,76 +0,0 @@
|
||||||
--'
|
|
||||||
-- ds18b20 one wire example for NODEMCU (Integer firmware only)
|
|
||||||
-- NODEMCU TEAM
|
|
||||||
-- LICENCE: http://opensource.org/licenses/MIT
|
|
||||||
-- Vowstar <vowstar@nodemcu.com>
|
|
||||||
--'
|
|
||||||
|
|
||||||
pin = 9
|
|
||||||
ow.setup(pin)
|
|
||||||
count = 0
|
|
||||||
repeat
|
|
||||||
count = count + 1
|
|
||||||
addr = ow.reset_search(pin)
|
|
||||||
addr = ow.search(pin)
|
|
||||||
tmr.wdclr()
|
|
||||||
until((addr ~= nil) or (count > 100))
|
|
||||||
if (addr == nil) then
|
|
||||||
print("No more addresses.")
|
|
||||||
else
|
|
||||||
print(addr:byte(1,8))
|
|
||||||
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.")
|
|
||||||
repeat
|
|
||||||
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)
|
|
||||||
|
|
||||||
-- handle negative temperatures
|
|
||||||
if (t > 0x7fff) then
|
|
||||||
t = t - 0x10000
|
|
||||||
end
|
|
||||||
|
|
||||||
if (addr:byte(1) == 0x28) then
|
|
||||||
t = t * 625 -- DS18B20, 4 fractional bits
|
|
||||||
else
|
|
||||||
t = t * 5000 -- DS18S20, 1 fractional bit
|
|
||||||
end
|
|
||||||
|
|
||||||
local sign = ""
|
|
||||||
if (t < 0) then
|
|
||||||
sign = "-"
|
|
||||||
t = -1 * t
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Separate integral and decimal portions, for integer firmware only
|
|
||||||
local t1 = string.format("%d", t / 10000)
|
|
||||||
local t2 = string.format("%04u", t % 10000)
|
|
||||||
local temp = sign .. t1 .. "." .. t2
|
|
||||||
print("Temperature= " .. temp .. " Celsius")
|
|
||||||
end
|
|
||||||
tmr.wdclr()
|
|
||||||
until false
|
|
||||||
else
|
|
||||||
print("Device family is not recognized.")
|
|
||||||
end
|
|
||||||
else
|
|
||||||
print("CRC is not valid!")
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -1,73 +0,0 @@
|
||||||
------------------------------------------------------------------------------
|
|
||||||
-- DS18B20 query module
|
|
||||||
--
|
|
||||||
-- LICENCE: http://opensource.org/licenses/MIT
|
|
||||||
-- Vladimir Dronnikov <dronnikov@gmail.com>
|
|
||||||
--
|
|
||||||
-- Example:
|
|
||||||
-- dofile("ds18b20.lua").read(4, function(r) for k, v in pairs(r) do print(k, v) end end)
|
|
||||||
------------------------------------------------------------------------------
|
|
||||||
local M
|
|
||||||
do
|
|
||||||
local bit = bit
|
|
||||||
local format_addr = function(a)
|
|
||||||
return ("%02x-%02x%02x%02x%02x%02x%02x"):format(
|
|
||||||
a:byte(1),
|
|
||||||
a:byte(7), a:byte(6), a:byte(5),
|
|
||||||
a:byte(4), a:byte(3), a:byte(2)
|
|
||||||
)
|
|
||||||
end
|
|
||||||
local read = function(pin, cb, delay)
|
|
||||||
local ow = require("ow")
|
|
||||||
-- get list of relevant devices
|
|
||||||
local d = { }
|
|
||||||
ow.setup(pin)
|
|
||||||
ow.reset_search(pin)
|
|
||||||
while true do
|
|
||||||
tmr.wdclr()
|
|
||||||
local a = ow.search(pin)
|
|
||||||
if not a then break end
|
|
||||||
if ow.crc8(a) == 0 and
|
|
||||||
(a:byte(1) == 0x10 or a:byte(1) == 0x28)
|
|
||||||
then
|
|
||||||
d[#d + 1] = a
|
|
||||||
end
|
|
||||||
end
|
|
||||||
-- conversion command for all
|
|
||||||
ow.reset(pin)
|
|
||||||
ow.skip(pin)
|
|
||||||
ow.write(pin, 0x44, 1)
|
|
||||||
-- wait a bit
|
|
||||||
tmr.alarm(0, delay or 100, 0, function()
|
|
||||||
-- iterate over devices
|
|
||||||
local r = { }
|
|
||||||
for i = 1, #d do
|
|
||||||
tmr.wdclr()
|
|
||||||
-- read rom command
|
|
||||||
ow.reset(pin)
|
|
||||||
ow.select(pin, d[i])
|
|
||||||
ow.write(pin, 0xBE, 1)
|
|
||||||
-- read data
|
|
||||||
local x = ow.read_bytes(pin, 9)
|
|
||||||
if ow.crc8(x) == 0 then
|
|
||||||
local t = (x:byte(1) + x:byte(2) * 256)
|
|
||||||
-- negatives?
|
|
||||||
if bit.isset(t, 15) then t = 1 - bit.bxor(t, 0xffff) end
|
|
||||||
-- NB: temperature in Celsius * 10^4
|
|
||||||
t = t * 625
|
|
||||||
-- NB: due 850000 means bad pullup. ignore
|
|
||||||
if t ~= 850000 then
|
|
||||||
r[format_addr(d[i])] = t
|
|
||||||
end
|
|
||||||
d[i] = nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
cb(r)
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
-- expose
|
|
||||||
M = {
|
|
||||||
read = read,
|
|
||||||
}
|
|
||||||
end
|
|
||||||
return M
|
|
|
@ -11,17 +11,17 @@ package.loaded["ds18b20"]=nil
|
||||||
<a id="ds18b20_setup"></a>
|
<a id="ds18b20_setup"></a>
|
||||||
|
|
||||||
## readTemp()
|
## readTemp()
|
||||||
Scans the bus for DS18B20 sensors, starts a readout (conversion) for all sensors and calls a callback function when all temperatures are available. Powered sensors are read at once first. Parasit-powered sensors are read one by one. The first parasit-powered sensor is read together with all powered sensors.
|
Scans the bus for DS18B20 sensors, starts a readout (conversion) for all sensors and calls a callback function when all temperatures are available. Powered sensors are read at once first. Parasite-powered sensors are read one by one. The first parasite-powered sensor is read together with all powered sensors.
|
||||||
|
|
||||||
The module requires `ow` module.
|
The module requires `ow` module.
|
||||||
|
|
||||||
The also module uses `encoder` module for printing debug information with more readable representation of sensor address (`encoder.toBase64()`).
|
The also module uses `encoder` module for printing debug information with more readable representation of sensor address (`encoder.toHex()`).
|
||||||
|
|
||||||
#### Syntax
|
#### Syntax
|
||||||
`readTemp(callback, pin)`
|
`readTemp(callback, pin)`
|
||||||
|
|
||||||
#### Parameters
|
#### Parameters
|
||||||
- `callback` function that receives all results when all conversions finish. The callback funciton has one parameter - an array addressed by sensor addresses and a value of the temperature (string for integer version).
|
- `callback` function that receives all results when all conversions finish. The callback function has one parameter - an array addressed by sensor addresses and a value of the temperature (string for integer version).
|
||||||
- `pin` pin of the one-wire bus. If nil, GPIO0 (3) is used.
|
- `pin` pin of the one-wire bus. If nil, GPIO0 (3) is used.
|
||||||
|
|
||||||
#### Returns
|
#### Returns
|
||||||
|
@ -34,7 +34,7 @@ pin = 3 -- gpio0 = 3, gpio2 = 4
|
||||||
|
|
||||||
function readout(temp)
|
function readout(temp)
|
||||||
for addr, temp in pairs(temp) do
|
for addr, temp in pairs(temp) do
|
||||||
print(string.format("Sensor %s: %s 'C", encoder.toBase64(addr), temp))
|
print(string.format("Sensor %s: %s 'C", encoder.toHex(addr), temp))
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Module can be released when it is no longer needed
|
-- Module can be released when it is no longer needed
|
||||||
|
@ -48,7 +48,7 @@ if t.sens then
|
||||||
print("Total number of DS18B20 sensors: "..table.getn(t.sens))
|
print("Total number of DS18B20 sensors: "..table.getn(t.sens))
|
||||||
for i, s in ipairs(t.sens) do
|
for i, s in ipairs(t.sens) do
|
||||||
-- print(string.format(" sensor #%d address: %s%s", i, s.addr, s.parasite == 1 and " (parasite)" or ""))
|
-- print(string.format(" sensor #%d address: %s%s", i, s.addr, s.parasite == 1 and " (parasite)" or ""))
|
||||||
print(string.format(" sensor #%d address: %s%s", i, encoder.toBase64(s.addr), s.parasite == 1 and " (parasite)" or "")) -- readable address with base64 encoding is preferred when encoder module is available
|
print(string.format(" sensor #%d address: %s%s", i, encoder.toHex(s.addr), s.parasite == 1 and " (parasite)" or "")) -- readable address with Hex encoding is preferred when encoder module is available
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
```
|
```
|
|
@ -12,7 +12,7 @@ function readout(temp)
|
||||||
|
|
||||||
for addr, temp in pairs(temp) do
|
for addr, temp in pairs(temp) do
|
||||||
-- resp = resp .. string.format("Sensor %s: %s ℃</br>", addr, temp)
|
-- resp = resp .. string.format("Sensor %s: %s ℃</br>", addr, temp)
|
||||||
resp = resp .. string.format("Sensor %s: %s ℃</br>", encoder.toBase64(addr), temp) -- readable address with base64 encoding is preferred when encoder module is available
|
resp = resp .. string.format("Sensor %s: %s ℃</br>", encoder.toHex(addr), temp) -- readable address with base64 encoding is preferred when encoder module is available
|
||||||
end
|
end
|
||||||
|
|
||||||
resp = resp ..
|
resp = resp ..
|
||||||
|
|
Loading…
Reference in New Issue