NodeJS Blynk - Code Examples for Basic Tasks (Work in Progress)

10 - Reading and displaying Values from a DHT Sensor

Uses this library…

var sensorLib = require('node-dht-sensor');

// 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(29, readout.temperature.toFixed(0));
  blynk.virtualWrite(30, readout.humidity.toFixed(0));
  console.log('Temperature:', readout.temperature.toFixed(1) + 'C');
  console.log('Humidity:   ', readout.humidity.toFixed(1)    + '%');
}, 2000);