node-dht-sensor
A simple node.js module for reading temperature and relative humidity using a compatible DHT sensor.
Installation
$ npm install node-dht-sensor
Please note that differently from versions 0.0.x there's no need to pre-install the BCM2835 library 2.
Usage
To initialize the sensor, you have to specify the sensor type and the GPIO pin where the sensor is connected to. It should work for DHT11, DHT22 and AM2302 sensors.
You should use sensorType value to match the sensor as follows:
Sensor | sensorType value |
---|---|
DHT11 | 11 |
DHT22 or AM2302 | 22 |
If the initialization succeeds when you can call the read function to obtain the latest readout from the sensor. Readout values contains a temperature and a humidity property.
First Example
This sample queries a DHT11 sensor connected to the GPIO 4 and prints out the result on the console.
var sensor = ; sensor;
Multiple Sensors Example
The following example shows a method for querying multiple sensors connected to the same Raspberry Pi. For this example, we have two sensors:
- A DHT11 sensor connected to GPIO 17
- High-resolution DHT22 sensor connected to GPIO 4
var sensorLib = ; var app = sensors: name: "Indoor" type: 11 pin: 17 name: "Outdoor" type: 22 pin: 4 { for var sensor in thissensors var readout = sensorLib; console; ; }; app;
Promises API
Promises API provides an alternative read
method that returns a Promise object rather than using a callback. The API is accessible via require('node-dht-sensor').promises
.
var sensor = promises; // You can use `initialize` and `setMaxTries` just like beforesensor;sensor; // You can still use the synchronous version of `read`:// var readout = sensor.readSync(22, 4); sensor;
Using async/await
:
const sensor = promises; { try const res = await sensor; console; catch err console; } ;
Test mode
A test mode of operation is available since version 0.2.0
. In this mode of operation, the library does not communicate with the sensor hardware via the GPIO but instead it returns a pre-configured readout value. You can use the test mode during development without the need to have an actual sensor connected.
To enable the test mode, fake values must be defined at initialization. In the example below we specify fixed values for temperature equal to 21°C and humidity equal to 60%.
sensor;
After initialization, we can call the read
method as usual.
sensor;
And the result will always be the configured readout value defined at initialization.
$ node examples/fake-test.jstemp: 21.0°C, humidity: 60.0%$ node examples/fake-test.jstemp: 21.0°C, humidity: 60.0%
You can find a complete source code example in examples/fake-test.js.
Reference for building from source
Standard node-gyp commands are used to build the module. So, just make sure you have node and node-gyp as well as the Broadcom library to build the project.
-
In case, you don't have node-gyp, install it first:
$ sudo npm install -g node-gyp$ sudo update-alternatives --install /usr/bin/node-gyp node-gyp /opt/node-v10.15.3-linux-armv7l/bin/node-gyp 1 -
Generate the configuration files
$ node-gyp configure -
Build the component
$ node-gyp build
Tracing and Debugging
Verbose output from the module can be enabled by by specifying the --dht_verbose=true
flag when installing the node via npm.
$ npm install node-dht-sensor --dht_verbose=true
if you are interested in enabling trace when building directly from source you can enable the -Ddht_verbose
flag when running node-gyp configure.
$ node-gyp configure -- -Ddht_verbose=true
Appendix A: Quick Node.js installation guide
There are many ways you can get Node.js installed on your Raspberry Pi. Here is just one of way you can do it.
$ wget https://nodejs.org/dist/v10.15.3/node-v10.15.3-linux-armv7l.tar.xz$ tar xvfJ node-v10.15.3-linux-armv7l.tar.xz$ sudo mv node-v10.15.3-linux-armv7l /opt$ sudo update-alternatives --install /usr/bin/node node /opt/node-v10.15.3-linux-armv7l/bin/node 1$ sudo update-alternatives --set node /opt/node-v10.15.3-linux-armv7l/bin/node$ sudo update-alternatives --install /usr/bin/npm npm /opt/node-v10.15.3-linux-armv7l/bin/npm 1
Please note that you may have to use armv6l instead of arm7l if you have an early Raspberry Pi model.