树莓派lot=Node.js+Bylnk+DHT11/DHT22

安装npm及Bylnk库

 

1
2
3
4
5
sudo apt install npm -y
sudo apt-get install build-essential
sudo npm install -g npm
sudo npm install -g onoff
sudo npm install -g blynk-library

安装传感器库

1
2
3
4
5
6
7
8
wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.46.tar.gz
tar zxvf bcm2835-1.46.tar.gz
cd bcm2835-1.46
./configure
make
sudo make check
sudo make install
sudo npm install -g node-dht-sensor

连接传感器

  • GND至GND
  • DAT信号到GPIO7
  • VCC至3.3v

创建可执行文件

1
2
cd ~
git clone https://github.com/wanghurui/Blynk-dht11-22.git

或者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
var blynkLib = require('blynk-library');
var sensorLib = require('node-dht-sensor');

var AUTH = 'YOUR\_AUTH\_TOKEN';

// Setup Blynk
var blynk = new blynkLib.Blynk(AUTH);

// Setup sensor, exit if failed
var sensorType = 11; // 11 for DHT11, 22 for DHT22 and AM2302
var sensorPin = 4; // The GPIO pin number for sensor signal
if (!sensorLib.initialize(sensorType, sensorPin)) {
console.warn('Failed to initialize sensor');
process.exit(1);
}

// Automatically update sensor value every 2 seconds
setInterval(function() {
var readout = sensorLib.read();
blynk.virtualWrite(3, readout.temperature.toFixed(1));
blynk.virtualWrite(4, readout.humidity.toFixed(1));

console.log('Temperature:', readout.temperature.toFixed(1) + 'C');
console.log('Humidity: ', readout.humidity.toFixed(1) + '%');
}, 2000);

将**YOUR\_AUTH\_TOKEN**替换为App中的令牌。
1
sudo NODE\_PATH =/usr/local/lib/node\_modules node ./blynk-sensor-test.js

输出应如下所示

1
2
3
4
5
6
7
OnOff mode
Connecting to TCP: cloud.blynk.cc 8442
Connected
Temperature: 18.0C
Humidity: 26.0%
Temperature: 18.0C
Humidity:   26.0%

可以使用pm2将此程序放在后台守护进程并且开机自启动

1
2
3
4
npm install -g pm2
sudo NODE\_PATH=/usr/local/lib/node\_modules pm2 start ./blynk-sensor-test.js
sudo pm2 save
sudo pm2 startup

 

作者

椰椰雪球

发布于

2019-07-29

更新于

2022-04-05

许可协议

评论

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×