Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements PIR sensor inferface; calibrated, motionstart, motionend e…
…vents Signed-off-by: Rick Waldron waldron.rick@gmail.com <waldron.rick@gmail.com>
- Loading branch information
Showing
3 changed files
with
122 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,51 @@ | |||
var arduino = require('../'), | |||
board, pir; | |||
|
|||
board = new arduino.Board({ | |||
debug: false | |||
}); | |||
|
|||
pir = new arduino.PIR({ | |||
board: board, | |||
pin: 7 | |||
}); | |||
|
|||
// 'calibrated' event fired when PIR sensor is | |||
// ready to detect movement/motion in observable range | |||
// | |||
// All events receive error and date arguments | |||
pir.on('calibrated', function(err, date) { | |||
|
|||
console.log('calibrated'); | |||
|
|||
// Current sensor data stored in properties | |||
// of this PIR instance: | |||
// | |||
// this.state - current state of motion | |||
// 0 No motion currently detected | |||
// 1 Motion currently detected | |||
|
|||
// 'motionstart' event fired when motion occurs | |||
// within the observable range of the PIR sensor | |||
this.on('motionstart', function(err, date) { | |||
|
|||
console.log('motionstart', this.state); | |||
console.log( date ); | |||
|
|||
}); | |||
|
|||
// 'motionend' event fired when motion has ceased | |||
// within the observable range of the PIR sensor | |||
this.on('motionend', function(err, date) { | |||
|
|||
console.log('motionend', this.state); | |||
|
|||
}); | |||
}); | |||
|
|||
|
|||
// To test, use the following: | |||
// http://www.ladyada.net/images/sensors/pirardbb.gif | |||
// | |||
// More information: | |||
// http://www.ladyada.net/learn/sensors/pir.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,69 @@ | |||
var events = require('events'), | |||
util = require('util'); | |||
|
|||
/* | |||
* Main PIR constructor | |||
* Process options | |||
* Tell the board to set it up | |||
*/ | |||
var PIR = function (options) { | |||
if (!options || !options.board) { | |||
throw new Error('Must supply required options to PIR'); | |||
} | |||
this.board = options.board; | |||
this.pin = this.board.normalizePin(options.pin || 9); | |||
this.state = null; | |||
this.calibrated = false; | |||
|
|||
setInterval(function () { | |||
this.board.digitalRead(this.pin); | |||
}.bind(this), 50); | |||
|
|||
this.board.on('data', function (message) { | |||
var m = message.slice(0, -1).split('::'), | |||
timestamp = new Date(), | |||
err = null, | |||
pin, data; | |||
|
|||
if (!m.length) { | |||
return; | |||
} | |||
|
|||
pin = m[0]; | |||
data = m[1]; | |||
|
|||
if (pin === this.pin) { | |||
|
|||
// If this is not a calibration event | |||
if (this.state != null && this.state != +data) { | |||
|
|||
// Update current state of PIR instance | |||
this.state = +data; | |||
|
|||
// 'motionstart' event fired when motion occurs | |||
// within the observable range of the PIR sensor | |||
if (data === '01') { | |||
this.emit('motionstart', err, timestamp); | |||
} | |||
|
|||
// 'motionend' event fired when motion has ceased | |||
// within the observable range of the PIR sensor | |||
if (data === '00') { | |||
this.emit('motionend', err, timestamp); | |||
} | |||
} | |||
|
|||
// 'calibrated' event fired when PIR sensor is | |||
// ready to detect movement/motion in observable range | |||
if (!this.calibrated) { | |||
this.calibrated = true; | |||
this.state = +data; | |||
this.emit('calibrated', err, timestamp); | |||
} | |||
} | |||
}.bind(this)); | |||
}; | |||
|
|||
util.inherits(PIR, events.EventEmitter); | |||
|
|||
module.exports = PIR; |