Working AirQuality and MotionSensor in nodejs


#1

Hello,

I have successfully added the AirQuality and Motion Sensor with nodejs and just want to share the code with you.

AirQuality:

var AnalogSensor = require('./base/analogSensor')
var commands     = require('../commands')

function AirQualityAnalogSensor(pin) {
  AnalogSensor.apply(this, Array.prototype.slice.call(arguments))
}
AirQualityAnalogSensor.prototype = new AnalogSensor()

AirQualityAnalogSensor.prototype.read = function() {
  this.board.pinMode(this.board.INPUT)
  var res = AnalogSensor.prototype.read.call(this)
  return parseInt(res)
}
module.exports = AirQualityAnalogSensor

MotionSensor:

var DigitalSensor = require('./base/digitalSensor')
var commands     = require('../commands')

function MotionDigitalSensor(pin) {
  DigitalSensor.apply(this, Array.prototype.slice.call(arguments))
}
MotionDigitalSensor.prototype = new DigitalSensor()

MotionDigitalSensor.prototype.read = function() 
{
  var write = this.board.writeBytes(commands.dRead.concat([this.pin, commands.unused, commands.unused]))
  if (write) {
    this.board.wait(200)
    var singleByte = this.board.readByte()
    var bytes = this.board.readBytes();
    return bytes[0];
  }
}
module.exports = MotionDigitalSensor

#2

Hey,
Thanks a lot for sharing the code here. It would be great if you could send us a pull request here: https://github.com/DexterInd/GrovePi/tree/master/Software/NodeJS/libs/sensors so that we can merge it directly in the Github repository and others can use it too.

-Karan


#3

Agreed, thanks! Please share!