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

安装npm及Bylnk库

 

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

安装传感器库

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

创建可执行文件

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

或者

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中的令牌。

sudo NODE_PATH =/usr/local/lib/node_modules node ./blynk-sensor-test.js

输出应如下所示

OnOff mode
Connecting to TCP: cloud.blynk.cc 8442
Connected
Temperature: 18.0C
Humidity: 26.0%
Temperature: 18.0C
Humidity: 26.0%

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

npm install -g pm2
sudo NODE_PATH=/usr/local/lib/node_modules pm2 start ./blynk-sensor-test.js
sudo pm2 save
sudo pm2 startup