Doorbell Js
Doorbell Js
Doorbell Js
function exit() {
high.unexport();
button.unexport();
process.exit();
}
thingShadows.on('connect', function() {
thingShadows.register( 'raspberrypi' , { ignoreDeltas: true,
persistentSubscribe: true } );
setTimeout( function() {
clientTokenUpdate = thingShadows.update('raspberrypi', State );
}, 2000 );
});
thingShadows.on('status',
function(thingName, stat, clientToken, stateObject) {
console.log('received '+stat+' on '+thingName+': '+
JSON.stringify(stateObject));
});
thingShadows.on('delta',
function(thingName, stateObject) {
console.log('received delta '+' on '+thingName+': '+
JSON.stringify(stateObject));
});
thingShadows.on('timeout',
function(thingName, clientToken) {
console.log('received timeout '+
clientToken);
});
console.log('button register');
high.writeSync(1);
button.watch(
function(err, value) {
console.log('button pressed');
var date = new Date();
var tmp = date.getFullYear()+""+date.getMonth()+1+""+date.getDate()
+"-"+date.getHours() + date.getMinutes() + date.getSeconds()+".jpg";
var exec = require('child_process').exec;
var cmd = 'fswebcam '+tmp;
sleep.sleep(3);
var JsFtp = require('jsftp'),
ftp = new JsFtp({
"host": "ftp-server.com",
"user": "username",
"pass": "password",
}),
file1 = './'+tmp;
delete State['version'];
delete State['connected'];
State["state"]["reported"]["button"] = 1;
buttonStateResponse = thingShadows.update('raspberrypi', State);
thingShadows.publish('topic/pressed', value.toString());
});
process.on('SIGINT', exit);