NodeJS sensor scripts running but no output


Firstly I’m a GrovePi and NodeJS newbie and I’m trying to get the DHTDigitalSensor nodejs script to work. I have installed the Grove Pi libraries as per the configuration instructions and installed the npm module node-grovepi. I run the DHTDigitalSensor.js script, which runs but does not return anything and then returns to the pi command line almost instantly.

I have my temp / humidity sensor plugged in to the D7 port and I have tested it with the python script for the sensor, which does work so I know the sensor is functioning.

The reason that I want the nodejs version to work is that I want to send the data using the Bleno nodejs library to a Bluetooth Central device.

Any help with getting the nodejs program working would therefore be greatly appreciated!

Many thanks


Hey Graham,
Can you post the NodeJS code that you were using with the sensor. Do you have any other sensor that you have tried with the NodeJS.


Hi Karan

Thanks for the quick response (I’m not sure I received a notification email so apologies for my slow response). I’ve tried both the DHTDigitalSensor.js code and the AirQualityAnalogSensor.js from the DexterInd Github ( in order to see whether it was just the script for the DHTDigitalSensor was the issue. Both however exhibit the same behaviour - the script runs and then stops / returns to the command line without providing any data (or error messages).

Here is the code for the DHTDigitalSensor for completeness:

var DigitalSensor = require(’./base/digitalSensor’)
var commands = require(’…/commands’)

function DHTDigitalSensor(pin, moduleType, scale) {
this.moduleType = moduleType
this.scale = scale
function convertCtoF(temp) {
return temp * 9 / 5 + 32
function convertFtoC(temp) {
return (temp - 32) * 5 / 9
function getHeatIndex(temp, hum, scale) {
var needsConversion = typeof scale == ‘undefined’ || scale == DHTDigitalSensor.CELSIUS

temp = needsConversion ? convertCtoF(temp) : temp

var hi = -42.379 +
2.04901523 * temp +
10.14333127 * hum +
-0.22475541 * temp * hum +
-0.00683783 * Math.pow(temp, 2) +
-0.05481717 * Math.pow(hum, 2) +
0.00122874 * Math.pow(temp, 2) * hum +
0.00085282 * temp * Math.pow(hum, 2) +
-0.00000199 * Math.pow(temp, 2) * Math.pow(hum, 2)

return needsConversion ? convertFtoC(hi) : hi

DHTDigitalSensor.prototype = new DigitalSensor()
DHTDigitalSensor.VERSION = {
‘DHT11’ : 0
, ‘DHT22’ : 1
, ‘DHT21’ : 2
, ‘AM2301’: 3
DHTDigitalSensor.CELSIUS = 'c’
DHTDigitalSensor.FAHRENHEIT = ‘f’ = function() {
var write = this.board.writeBytes(commands.dht_temp.concat([, this.moduleType, commands.unused]))
if (write) {
var bytes = this.board.readBytes(9)
if (bytes instanceof Buffer) {
var hex
var tempBytes = bytes.slice(1, 5).reverse()
var humBytes = bytes.slice(5, 9).reverse()

  hex = '0x' + tempBytes.toString('hex')
  var temp = (hex & 0x7fffff | 0x800000) * 1.0 / Math.pow(2, 23) * Math.pow(2, ((hex >> 23 & 0xff) - 127))
  temp = +(Number(parseFloat(temp - 0.5).toFixed(2)))
  if (this.scale == DHTDigitalSensor.FAHRENHEIT) {
    temp = convertCtoF(temp)

  hex = '0x' + humBytes.toString('hex')
  var hum = (hex & 0x7fffff | 0x800000) * 1.0 / Math.pow(2, 23) * Math.pow(2, ((hex >> 23 & 0xff) - 127))
  hum = +(Number(parseFloat(hum - 2).toFixed(2)))

  var heatIndex = +(Number(parseFloat(getHeatIndex(temp, hum, this.scale)).toFixed(2)))
  // From:

  return [temp, hum, heatIndex]
} else
  return false

} else {
return false

module.exports = DHTDigitalSensor

Hi GrahamHorne, I just wanted to hop in here with a point: our NodeJS examples were developed by a community member and not by DI folks. I’ll reach out to the person that developed them to see if he can help; just wanted to let you know that we might not have an answer here. None of us are Node experts unfortunately. Really sorry about this, but we’ll do our best to get someone on it.


Hi John

I was just wondering if you had been able to find anyone that might be able to help?

Many thanks


Hi, I am not sure if this is still a relevant thread but just in case I thought I would respond.
I had the same issue and it turned out to be a permissions thing. I added my user to the i2c group which owns /dev/i2c-1 on my build. Soon as I did that and started a new session everything started working perfectly.

I only picked the permissions issue up because I decided to try one of the python projects from the Grovepi git repo. That came back with a permissions error… Perhaps its worth looking at the nodejs api and implementing the means to report similar?


1 Like