Doorbell Js

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

var awsIot = require('aws-iot-device-sdk');

var Gpio = require('onoff').Gpio;


var sleep = require('sleep');
var JSFtp = require("jsftp");

var thingShadows = awsIot.thingShadow({


keyPath: 'private.pem.key',
certPath: 'certificate.pem.crt',
caPath: 'root-CA.crt',
clientId: 'raspberrypi',
region: 'ap-northeast-1'
});

function exit() {
high.unexport();
button.unexport();
process.exit();
}

var State = {"state":{"reported":{"connected":"yes"}}};


var clientTokenUpdate;

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);
});

high = new Gpio(23, 'out');


button = new Gpio(24, 'in', 'rising');

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;

exec(cmd, function(error, stdout, stderr) {


console.log(stdout);
});

sleep.sleep(3);
var JsFtp = require('jsftp'),
ftp = new JsFtp({
"host": "ftp-server.com",
"user": "username",
"pass": "password",
}),
file1 = './'+tmp;

ftp.put(tmp, tmp, function () {


console.log(tmp, arguments);
});

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);

You might also like