NodeJS sensor scripts running but no output


#1

Hi,

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

Graham


#2

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.

-Karan


#3

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 (https://github.com/DexterInd/GrovePi/blob/master/Software/NodeJS/libs/sensors/DHTDigitalSensor.js) 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) {
DigitalSensor.apply(this, Array.prototype.slice.call(arguments))
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) {
// http://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml
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’

DHTDigitalSensor.prototype.read = function() {
var write = this.board.writeBytes(commands.dht_temp.concat([this.pin, this.moduleType, commands.unused]))
if (write) {
this.board.wait(500)
this.board.readByte()
this.board.wait(200)
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: https://github.com/adafruit/DHT-sensor-library/blob/master/DHT.cpp

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

} else {
return false
}
}

module.exports = DHTDigitalSensor


#4

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.

John


#5

Hi John

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

Many thanks

Graham


#6

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?

thanks
Paul