A node.JS implementation for using the DS1820, DS18S20 and/or DS18B20 temperature sensor with Raspberry Pi.
This package is to be used by Raspberry Pi installed with node.JS to interact with the awesome temperature sensors DS1820, DS18S20 and DS18B20 made by Maxim integrated.
At time of writing only the DS18B20 sensor have been tested out with this package. Please tell me if you get the chance to test it with the other sensors.
$ npm install ds18x20
Each function in this library can be used sync or async. The same functions are used. The only difference is that if the last argument have a callback function, then it's an async function. There will be examples below for each function, both as sync and async.
var sensor = require('ds18x20');
To be able to probe the sensors, the temperature driver needs to be loaded in your Raspberry pi (done with the command sudo modprobe w1-gpio && sudo modprobe w1-therm
).
sensor.isDriverLoaded(function (err, isLoaded) {
console.log(isLoaded);
});
var isLoaded = sensor.isDriverLoaded();
console.log(isLoaded);
true / false
Should the driver not be loaded you can use the command below. However, to run this command you need to be root (sudo node
).
Instead of loading the driver through node, I recommend loading the driver at startup through shell script: sudo modprobe w1-gpio && sudo modprobe w1-therm
.
sensor.loadDriver(function (err) {
if (err) console.log('something went wrong loading the driver:', err)
else console.log('driver is loaded');
});
try {
sensor.loadDriver();
console.log('driver is loaded');
} catch (err) {
console.log('something went wrong loading the driver:', err)
}
sensor.list(function (err, listOfDeviceIds) {
console.log(listOfDeviceIds);
});
var listOfDeviceIds = sensor.list();
console.log(listOfDeviceIds);
[ '28-00000574c791', '28-00000574f4f3' ]
The temperature is requested from each sensor. The result is returned in Celsius degrees and rounded to in decimal place.
sensor.getAll(function (err, tempObj) {
console.log(tempObj);
});
var tempObj = sensor.getAll();
console.log(tempObj);
Temperature in x° C
{ '28-00000574c791': 22.9, '28-00000574f4f3': 22.8 }
The result is returned in Celsius degrees and rounded to in decimal place.
sensor.get('28-00000574c791', function (err, temp) {
console.log(temp);
});
var temp = sensor.get('28-00000574c791');
var listOfTemps = sensor.get(['28-00000574c791', '28-00000574f4f3']);
console.log(temp);
console.log(listOfTemps);
Temperature in x° C
23.1
[ 23.1, 23.2 ]