Taiyuk
Published © GPL3+

IoT DoorBell

Reinvent doorbell to receive alert and know who is outside, no matter where you are!

IntermediateFull instructions provided33,623
IoT DoorBell

Things used in this project

Story

Read more

Schematics

Raw materials

Raspberry Pi, Button, USB Webcam

Code

doorbell.js

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

Credits

Taiyuk

Taiyuk

3 projects • 7 followers

Comments