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.
## read_temp()
Scans the bus for DS18B20 sensors (optional), 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 also module uses `encoder` module for printing debug information with more readable representation of sensor address (`encoder.toHex()`).
The module requires `ow` module.
#### Syntax
#### Syntax
`readTemp(callback, pin)`
`read_temp(callback, pin, unit, force_search, save_search)`
#### Parameters
#### Parameters
-`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).
-`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.
-`unit` unit can be Celsius ("C" or ds18b20.C), Kelvin ("K" or ds18b20.K) or Fahrenheit ("F" or ds18b20.F). If not specified (nil) latest used unit is used.
-`force_search` if not nil a bus search for devices is performed before readout. If nil the existing list of sensors in memory is used. If the bus has not been searched yet the search performed as well.
-`save_search` if not nil found sensors are saved to the file `ds18b20_save.lc`. When `read_temp` is called, list of sensors in memory is empty and file `ds18b20_save.lc` is present then sensor addresses are loaded from file - usefull when running from batteries & deepsleep - immediate readout is performed (no bus scan).
#### Returns
#### Returns
nil
nil
#### Example
#### Example
```lua
```lua
t=require("ds18b20")
localt=require("ds18b20")
pin=3-- gpio0 = 3, gpio2 = 4
localpin=3-- gpio0 = 3, gpio2 = 4
functionreadout(temp)
localfunctionreadout(temp)
ift.sensthen
print("Total number of DS18B20 sensors: "..#t.sens)
-- Module can be released when it is no longer needed
-- Module can be released when it is no longer needed
...
@@ -42,13 +51,15 @@ function readout(temp)
...
@@ -42,13 +51,15 @@ function readout(temp)
package.loaded["ds18b20"]=nil
package.loaded["ds18b20"]=nil
end
end
-- t:readTemp(readout) -- default pin value is 3
t:read_temp(readout,pin,t.C)```
t:readTemp(readout,pin)
ift.sensthen
##enable_debug()
print("Total number of DS18B20 sensors: "..table.getn(t.sens))
Enablesdebugoutputofthemodule.
fori,sinipairs(t.sens)do
-- print(string.format(" sensor #%d address: %s%s", i, s.addr, s.parasite == 1 and " (parasite)" or ""))
#Properties
print(string.format(" sensor #%d address: %s%s",i,encoder.toHex(s.addr),s.parasite==1and" (parasite)"or""))-- readable address with Hex encoding is preferred when encoder module is available
print(string.format("Sensor %s: %s °C",encoder.toHex(addr),temp))-- readable address with base64 encoding is preferred when encoder module is available
end
end
-- Module can be released when it is no longer needed
-- Module can be released when it is no longer needed
print("Total number of DS18B20 sensors: "..table.getn(t.sens))
print("first call, no addresses in flash, search is performed")
fori,sinipairs(t.sens)do
t:read_temp(readout,pin,t.C)
-- 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.toHex(s.addr),s.parasite==1and" (parasite)"or""))-- 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