Govindaraj Kathirvel
Published © Apache-2.0

Secure My Home

Secure My home is a Home security system monitors the home from intruders and notifies the user on the mobile.

IntermediateShowcase (no instructions)2 hours550
Secure My Home

Story

Read more

Code

Secure My Home Code

JavaScript
Work in Progress Code.
 
// change this to false to use the hand rolled version
var useUpmVersion = true;

// we want mraa to be at least version 0.6.1
var mraa = require('mraa');
var version = mraa.getVersion();

if (version >= 'v0.6.1') {
    console.log('mraa version (' + version + ') ok');
}
else {
    console.log('meaa version(' + version + ') is old - this code may not work');
}



var upmBuzzer = require("jsupm_buzzer");// Initialize on GPIO 5
var myBuzzer = new upmBuzzer.Buzzer(5);
var chords = [];
chords.push(upmBuzzer.DO);
chords.push(upmBuzzer.RE);
chords.push(upmBuzzer.MI);
chords.push(upmBuzzer.FA);
chords.push(upmBuzzer.SOL);
chords.push(upmBuzzer.LA);
chords.push(upmBuzzer.SI);
chords.push(upmBuzzer.DO);
chords.push(upmBuzzer.SI);
var chordIndex = 0;

// Print sensor name
console.log(myBuzzer.name());

function SoundAlert() {
    if (chords.length != 0) {
        //Play sound for one second
        console.log(myBuzzer.playSound(chords[chordIndex], 1000000));
        chordIndex++;
        //Reset the sound to start from the beginning. 
        if (chordIndex > chords.length - 1)
            chordIndex = 0;
    }
}

//setInterval(melody, 100);

// Print message when exiting



var upmMicrophone = require("jsupm_mic");
// Attach microphone to analog port A0
var myMic = new upmMicrophone.Microphone(0);

var threshContext = new upmMicrophone.thresholdContext;
threshContext.averageReading = 0;
threshContext.runningAverage = 0;
threshContext.averagedOver = 2;

var is_running = false;
// Infinite loop, ends when script is cancelled
// Repeatedly, take a sample every 2 microseconds;
// find the average of 128 samples; and
// print a running graph of the averages
while (1==1) {
    console.log("in loop");
    var buffer = new upmMicrophone.uint16Array(128);
    var len = myMic.getSampledWindow(60, 128, buffer);
    if (len) {
        var thresh = myMic.findThreshold(threshContext, 30, buffer, len);
        myMic.printGraph(threshContext);
        if (thresh) {
            console.log("Threshold is " + thresh);
            SoundAlert();
            displayAlert('Security Alarm');
        }
        else
        {
            console.log("Not in Threshold is " + thresh);
            displayAlert('No Alert');
        }
    }
}


function displayAlert(message) {
    var lcd, display;
    if (useUpmVersion) {
        lcd = require('jsupm_i2clcd');
        display = new lcd.Jhd1313m1(0, 0x3E, 0x62);
    }
    else {
        lcd = require('./lcd');
        display = new lcd.LCD(0);
        display.setColor(0, 60, 255);
    }
    display.setCursor(1, 1);
    display.write(message);
}


// Print message when exiting
process.on('SIGINT', function () {
    console.log("Exiting...");
    process.exit(0);
});

Credits

Govindaraj Kathirvel

Govindaraj Kathirvel

3 projects • 1 follower

Comments