How to build a Raspberry Pi security camera
Detect and record movement
Changing all parameters to suit your specific needs is very easy and the motion.conf file will often have nice, self-explanatory comments while the website and man page have more information to offer.
Obviously, this service doesn't do much without a working, compatible webcam and so check out this list of webcams worth trying with the Raspberry Pi.
Using a plug and play webcam makes life easy for this task, and one cheap, readily available webcam that works is the Logitech C170. Note: If you are using the Raspberry Pi, the Raspberry Pi camera won't work with Motion.
To tell if the USB webcam connects OK, run the command lsusb. At this point, you will likely have a working webcam, a working web server and an adequate Motion configuration.
This is good, but you'll also need to create a folder for the images and set ownership for Motion. By default, Motion drops the images and SWF files into the /tmp/motion folder. It won't create the folder, therefore, you will need to:
cd /tmp
mkdir motion
Get the best Black Friday deals direct to your inbox, plus news, reviews, and more.
Sign up to be the first to know about unmissable Black Friday deals on top tech, plus get all your favorite TechRadar content.
chown motion:motion motion
Now, let's see how everything works. To start with, you can get Motion up and running with the command:
service motion start
and you can always restart it with the command:
service motion restart
The first thing you'll need to do to test that everything works fine is to see if you can view the default web page. Since your Raspberry Pi will have a unique network address, you can just type it in the web browser.
For example, if the Raspberry Pi is connected to a router with the IP of 192.168.0.1, your device could have an IP like 192.168.0.106. Thus, the URL would also be http://192.168.0.106. If you have success returning the default web page, you will see a message stating that everything is working properly.
If not, you will get a typical browser error which makes it obvious that something is not quite right.
Now with a working server, let's move on and get down to viewing and recording video. You can test the video in your browser by typing your network IP and port. By default, the Motion webcam port will be 8081. Therefore, if you type http://192.168.0.106:8081 in the browser, you should see your video stream.
A simple setup like this can be beneficial and have many uses aside from security: such as keeping an eye on a newborn while you're working in another room. Since all should be well at this point with the Motion service running, you can now go in front of your webcam and jump around a bit.
Actually, hand waving will suffice but a few jumping jacks aren't going to hurt. After that, you should be able to browse the new motion folder and see a bunch of JPEG files and at least one SWF file.
Now, that you have a motion-detecting device that you can view from your local network, you can move on and make adjustments so that you can see and view your webcam from outside your local network. For some of you, this may be the setup you desire; especially if your webcam and Raspberry Pi are well hidden and unlikely to be tampered with.
However, in addition to storing data in your own home, we will explain how to back up the files to another server for safe keeping, just in case your SD card or hard drive fails, or someone decides to steal, break or ruin your webcam (or webcams) in your home.
Making it web friendly
The whole idea here is to record, store and manage video from a website or by using your IP address that was given to you from your ISP. To find out your IP head to the handy WhatIsMyIPAddress website. In order to broadcast video out, you'll need to set up port forwarding on your router to enable your network IP to use port 8081.
While you are at it, you may as well do the same for port 80 since this same network IP will be used to display web pages from computers outside your local network; such as your friend across town or a loved one overseas.
After you have made the previous changes to your router's settings, try typing http://my_ipaddress_from_isp and http://my_ipaddress_from_isp:8081. You should get the same results as you did when you were checking your local IP.
The next step is to clean this up and view organised data view web URLs; such as http://my_ipaddress_from_isp/video.php or to view it from another website using an iframe.
In order to show the webcam from the page video.php, you just need to use an img tag with the network IP and port. Have a look at the code below, which shows this in complete detail and displays it with the default width and height as specified in the motion.conf file:
Now, let's imagine a scenario where you want to stream this livecam from another website using an iframe. Well, all you have to do is make an iframe from another page on the different server. The simple one-liner is shown below.
The next set of code will explain how to display the files that have been saved after motion is detected and recorded.
With that said, we will move on and create a simple way to organise the saved files that recorded the movement. The first detail that will need to be changed is to save the images and SWF files into a folder within the web directory.
The root web folder is located at /var/www/html or /var/www. At this point, a light should go off since you have already made several changes to the Motion setup. Reopen the /etc/motion/motion.conf file and change the target directory. By default, the target directory is located at /tmp/motion.
The new target is /var/www/images:
target_dir /var/www/images
Current page: Configuring motion
Prev Page Getting started Next Page Viewing and backing up the video and images