# node-dht-sensor
A simple node.js module for reading temperature and relative humidity using a compatible DHT sensor.
![](https://github.com/momenso/node-dht-sensor/workflows/Node.js%20CI/badge.svg)
[![npm](https://img.shields.io/npm/v/node-dht-sensor.svg?label=npm%20package)](https://www.npmjs.com/package/node-dht-sensor)
[![npm](https://img.shields.io/npm/dm/node-dht-sensor.svg)](https://www.npmjs.com/package/node-dht-sensor)
[![LICENSE](https://img.shields.io/github/license/momenso/node-dht-sensor.svg)](https://github.com/momenso/node-dht-sensor/blob/master/LICENSE)
## Installation
```shell session
$ 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](https://www.raspberrypi.org/documentation/usage/gpio/) 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
![example1](https://cloud.githubusercontent.com/assets/420851/20246902/1a03bafc-a9a8-11e6-8158-d68928b2e79f.png)
This sample queries a DHT11 sensor connected to the GPIO 4 and prints out the result on the console.
```javascript
var sensor = require("node-dht-sensor");
sensor.read(11, 4, function(err, temperature, humidity) {
if (!err) {
console.log(`temp: ${temperature}°C, humidity: ${humidity}%`);
}
});
```
### Multiple Sensors Example
![example2](https://cloud.githubusercontent.com/assets/420851/20246914/554d72c4-a9a8-11e6-9162-ae51ecdf4212.png)
The following example shows a method for querying multiple sensors connected to the same Raspberry Pi. For this example, we have two sensors:
1. A DHT11 sensor connected to GPIO 17
2. High-resolution DHT22 sensor connected to GPIO 4
```javascript
var sensorLib = require("node-dht-sensor");
var app = {
sensors: [
{
name: "Indoor",
type: 11,
pin: 17
},
{
name: "Outdoor",
type: 22,
pin: 4
}
],
read: function() {
for (var sensor in this.sensors) {
var readout = sensorLib.read(
this.sensors[sensor].type,
this.sensors[sensor].pin
);
console.log(
`[${this.sensors[sensor].name}] ` +
`temperature: ${readout.temperature.toFixed(1)}°C, ` +
`humidity: ${readout.humidity.toFixed(1)}%`
);
}
setTimeout(function() {
app.read();
}, 2000);
}
};
app.read();
```
### 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`.
```javascript
var sensor = require("node-dht-sensor").promises;
// You can use `initialize` and `setMaxTries` just like before
sensor.setMaxRetries(10);
sensor.initialize(22, 17);
// You can still use the synchronous version of `read`:
// var readout = sensor.readSync(22, 4);
sensor.read(22, 17).then(
function(res) {
console.log(
`temp: ${res.temperature.toFixed(1)}°C, ` +
`humidity: ${res.humidity.toFixed(1)}%`
);
},
function(err) {
console.error("Failed to read sensor data:", err);
}
);
```
Using `async/await`:
```javascript
const sensor = require("node-dht-sensor").promises;
async function exec() {
try {
const res = await sensor.read(22, 4);
console.log(
`temp: ${res.temperature.toFixed(1)}°C, ` +
`humidity: ${res.humidity.toFixed(1)}%`
);
} catch (err) {
console.error("Failed to read sensor data:", err);
}
}
exec();
```
### 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%.
```javascript
sensor.initialize({
test: {
fake: {
temperature: 21,
humidity: 60
}
}
});
```
After initialization, we can call the `read` method as usual.
```javascript
sensor.read(22, 4, function(err, temperature, humidity) {
if (!err) {
console.log(
`temp: ${temperature.toFixed(1)}°C, ` +
`humidity: ${humidity.toFixed(1)}%`
);
}
});
```
And the result will always be the configured readout value defined at initialization.
```shell session
$ node examples/fake-test.js
temp: 21.0°C, humidity: 60.0%
$ node examples/fake-test.js
temp: 21.0°C, humidity: 60.0%
```
You can find a complete source code example in [examples/fake-test.js](https://github.com/momenso/node-dht-sensor/blob/master/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.
1. In case, you don't have node-gyp, install it first:
```shell session
$ 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
```
2. Generate the configuration files
```shell session
$ node-gyp configure
```
3. Build the component
```shell session
$ 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.
```shell session
$ 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.
```shell session
$ 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.
```shell session
$ wget https://nodejs.org/dist/v14.15.4/node-v14.15.4-linux-armv7l.tar.xz
$ tar xvfJ node-v14.15.4-linux-armv7l.tar.xz
$ sudo mv node-v14.15.4-linux-armv7l /opt
$ sudo update-alternatives --install /usr/bin/node node /opt/node-v14.15.4-linux-armv7l/bin/node 1
$ sudo update-alternatives --set node /opt/node-v14.15.4-linux-armv7l/bin/node
$ sudo update-alternatives --install /usr/bin/npm npm /opt/node-v14.15.4-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.
### References
[1]: Node.js download - https://nodejs.org/en/download/
[2]: BCM2835 - http://www.airspayce.com/mikem/bcm2835/
[3]: Node.js native addon build tool - https://github.com/TooTallNate/node-gyp
[4]: GPIO: Raspbery Pi Models A and B - https://www.raspberrypi.org/documentation/usage/gpio/
没有合适的资源?快使用搜索试试~ 我知道了~
Node.js 湿度和温度传感器插件_C语言_C++_JavaScript_代码_相关文件_下载
共24个文件
js:8个
h:3个
cpp:3个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 192 浏览量
2022-07-13
01:09:02
上传
评论
收藏 71KB ZIP 举报
温馨提示
一个简单的 node.js 模块,用于使用兼容的 DHT 传感器读取温度和相对湿度。'' 法 要初始化传感器,您必须指定传感器类型和传感器连接到的GPIO 引脚。它应该适用于 DHT11、DHT22 和 AM2302 传感器。 您应该使用 sensorType 值来匹配传感器,如下所示: 传感器 传感器类型值 DHT11 11 DHT22 或 AM2302 22 如果初始化成功,则可以调用 read 函数从传感器获取最新的读数。读数值包含温度和湿度属性。 第一个例子 更多详情、使用方法,请下载后细读README.md文件
资源推荐
资源详情
资源评论
收起资源包目录
node-dht-sensor-master.zip (24个子文件)
node-dht-sensor-master
.travis.yml 53B
package.json 904B
.github
workflows
nodejs.yml 1KB
test
node-dht-sensor.js 5KB
LICENSE 7KB
package-lock.json 57KB
src
node-dht-sensor.cpp 8KB
util.h 280B
util.cpp 291B
bcm2835
bcm2835.c 58KB
bcm2835.h 92KB
dht-sensor.cpp 6KB
dht-sensor.h 157B
examples
promisse.js 385B
fake-test.js 369B
async-explicit.js 1KB
sync-implicit.js 1KB
sync-explicit.js 1KB
duty.js 1KB
.gitignore 137B
lib
index.js 618B
README.md 7KB
.editorconfig 298B
binding.gyp 631B
共 24 条
- 1
资源评论
快撑死的鱼
- 粉丝: 1w+
- 资源: 9156
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功