Provides a high-level interface to a "1-wire" compatible temperature device or file on a Raspberry Pi.





Provides a high-level interface to a "1-wire"
compatible device or file on a Raspberry Pi.
This code was really only tested with a DS18B20 on a Raspberry Pi 3. I would
very much appreciate testers with different hardware to test and use this
module.
npm install raspi-1wire-temp
The idea with this approach is for R1WT to automatically determine what type of
temperature controller to create. Currently this project only supports the
output from a DS18B20 device. However if/when more temperature sensors are
tested they could be integrated into R1WT relatively easy.
```
const r1wt = require('raspi-1wire-temp');
const devices = r1wt.findDevices();
By default, R1WT looks for devices that can be globbed with
/sys/bus/w1/devices/28-*/w1_slave. However, findDevices() method has an
optional _hint_ glob string that can help R1WT find the device files.
``
const r1wt = require('raspi-1wire-temp');
const devices = r1wt.findDevices('/dev/my/temp/sensor*.dev');
The findDevices() method will return a non-null array of filenames to devices.
Once you obtain the filename of a device, either through findDevices() or fromfromDevice()
a known location, the method is used to create the temperature
controller.
`
const r1wt = require('raspi-1wire-temp');
const devices = r1wt.findDevices();
assert(devices.length > 0);
const controller = r1wt.fromDevice(devices[0]);
console.log(controller.current.celsius)
console.log(controller.current.fahrenheit)
`
For every call to controller.current the controller will re-read the device
for the current temperature. If reading from the device is an expensive call it
might be wise to cache the current temperature object for some time.
For some contexts, like testing, you might need a stubbed or emulated device
controller. The fromStream() method can be used to provide this
functionality. This may be particularly useful when developing software on
non-RPI hardware. Effectively allowing a temperature sensor to be emulated with
a known stream of data.
`
const r1wt = require('raspi-1wire-temp');
const controller = r1wt.fromStream(false, 1000, 2000, 3000)
assert(controller.current.celsius == 1000);
assert(controller.current.celsius == 2000);
assert(controller.current.celsius == 3000);
`
The first argument to fromStream() is a flag indicating if the stream shouldfalse
repeat. When set to the stream will raise an error when the data hastrue
been exhausted. To repeat the stream, set the flag to .
`
const r1wt = require('raspi-1wire-temp');
const controller = r1wt.fromStream(true, 1000, 2000, 3000)
assert(controller.current.celsius == 1000);
assert(controller.current.celsius == 2000);
assert(controller.current.celsius == 3000);
assert(controller.current.celsius == 1000);
assert(controller.current.celsius == 2000);
assert(controller.current.celsius == 3000);
// etcetera . . .
`
```
npm run test
npm run cover