(prev. post is Get a working webcam on Arduino Yun - The smart entrance - Internet of Holiday Lights RoadTest read this to have a working webcam on Arduino Yun)
In this post I will explain how to start a python script or possibly any other program / bash by our Arduino sketch Yun.
In particular, I am going to explain the procedure to have a motion detector (using a HC RS04 ultrasound sensor) that allows to send a photo by email in case it detects a change of distance of at least 2cm in two successive measurements in 'arc of about 60ms.
So let's see what are the steps necessary to accomplish this purpose.
Get a working python with support to HTTPS/SSL doing in SSH :
opkg update #updates the available packages list opkg install distribute #it contains the easy_install command line tool opkg install python-openssl #adds ssl support to python
I have positioned the sendemail.py (download it from attached file) in the /www/cgi-bin with FileZilla. [sendemail.py taken from http://dev.mikamai.com/post/76945627390/you-cant-touch-this-an-evil-arduino-based]
The sendmail.py was edited with my email ad login parameters of gmail.
IMPORTANT : go to https://www.google.com/settings/security/lesssecureapps and enable this option to have a working script!!
This is the script that I have loaded in my Arduino Yun that send an email when the distance have a delta > 2cm.
#include <Bridge.h>
#include <Wire.h>
//HC RS04 ultrasound sensor
int triggerPort = 4;
int echoPort = 2;
Process p;
void setup() {
Wire.begin();
Bridge.begin();
Serial.begin(115200);
//ultrasound sensor
pinMode( triggerPort, OUTPUT );
pinMode( echoPort, INPUT );
}
void commandGetImage(int n){
p.runShellCommand("fswebcam --device /dev/video0 --resolution 640x480 --jpeg 95 --save /www/"+String(n)+".jpg --palette MJPEG --no-banner"); //takes the picture
while(p.running()); //wait till the process ends
}
float getDistance(){
digitalWrite( triggerPort, LOW );
//send one impulse of 10microsec on trigger
digitalWrite( triggerPort, HIGH );
delayMicroseconds( 10 );
digitalWrite( triggerPort, LOW );
float duration = pulseIn( echoPort, HIGH );
float centimeter = 0.034 * duration / 2;
if( duration > 58000 ){
duration=-1;
centimeter=-1;
}
if (centimeter==0){
Serial.println("ERROR - 0 cm - get again the distance");
delay(10);
centimeter=getDistance();
}
return centimeter;
}
void loop() {
float d,prev=getDistance();
float delta=0;
while (true){
delta=0;
//get the first distance
prev=getDistance();
//get the image
commandGetImage(0);//0.jpg is the image file taken
//wait 50ms
delay(50);
//get the second distance
d=getDistance();
//calculate the delta
delta=abs(d-prev);
//if the delta is big send email - a movement is recognised!
if(delta>2){
Serial.print(" delta= "+String(delta) + "["+String(prev)+"cm,"+String(d)+"cm]");
Serial.print("\nSENDING...");
p.runShellCommand("python /www/cgi-bin/sendemail.py /www/" +String(0)+".jpg"); //sends the picture via email
while(p.running()); //wait till the process ends
Serial.println("E-MAIL SENDED");
}else{
Serial.print(".");
delay(50);
}
}
}