Paso 8: Ejecutar el código javascript
- Para ejecutar el módulo de cámara de seguridad en el Edison, utilice el código embebido y modificar para actualizar la dirección de correo electrónico y contraseña.
//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); } }
- Y luego el cd en el directorio raíz y utilice el comando siguiente para ejecutar javascript.
cd /home/root/ node securityCamera_instructable.js
- Tada! recibirá un correo electrónico como se muestra en la imagen!
Si usas Gmail para su demostración, podría ver que los correos electrónicos de su Edison se bloquean después de enviar correos electrónicos de 100-150. Eso es porque Gmail limita el número de correos electrónicos que puede enviar en un día. Utilizamos Yahoomail para conseguir alrededor de esta cuestión en el Austin Mini Maker Faire 2015. Yahoomail le permite enviar emails de ~ 100 cada hora. Alternativamente, puede configurar su cliente de correo electrónico para conseguir alrededor de este tema. Además, puede disminuir la frecuencia del correo electrónico en el código mediante el ajuste de la línea siguiente
setTimeout(periodicActivity, 30000);
Pero esto también requiere que el usuario a ser más pacientes para ver la salida de correo electrónico.