A home intrusion detection setup (sort of)

Taking Pics

Knowing that something's up is not enough. You will want to know what's up – hence, the camera. You're going to need Pygame (installed by default) and PIL (Python Imaging Library) to take and convert the pictures. To get and install PIL, you need to install the Python development packages from the Raspbian repositories:

$ sudo apt-get install python-dev python3-dev

Then, install the imaging library from the Python repositories:

$ sudo easy_install Image

Pygame makes controlling a camera easy (lines 9 and 10 create a camera object; line 83 switches it on; lines 90 and 91 take a picture), but it only saves images in the uncompressed BMP format. BMP pictures are very big and are not a practical format for sending photos over the Internet (I'll talk about that later). So, in lines 94 to 97, you convert the images the camera takes to the compressed PNG format and copy it over to a pre-existing secret directory that hangs off your /home/pi directory (line 97).

Now that I think of it, maybe .secretdirectory is a bad choice for something you want to be inconspicuous. A better choice would be a series of random letters and numbers. Remember the dot at the beginning: It marks the directory as hidden.

Sending Photos

You really don't want the photos sitting on your Pi while the bad guys loot your home. For one thing, if the crooks become aware that they are being watched, they can just take a baseball bat to your Pi, and all your evidence will be lost. Additionally, it could be a false alarm, and you may end up calling the cops because your cat managed to get into your room.

What you want to have is a program that sends the photos to your phone (e.g., via email). That way, you can see what's up. The easiest way to send images from the command line is by using mutt and incron. The former is a command-line mail client, and the latter is a Linux kernel subsystem that allows you to monitor a directory or a file and then run a script when something changes.

By using these two tools, you can relieve the Python script of the time-consuming work of sending the pictures, and it can carry on watching your home.

To work with incron, you need incrond, the incron daemon. To install, enter:

$ sudo apt-get install incron

This, by the way, will also start the daemon.

Next, you will have to add pi to the /etc/incron.allow file, so the pi user (i.e., you) can use the incron subsystem.

I am not going to go into the depths of incron in this article, mainly because I already wrote about it [5]. All you need to know is that you create a special file (called an incron table), state which file or directory you want to monitor (in this case, the secret directory you created earlier to store your photos) and what event you want to act as a trigger (in this case, IN_CREATE, which fires when a new file or directory is created within the watched directory); then, you link that to a script that sends the picture by email.

To do all this, type:

$ incrontab -e

at the command line to start editing your incron table. Now enter the following line into the file,

/home/pi/.secretdirectory IN_CREATE /home/pi/bin/surveillance_mail.sh youraddress@gmail.com "Intruders!!!" $@/$#

where .secretdirectory is the name of the directory you created previously for your Python script to store the pictures, and youraddress@gmail.com is your email address (it doesn't have to be Gmail), preferably one you monitor from your smartphone.

The strange symbols at the end are replaced with the route to the monitored directory ($@ = /home/pi/.secretdirectory) and the name of the file that triggered the event ($# = the picture the camera took and was stored in the directory).

The script you call is surveillance_mail.sh, which you will now write and use to send the email.

Buy this article as PDF

Express-Checkout as PDF

Pages: 8

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content