Tuesday, 29 October 2013

Raspberry Pi Motion Detection Security Camera

We have weekly deliveries of meat or vegetables to our home that are left outside our house if we are not there to receive them.  Although they are only there for a few hours and hidden behind some planters someone noticed them and started stealing them. I decided to catch them by using a Raspberry Pi to make a really cheap security camera.

Overview:

Connect a webcam to your Raspberry Pi. Do some software stuff.  Get a home CCTV camera that detects motion, records when there's motion and emails a link to that video uploaded into a google drive folder.

Kit list:

Raspberry Pi
Wheezy Raspbian Linux OS
Webcam (eLinux USB Webcam Compatibility List)
Wifi USB dongle (eLinux USB Wifi Adapter Compatibility List)
Development was done using a Mac.

Hardware set up
Plug it all in
You may need a powered USB hub.

Development:

Plug in your webcam and check that it has been recognised by typing the following into your Raspberry Pi's terminal either with a directly connected keyboard/screen or a ssh connection to the RPi.  It is our recommendation you set up a ssh connection for remote access once it is set up.

lsusb

Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.
Bus 001 Device 007: ID 145f:018d Trust
Bus 001 Device 005: ID 148f:5370 Ralink Technology, Corp. RT5370 Wireless Adapter

Install motion and dependencies:
sudo apt-get install motion
sudo apt-get autoremove
sudo apt-get install fswebcam

Setup motion

All setup of the motion service is done by editing its configuration file (/etc/motion/motion.conf).
sudo nano /etc/motion/motion.conf

The main settings we changed are as follows:
  • width 640
  • height 480
  • threshold 3000 (number of pixels required to change to trigger, was set to 1500)
  • framerate 1 (if this is set too high, your output videos will run too fast: see FAQ: motion mpeg runs too fast)
  • quality = 100 (sets jpeg compression was 75)
  • ffmpeg_video_codec mpeg4 (came as swf a flash file, many options but mpeg4 is 'default')
  • text_double on (enlarge information overlayed on video e.g. time)
  • daemon on (allows motion to run as a service)
  • webcam_localhost off (allows you to connect to live webcam feed using a browser pi_ip:8081)
  • webcam_port 8081
  • control_localhost off (allows you to connect to remote control interface using a browser pi_ip:8080)
To apply any changes in the conf file immediately the service must be restarted.
sudo service motion restart

To make the motion service autostart upon booting the Pi you need to add a line of text to /etc/default/motion as follows:
sudo nano /etc/default/motion
start_motion_daemon=yes

First time round we need to start it manually.
sudo service motion start

By default the images and videos are stored in /tmp/motion although this can also be changed in the config file.  We had an issue with permissions not allowing the motion process to delete images once the video had been created and have not solved this issue yet - any help in the comments please!

You can see the live feed from the webcam/motion by opening <your pi's ip address>:8081 in something like Firefox browser (it didn't work with Chrome).  The port number can be changed in motion's config file.

Google Drive & Email Integration

Motion saves images into a folder and then creates a video from that sequence of jpegs.  It does this everytime motion is detected.  It also allows you to specify an action to take on certain events such as when a video file has finished being written.  I decided to run a python programme once a new video file has been created. Here's the pseudo code:
  • When a new video is created:
    • Copy video file to a google drive folder
    • Send an email to alert me that a new video is available
    • Include a link to the new file in the email
    • Delete old files from the pi
I found an awesome tutorial by Jeremy Blythe with code that did the job. The tutorial requires installation of a python google library.  If you can't use pip as instructed in the tutorial you can use apt-get as follows:
sudo apt-get install python-gdata

Create two files as listed on Jeremy's blog (one is a python programme, the other a config file)
Alter the .cfg file with your details.  Note that your gmail account username is the full email address such as first.secondname@gmail.com.  Also make sure to create the Google Drive folder that you point to before running the code.  The python code worked straight away for me with no issues. I have posted the files on github's raspberry-pi-cctv repository.

Finally, we want this python code to be run whenever a video file has been created. to do this go back to editing the motion.conf file (etc/motion/motion.conf) and scroll down to near the bottom where you will find an on_movie_end option.  Change the value to point to your python and config files, for example:
on_movie_end /home/pi/motion/motionevent.py /home/pi/motion/motionevent.cfg %f

Outstanding Issues

Permissions on the jpeg files do not allow them to be deleted programmatically from the python script.
Some videos are corrupted.
Create a ppm image to mask areas where you don't want to sense motion. Partially done.

No comments:

Post a Comment