Motion Detection Surveillance System with raspberry pi

One of these days, the disc brake lock of my bike couldn’t unlock. My first thought was that somebody tried to steal the bike. Probably this isn’t the case, but I took some immediate measures to restore my inner peace again.

I used to have a windows laptop running yawcam (Yet Another Webcam Software) and recording images when motion was detected. But this laptop is now dead and I don’t use Windows at all anymore, so I had to find a Linux equivalent.

To my surprise, there is an application called motion, which is a full-blown suite of streaming, motion detection with a lot more capabilities than yawcam. It is like yawcam’s bigger brother.

There are millions of tutorials out there on how to setup your system, but I found it a lot easier reading the manual and the configuration files of motion to set it up. It is straightforward and easy if you avoid being a copy-paste monkey and you have a basic understanding of Linux.

So this is not going to be a step by step guide, you can find millions of them out there. This is going to be a tips and tricks guide so that you can get the most out of your equipment.

Also this is how I did it. It is NOT the cheapest, easiest, best way or any of that. It just happened for me to have some equipment, buy some extra and set it up in a couple of hours.

But enough of the disclaimers. I used:

  • one cheap no-name webcam compatible with Linux
  • one Logitech HD (720p) webcam
  • one Macro Lens, one Macro Lens combined with a fish eye.
  • one raspberry pi 512mb running Arch Linux
  • one usb cable extender

What happens when motion is detected, is that images are being recorded (along with a video for the event) and stored locally on the raspberry pi.

Then every 10 minutes, a script uploads all the new pictures to a server where further processing can take place. Now for further processing the possibilities are endless and I’ve got plenty of ideas but nothing concrete yet.

From having a private website or app that you can browse through the activity after receiving alerts, to recording faces and building a database with facial recognition results, separating the strangers from friends. Or running ANPR (Automatic Number Plate Recognition) software and seeing who reverses at your drive because it is “convenient” nearly dropping your bike.

Since I am no law expert, I don’t know how legal are these ideas if they get implemented, so they’ll have to stay in my head for a while.

But imagine how great it would be, having in your database all these people that go around these weeks trying to persuade you to vote for the Labour party. You could activate a pre-recorded message stating all the reasons why you won’t vote for the Labour party and kindly ask them to move on and never come back again.

Anyway, let’s get on with the project for now.

Installing and configuring motion

Installing motion on an Arch Linux raspberry pi, is pretty straight forward. Just type in:

sudo pacman -S motion

If you don’t use Arch Linux but some other flavour, it is going to be either:

sudo apt-get install motion

Or

sudo yum install motion

Now that you installed motion, it is time to configure it.
Generally, motion has got the capability of running multiple threads – cameras simultaneously. This is why apart from the main configuration file, you can have small configuration files for each camera.
The main configuration file can be found at

/etc/motion/motion.conf

For extra threads, just create a thread1.conf, thread2.conf etc.
After finishing with the main configuration file, I will show you my thread configuration files.

To start, switch the daemon off temporarily so that debugging can be significantly easier.

daemon off

Then, make sure, if you have got a USB camera that input is set to 8:

input 8

Afterwards, adjust the width and height of the image. I’ll have to warn you though, that there are going to be issues if you try running more than one camera in good resolution. This is normally because your USB ports belong to the same bus and the controller can’t handle the bandwidth. This was the most annoying part of the installation for me, tweaking the resolution in order for it to work without crashing.

I ended up using two linux boxes, one running Ubuntu Server, the other one running Arch Linux, in order to have at least 640 x 480 px resolution for the one camera and 720 x 576 px for the other one.

I kept the defaults when it comes for motion detection settings, but on one camera I got rid of area number 9 (down and right) of the grid because my flatmate’s motorbike cover shakes like a bloody sail as well as number 1, 2, 3 (upper three grids) because there are trees in the picture that move when it is slightly windy. I wanted to avoid having false positives.

This means, that I changed the variable area to the following value:

area_detect 45678

Other than those tips, it is mostly trial and error along with previous experience when it comes to tweaking. Every installation is a bit unique and you should fine tune it to cover your individual needs.

Don’t forget to change the target directory and filename pattern to those of your choice.

target_dir /home/user/motion/cam1

Then, don’t forget to set a password if you are not restricting the http control to localhost as well as choose a port for the webcam stream.

After that, there are a lot of extra options to run scripts or write events in a database when detecting a motion, but I’d recommend to leave this for later.

Now, at the end of the configuration file, there are four commented out parameters which include the additional configuration files if you are running more than one camera simultaneously on one computer.

As I promised, here is my mini configuration file for a camera:

# /usr/local/etc/thread1.conf
#
# This config file was generated by motion 3.2.12 



###########################################################
# Capture device options
############################################################

# Videodevice to be used for capturing  (default /dev/video0)
# for FreeBSD default is /dev/bktr0 
videodevice /dev/video0

# The video input to be used (default: 8)
# Should normally be set to 1 for video/TV cards, and 8 for USB cameras 
input 8

# Draw a user defined text on the images using same options as C function strftime(3)
# Default: Not defined = no text
# Text is placed in lower left corner
text_left CAMERA 2


############################################################
# Target Directories and filenames For Images And Films
# For the options snapshot_, jpeg_, mpeg_ and timelapse_filename
# you can use conversion specifiers
# %Y = year, %m = month, %d = date,
# %H = hour, %M = minute, %S = second,
# %v = event, %q = frame number, %t = thread (camera) number,
# %D = changed pixels, %N = noise level,
# %i and %J = width and height of motion area,
# %K and %L = X and Y coordinates of motion center
# %C = value defined by text_event
# Quotation marks round string are allowed.
############################################################

# Target base directory for pictures and films
# Recommended to use absolute patch. (Default: current working directory)
target_dir /home/ahughes/motion/cam2


############################################################
# Live Webcam Server
############################################################

# The mini-http server listens to this port for requests (default: 0 = disabled)
webcam_port 8082

# Command to be executed when a picture (.ppm|.jpg) is saved (default: none)
# The filename of the picture is appended as an argument for the command.
on_picture_save /usr/local/motion-extras/camparse1.pl

# Command to be executed when a movie file (.mpg|.avi) is closed. (default: none)
# Filename of movie is appended as an argument for the command.
on_movie_end /usr/local/motion-extras/mpegparse1.pl

One thing that you shouldn’t forget, is allowing hosts apart from localhost accessing the video stream (if you want to be able to view the camera stream from other machines. The configuration file gives more than clear instructions on achieving that.

One thing I generally do in order to always access the infrastructure operating at my house, is tracking my external ip and automatically updating my A records for the ahughes.org domain.

Consider this a more advanced dyndns. I use rackspace’s DNS API which is free of charge, as I am already a rackspace customer. You can achieve the same thing using one of the alternative free dyndns services out there. As far as I know, I think dyndns stopped their free service, but there should be alternatives out there.

Don’t forget to do port forwarding to the machine running motion. Every router tends to have a different interface (rather annoyingly), but if I can achieve it on my BT router (probably the worst out there) you can achieve it on yours.

And finally, for now, some pictures that I gathered so far after installing this surveillance system:

Caught by my own camera and I don't look particularly happy
Caught by my own camera and I don’t look particularly happy
This is me leaving for the office probably
This is me leaving for the office probably on the fazer aka pacman

If you are interested in setting up a system like that yourself and need help or have any questions, ping me.

You know where to find me.

Cheers!

Leave a Reply