Tuesday, November 12, 2013

Raspberry Pi Motion Detector


One of my first projects with a Raspberry Pi was a  motion detector for a home security application. The goal was to  alert me via an email when there was any movement detected by a webcam and also upload pictures and videos to a Google drive account. The webcam  could also be controlled remotely to view my home when I was away as well.

This post should help if you want to attempt something similar.

First off, you will need:

A webcam -  For this project I used an inexpensive Silicon Motion USB webcam which I borrowed from a friend you had it lying around. 

WiFi  - I used the popular Edimax EW-7811Un 150 Mbps Wireless 802.11bgn USB Adapter

Motion Detect Software:

Motion - An open source motion detection software

sudo apt-get install motion     

Main file of importance here is:  /etc/motion/motion.conf
Lines to modify
daemon on -> change this to  off
webcam_localhost on -> change this to  off
control_localhost on  -> change this to  off
Some useful parameters:
TCP/IP Port for remote webcam control:
control_port 8080
TCP/IP Port for remove webcam stream:
webcam_port 8081
Picture size:
width 320
height 240
Threshold for detecting motion. The threshold is the number of changed pixels counted after noise filtering, masking, despeckle, and labelling. Change this based on your personal needs.
threshold 1500

Actions to perform based on motion detect:
on_picture_save <script>
on_motion_detected <script>
on_movie_end <script>

You can customize what action you want to take based on several triggers. In my case, I used a google drive uploader script written by Peter Nichols(Thanks!).  You can copy the python code from the post here

You will need python and gdata (google data) Linux packages to get this to work.

Usage: Change the action line in the motion.conf above to this:
on_picture_save sudo python <script name> -u <google username> -p <google passwd> -l <google drive folder> -f  %f -m jpg
on_movie_end sudo python  <script name> -u <google username> -p <google passwd> -l <google drive folder> -f  %f -m mp4


You are all set!

Start the motion software in setup mode:
sudo motion -s

While running, you can see the pixel change value, noise value etc per frame. You can change the threshold value in the motion.conf file based on your specific needs. 

Test out the motion detection to see if everything works as expected.

Remote control/monitoring of the Webcam

 You can connect to your Raspberry Pi IP address with port 8080 i.e. (<my raspberry Pi IP>:8080) to control and change different parameters of your motion.conf file remotely. I will let you play around with that

You can also monitor your webcam feed via the streaming port (8081). This is useful if you want to see what is going on in your home after a motion detect alarm.

If you want to connect to your raspberry pi and motion software from a remote location, you have to setup port forwarding via your home router firewall settings. You can usually see what devices are connected to your router (via IP address) and enable certain ports for those devices (eg 8080,8081). Be careful here, since you are now opening this up to the outside world! Motion allows you for password enabled access as well (see motion.conf file) which I would highly recommend.

Please leave your comments or questions below and I will be happy to respond to them.

3 comments:

  1. Hi, please, could you helpme? im getting this response, thaks a lot for this tutorial!!!

    Executing external command 'sudo python -u <#####@gmail.com> -p <#####> -l -f /tmp/motion/01-20140322094640-01.jpg -m jpg'
    [1] File of type 1 saved to: /tmp/motion/01-20140322094640-01.jpg
    &: 1: &: cannot open googledrive1.py: No such file

    ReplyDelete
  2. Did you download the Google drive upload script I pointed to in my post?. You also need to make sure all the account settings are set for your gmail account.

    ReplyDelete