raspy
Posts: 17
Joined: Thu Apr 19, 2012 6:15 pm

Pir emailing webcam

Wed Mar 13, 2013 6:23 pm

Hi, How do I start writing a script so when the pir sensor is triggered the webcam takes a pic and emails it to me? Is there some sort of guide to help start writing a script for this?

Many thanks

justinpoiroux
Posts: 19
Joined: Wed Feb 06, 2013 10:47 pm

Re: Pir emailing webcam

Wed Mar 13, 2013 10:12 pm

First look up an email script, then look up how to use sensors, as well as a webcam.

myminimonster
Posts: 33
Joined: Sun Dec 30, 2012 11:08 pm
Location: Midlands UK

Re: Pir emailing webcam

Thu Mar 14, 2013 2:32 am

Hi,

What language do you wish to use to write these programs?

I have the same features you wish to use.

I first got a webcam working with mjpg-streamer software.
I then wrote a php script that took a snapshot of the webcam and saved it to file, with a filename being the date

Code: Select all

<?php
//snapit.php

date_default_timezone_set('GMT');
$date23 = date('l dS F Y his');
//strip spaces out of date string
$date23 = preg_replace('/\s+/', '', $date23);
 //copies the webscam snapshot to a folder /var/www/snaps/
copy('http://192.168.0.12:8081/?action=snapshot', '/var/www/snaps/'.$date23.'.jpg');
?>
In my example after the above I write the filename to a database, which is optional

Then i moved onto a python script to monitor a GPIO input pin. I used this motion sensor
http://www.adafruit.com/products/189
I could have used motion software and detected motion using that but didnt for some reason.

This python script was obtained from here and an example to get the motion sensor working.
http://www.raspberrypi-spy.co.uk/2013/0 ... pi-part-1/
Within this python script I used this command to call the php script first mentioned above to trigger off the taking of the webcam picture

Code: Select all

import subprocess
proc = subprocess.Popen("php /var/www/snapit.php", shell=True, stdout=subprocess.PIPE)
                script_response = proc.stdout.read()
                print script_response
The response is the filename (date in my example) that is echo'd/returned from the first php script
So now I detected motion and called a script that takes a picture and provides me with the filename of it.

I then used the same subprocess method to call my php script that sends me the email.

I had to add a few other bits to mine as i was not actually emailing myself a picture attachment, i am simply sending the url to link to it.

So my html email body contained something like this

<img src="http://x.x.x.x/var/www/snaps/Monday11th ... 063454.jpg" />

To make sure the link worked i had to get my external ip address first and send that to the php script that is sending the email

if you want to do this, write another php script that gets your external ip address and call it from the python script before you send the email.

So in summary i have a python script that detects motion.

When motion is detected i used subprocesses to call
1) one php script to get my external ip and return it to the python script
2) one php script to take the webcam image and return the filename to the same python script
3) and send all the above parameters to a final php script that sends the email

I am a newbie so this may not be the best way to do it but works for me.

Hope this helps and let me know if i can assit with the above. It took me a million times long than i thought so will be happy to help you out along the way, if I can.
Adrian
Newbie

chuguanyu
Posts: 5
Joined: Sun Mar 03, 2013 3:22 am
Contact: Website

Re: Pir emailing webcam

Thu Mar 14, 2013 4:46 pm

why not use motion, which will take a snapshot when it 'detects' movement and then you can wire a script to take an action which could be emailing.
raspy wrote:Hi, How do I start writing a script so when the pir sensor is triggered the webcam takes a pic and emails it to me? Is there some sort of guide to help start writing a script for this?

Many thanks

realtek
Posts: 34
Joined: Wed Jan 09, 2013 3:16 pm

Re: Pir emailing webcam

Thu Mar 14, 2013 5:30 pm

I'm going wire mine up to my doorbell lol.

So when someone hits the doorbell it sends me an email with a copy of their photo.

Lets just hope they don't go away before I receive it! :)

realtek
Posts: 34
Joined: Wed Jan 09, 2013 3:16 pm

Re: Pir emailing webcam

Thu Mar 14, 2013 5:32 pm

BTW.. i'm actually going to wire this up inside the wirless receiver i plug in the wall... so they would never know what has happend :)

raspy
Posts: 17
Joined: Thu Apr 19, 2012 6:15 pm

Re: Pir emailing webcam

Thu Mar 14, 2013 8:47 pm

Thanks for all your replies. Adrian I have looked into your post but I get lost. I don't think I have a clue. I would like to use python. I have seen this guide. It's for a arduino is it possible to port over to use on a raspberry pi http://learn.adafruit.com/downloads/pdf ... tector.pdf

Thanks

myminimonster
Posts: 33
Joined: Sun Dec 30, 2012 11:08 pm
Location: Midlands UK

Re: Pir emailing webcam

Thu Mar 14, 2013 11:23 pm

Hi,

You can use the example python script on http://www.raspberrypi-spy.co.uk/2013/0 ... pi-part-1/ along with the python code from the link you provided to have one script that 1) detects the motion and 2) sends you the email.

These two scripts are performing each function separately. The one from the first link above just prints 'motion detected' to the screen. This is where you want to insert the code from the other link above, which will send the email instead.

This is a simular approach to what i did but I called the scripts rather than combining them in one script.

to learn, and how i did it, was to write one thing at a time. Write a python script /copy from http://www.raspberrypi-spy.co.uk/2013/0 ... pi-part-1/ just to make sure you can get the motion detection working.

After this move onto a new script for ending yourself an email You can just run the script from command line.

After this combine the two.
The only thing with the link to the arduino code you provided is that this one only send a plain email on detecting motion.

You can look at this afterwards with another new script that takes a screen shot from your camera and saves it.
Then you can add this script into the ones you created with detecting motion and emailing yourself and get it to send yourself the link to the picture.
As mentioned before, mine only sends me a link to the picture, it doesn't actually email me a picture. attachment. It appears in the email as an image though.

Hope this helps and let us know how you get on.

Adrian
Newbie

raspy
Posts: 17
Joined: Thu Apr 19, 2012 6:15 pm

Re: Pir emailing webcam

Sun Mar 24, 2013 8:08 pm

Hi. Thanks for all your replies. Not sure I'm up for this programming. Been looking and trying to no success.

Here is where I am. I have a pir script that was mentioned above from raspberry spy. That works. I have a snap shot script. This one

import pygame, sys
from pygame.locals import *
import pygame.camera
pygame.init()
pygame.camera.init()
cam = pygame.camera.Camera("/dev/video0",(352,288))
cam.start()
image= cam.get_image()
pygame.image.save(image,'/home/pi/pictures/camera.jpg')
cam.stop()

That works and saves a picture. What need help doing is making them 2 work together. Once these work then I will look into a email one.

If any one can help it would be great.

Thanks


gordon77
Posts: 5132
Joined: Sun Aug 05, 2012 3:12 pm

Re: Pir emailing webcam

Sun Mar 24, 2013 8:59 pm

I've tried the example for email with an attachment which worked OK, but note I haven't tried in on a RPi yet.

Just ensure you set all the hosts etc

Gordon77

myminimonster
Posts: 33
Joined: Sun Dec 30, 2012 11:08 pm
Location: Midlands UK

Re: Pir emailing webcam

Sun Mar 24, 2013 11:33 pm

Hi,

To get the two working together , if your talking about your pir script and the your snap shot script, use the subprocess popen command within your pir script.

Something like this, which contains a snippet of the PIR script.

Code: Select all

#declarations
import subprocess
#snippet from pir script from raspberry spy
 if Current_State==1 and Previous_State==0:
      # PIR is triggered
      print "  Motion detected!"
     proc = subprocess.Popen("python ../path_to/your_snap_script.py ", shell=True, stdout=subprocess.PIPE)
     script_response = proc.stdout.read()
     # this will contain any output text from your snap shot script
      print script_response
      # Record previous state
      Previous_State=1
Hope this helps too
Adrian
Newbie

anddav87
Posts: 11
Joined: Tue Jun 26, 2012 7:50 pm
Location: Benfleet, Essex, UK
Contact: Website

Re: Pir emailing webcam

Fri Mar 29, 2013 4:24 pm

raspy wrote:Hi, How do I start writing a script so when the pir sensor is triggered the webcam takes a pic and emails it to me? Is there some sort of guide to help start writing a script for this?

Many thanks
Hey

Check out what I just uploaded here for you: https://github.com/anddav87/PIRemail

You need to install fswebcam first. Todo this type

Code: Select all

sudo apt-get install fswebcam
Then you should have everything you need.

Let me know if you get stuck.

Cheers

Andy

Return to “Automation, sensing and robotics”