Stap 8: Voer de javascript-code
- Als u wilt uitvoeren de beveiligingsmodule camera op uw Edison, de ingesloten code gebruiken en wijzigen voor het bijwerken van het e-mailadres en wachtwoord.
//Print console message to start the camera process.stdout.write("Begin security camera"); //Set-up GPIO pin 6 var mraa = require('mraa'); var motionSensor = new mraa.Gpio(6); motionSensor.dir(mraa.DIR_IN); //Set up email information using node mailer var nodemailer = require("nodemailer"); var smtpTransport = nodemailer.createTransport({ service: "Gmail", auth: { user: "EdisonCam pass: "EdisonCamPassword" } }); //command line execution function used to activate USB camera from command line later in the code function run_cmd(cmd, args, callBack ) { var spawn = require('child_process').spawn; var child = spawn(cmd, args); var resp = ""; child.stdout.on('data', function (buffer) { resp += buffer.toString() }); child.stdout.on('end', function() { callBack (resp) }); } // () //Start motion sensor! periodicActivity(); function periodicActivity() { //Read the motion sensor activity var motionSensorTriggered = motionSensor.read(); //Image Number var i = 0; //Send email if the motion sensor is HIGH if(motionSensorTriggered){ //Print activity detected message process.stdout.write("Motion sensor detected"); //Remove previously captured picture run_cmd( "rm", ['-f', '/home/root/intruder_image.jpeg'], function(text) { console.log (text) }); //Take a picture from the USB camera - from command line run_cmd( "/home/root/ffmpeg-2.7.2-32bit-static/ffmpeg", ['-s', '1280x720', '-f', 'video4linux2', '-i', '/dev/video0', '-vframes', '1', 'intruder_image.jpeg'], function(text) { console.log (text) }); //Print done message console.log("\nDone with taking the picture"); //Send our email message with picture attachment smtpTransport.sendMail({ from: "EdisonCam ", to: "User Name ", subject: "Possible Intruder Alert", text: "A possible intruder is detected on your EdisonCam! Check the picture attached to see if you know this person. Please call 911 if you suspect any illegal activity.", attachments:[ { filename: 'intruder_image.jpeg', path: '/home/root/intruder_image.jpeg' } ] }, function(error, response){ //Send a report of the message to the console if(error){ console.log(error); }else{ console.log("Message sent: " + response.message); } smtpTransport.close(); }); process.stdout.write("Message sent"); // } //To avoid flooding of the user's Inbox with our emails, we want to wait a few seconds //(in this case, 30 seconds) before sending another email. The timeout //is in milliseconds. So, for 1 minute, you would use 60000. setTimeout(periodicActivity, 30000); }else{ //The motion sensor wasn't triggered, so we don't need to wait as long. // 1/10 of a second seems about right and allows Edison to do other // things in the background. setTimeout(periodicActivity, 100); } }
- En dan de cd in uw hoofdmap en gebruik de volgende opdracht uit te voeren van de javascript.
cd /home/root/ node securityCamera_instructable.js
- Tada! u ontvangt dan een e-mail zoals in de afbeelding!
Als u Gmail voor uw demo gebruikt, ziet u dat de e-mails van uw Edison worden geblokkeerd na het versturen van e-mails van 100-150. Dat komt omdat Gmail beperkt het aantal e-mails die kunt u in een dag. We gebruikten Yahoomail te krijgen rond dit onderwerp op de Austin Mini Maker Faire 2015. Yahoomail kunt u ~ 100 e-mails per uur verzenden. Als alternatief, kunt u opstelling uw eigen e-mailcliënt om rond dit probleem te krijgen. Ook kunt u de e-mail frequentie verminderen in uw code door de volgende regel aan te passen
setTimeout(periodicActivity, 30000);
Maar dit vereist ook de gebruiker meer geduld om te zien de output van de e-mail.