In this tutorial we'll show you how you can build your very own security camera that can detect movement, record a livestream and back up the video onto a server using a Raspberry Pi and a webcam.
We'll assume you have none of the required packages to follow this tutorial on video surveillance and video recording. You will use Motion which is the heart of this article. Aside from that, you will require Apache (or Nginx) and PHP.
Although this tutorial is geared towards using a Raspberry Pi, you can use another PC setup if you prefer. Do note, that if you go the Apache and PHP route, everything will work very easily without having to make extra changes to the server and PHP.
If you do decide to go with Nginx instead of Apache you will need to make some extra changes: such as installing PHP-FPM; changing the root folder path for web page files; and editing the following files: /etc/nginx/sites-available/default, /etc/nginx/sites-enabled/default and /etc/php5/fpm/php.ini.
Now, for the synopsis of each package. Motion will be used to record video after movement is triggered. The video clips will be written to a folder as Flash SWF files. However, Motion still allows you to see the location even without movement, much like a regular security camera.
Once you have those files, you may want to be able to sort through them effectively. Here is where the web server and PHP play their role. With the Apache or Nginx server, you can serve these files over the web.
Realistically, many files will be accumulated and you may want to create a loop with PHP in order to output each file into a link that can display the video in a popup. In which case a free video popup application, such as Shadowbox can be used. Lucky for you, the code can be downloaded and this contains all the files needed.
With all that covered, you'll have a setup that can deliver your videos. This tutorial will show you various options and their how-to counterparts. Since a camera like this could be used in your home as a security camera, you may want to password protect any web pages or the folder where you keep the videos.
Now, if someone did happen to break into your premises and decide to steal or wreck your Raspberry Pi, we'll also guide you through a backup plan that can be used to move your video files to a foreign web server that the robber won't have a clue exists.
Getting things to work
Since this article is about Motion, let's install this first from the Terminal in Raspbian, the operating system that powers your Raspberry Pi:
sudo apt-get update
sudo apt-get install motion
Now that one installation is out of the way, let's add the rest, which includes Apache sudo apt-get install apache2 and PHP:
sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt
To learn more about the apt-get command, check out our guide on how to master the Linux terminal with apt-get.
Let's move on and make some basic procedures and tests to see everything is working as it should. The main files which you will customise are /etc/motion/motion.conf and /etc/default/motion.
Open up motion.conf with your favourite editor. By default, you'll note that the parameters shown below are the opposite of the default values. For example, daemon off becomes daemon on:
Save the changes and open up the /etc/default/motion file and make the following changes:
Now, let's fine tune some options. Three changes that are needed are: the frame rate, quality and minimum amount of frames to trigger the motion to record:
Without changing this setting, two frames per second looks way too jerky and will miss out a lot of action, so we change the frame rate from 2 to 30 frames per second.
The second change is obvious since it's a quality upgrade. The third change sets the minimum amount of frames of motion that need to be detected. By default, the value is 1.
The problem with a number this low is that you can end up with unwanted recordings from things such as lights flicking. Keep in mind that you have many options and can look deeper into the features.
A good place to start is on the official Motion website. Some of the other features you might want to consider are: taking a picture at a desired interval, such as every second, every minute or every hour.
This feature makes it easy to host a live weather cam, for instance, or to determine if someone is sitting on your couch.