Skip to content

Instantly share code, notes, and snippets.

@beve
Last active December 30, 2015 05:39
Show Gist options
  • Save beve/7783855 to your computer and use it in GitHub Desktop.
Save beve/7783855 to your computer and use it in GitHub Desktop.
Raspberry Pi + Nodejs + PIR module + webcam + Philips Hue
var hue = require("node-hue-api");
var HueApi = require("node-hue-api").HueApi;
var gpio = require("gpio");
var nodemailer = require("nodemailer");
/* ************* */
/* CONF */
/* ************* */
var usernameHue = ''; // md5sum
var mailConf = {service: 'Gmail', user: '', pass: ''};
var mailOptions = {
from: "", // sender address
to: "", // list of receivers
subject: "", // Subject line
text: "", // text body. Can be replaced by html: string
attachments: [{fileName: 'capture.jpg', filePath: 'capture.jpg'}] // Attach capture
}
var gpioPinNumber = 11;
// Hue
var lightState = hue.lightState;
var api;
var status = false;
var lightOff;
// Nodemailer
var smtpTransport = nodemailer.createTransport("SMTP",{
service: mailConf,
auth: {
user: mailConf.user,
pass: mailConf.pass
}
});
// fswebcam
var exec = require('child_process').exec;
var initExtinction = function() {
lightOff = setTimeout(function() {
var state = lightState.create().off();
console.log('off');
api.setLightState(1, state);
status = false;
}, 180000);
}
function resetExtinction() {
console.log('reset timer');
clearTimeout(lightOff);
initExtinction();
}
function sendmail() {
smtpTransport.sendMail(mailOptions, function(error, response){
if(error){
console.log(error);
}else{
console.log("Mail sent ");
}
});
}
function takeScreenshot() {
setTimeout(function() {
exec('fswebcam capture.jpg', function(error, stdout, stderr) {
console.log('catpure ok');
sendmail();
});
}, 1000);
}
// Let's go
hue.searchForBridges(2000).then(function(result) {
var bridge = result[0];
if (bridge.id) {
console.log("Bridge found");
api = new HueApi(bridge.ipaddress, usernameHue);
// Init gpio
var gpioPin = gpio.export(gpioPinNumber, {
direction: "in",
ready: function() {
}
});
gpioPin.on("change", function(val) {
if (val == 1) {
console.log('detected');
var hours = new Date().getHours();
if ((hours > 21 || hours < 7)) {
if (!status) {
var state = lightState.create().on().white(500, 60);
api.setLightState(1, state)
.then(function() {status = true;takeScreenshot();initExtinction();console.log('on')}).done();
} else {
resetExtinction();
takeScreenshot();
}
}
} else {
console.log("PIR module sent off signal.");
}
});
}
}).done();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment