Of the many projects that you can build with the Raspberry Pi, one of the most interesting and permanently useful is the motion capture security system.
All you need is the usual Raspberry Pi setup, a webcam and an optional battery pack, along with a nondescript case for concealing the security system.
With this setup, you can keep an eye on your property from a remote location, receiving alerts if anything is captured by the webcam and a link to the footage if necessary.
Compared with the price of off-the-shelf security systems, this is remarkably affordable and portable, and as you’ll see below can be setup in minutes.
What You Need for Your Motion Capture Security System
There are several approaches for building a motion capture security system for the Raspberry Pi. I initially followed blogger Kean Walmsley’s suggestion to use an ARM-based distro of Arch Linux rather than the usual Raspbian.
However, this just didn’t seem to come together, so I reverted to the Raspbian OS, and made use of the camera drivers and motion detection package, which we’ll come to in a bit.
In addition, you will also need a Raspberry Pi, a formatted SD card, a webcam (see elinux.org/RPi_USB_Webcams for compatible devices) and a power supply; you will probably need a powered USB hub too, as many webcams need their own power supply when connected to the Pi. This might be a powered USB cable, or a battery solution for the Raspberry Pi, of which there are many options, most of which you can find via the eLinux wiki. Note that there are alternatives, however, such as 4x or 6x rechargeable AA batteries. Some portable iPad chargers will also work – but check your charger’s model number online before attempting to connect to your Raspberry Pi)
If you don’t already have a webcam, you might prefer to spend a few dollars on the official Raspberry Pi webcam.
Follow the instructions outlined previously on MakeUseOf to install Raspbian What You Need to Know About Getting Started with Raspberry Pi What You Need to Know About Getting Started with Raspberry Pi The Raspberry Pi computer is the latest in a line of compact, multi-function devices, shipped as a barebones package so that end users can use the hardware for virtually any computing project that comes to... Read More , and for the best results, ensure that it is set up with SSH Setting Up Your Raspberry Pi For Headless Use With SSH Setting Up Your Raspberry Pi For Headless Use With SSH The Raspberry Pi can accept SSH commands when connected to a local network (either by Ethernet or Wi-Fi), enabling you to easily set it up. The benefits of SSH go beyond upsetting the daily screening... Read More . You should also change the password, using the raspi_config menu Optimize The Power Of Your Raspberry Pi With Raspbian Optimize The Power Of Your Raspberry Pi With Raspbian As befits a man with too much tech on his hands, I’ve been playing with my Raspberry Pi recently, configuring the device so that it works to its fullest potential. It continues to run as... Read More .
Install the Required Software
With the Raspberry Pi booted up and with an SSH connection open from your main computer 4 Easy Ways to Use SSH In Windows 4 Easy Ways to Use SSH In Windows SSH is the de facto solution for securely accessing remote terminals on Linux and other UNIX-like systems. If you have a remote SSH server you want to access, you'll have to download an SSH client.... Read More (you can use a keyboard on your Pi, but SSH is better as future communication with the security setup will need to be performed this way) you should begin the process of downloading the latest update:
sudo apt-get update
Next, install the emacs editor, agreeing to the prompts:
sudo apt-get install emacs
Following this, you will need to install a VNC server. This will enable you connect to the security system and view the output. Observe and agree to any prompts.
sudo apt-get install tightvncserver
After installing VNC Server, enter the command vncserver – you will be required to set a password to use the system. You should also agree to the subsequent request to enter a view-only password.
The next step is to edit the VNC Server initialising script. Enter the following to switch to the correct directory:
cd /etc/init.d
You can then run emacs:
sudo emacs tightvncserver
With the console open, copy the following:
#! /bin/sh
# /etc/init.d/tightvncserver
#
# Carry out specific functions when asked to by the system
case "$1" in
start)
su pi -c '/usr/bin/vncserver'
echo "Starting VNC server "
;;
stop)
pkill vncserver
echo "VNC Server has been stopped (didn't double check though)"
;;
*)
echo "Usage: /etc/init.d/blah {start|stop}"
exit 1
;;
esac
exit 0
Right-click to paste it into emacs. A few moments later, it should auto-save. Exit by pressing Ctrl+X and agreeing to save the file upon exiting.
You should then modify the permissions for the VNC Server directory, making the new script executable:
sudo chmod +x tightvncserver
The following command will instruct the VNC Server to load when you boot the Raspberry Pi Security Centre:
sudo update-rc.d tightvncserver defaults
We’re nearly done, so end the current VNC session:
sudo pkill Xtightvnc
You are then ready to restart it:
sudo /etc/init.d/tightvncserver start
On your main device – the computer with which you plan to observe your webcam – you can now open your VNC client. Various suitable clients are available – I’ve used the TightVNC client for Windows.
Configuring Your Webcam
We’re now at the stage where we can begin to prepare the Raspberry Pi for your webcam, which means installing drivers, the necessary libraries and capture software. Enter the following commands in turn, paying heed to the prompts.
First, it’s the motion detection software:
sudo apt-get install motion
Next, install the required libraries:
sudo apt-get install libv4l-0
UVCcapture is software designed to capture the feed from a webcam:
sudo apt-get install uvccapture
You can now attach your webcam and check that all is working. Enter the following command:
dmesg | tail
This will output the details about your camera model, as below.
We then need to set the motion capture, so run:
emacs /etc/default/motion
…and set start_motion_daemon to yes.
If you have problems with permissions here, use:
sudo chmod 777 /etc/default/motion
Remember to undo this after the edit has been saved.
With this change saved (CTRL+C, following the on-screen prompts to save), you will then need to edit the motion.conf file.
emacs /etc/motion/motion.conf
The script below (taken from a Raspberry Pi forum thread “Installation Guide for motion detection with webcam“) should be added to motion.conf:
set "daemon on"
set "minimum_frame_time 5" (this can be modified, depending how often you want to take picture)
set "pre_capture 2"
set "post_capture 2"
set "output_normal on"
set "quality 100"
set "ffmpeg_cap_new on"
set "ffmpeg_timelapse 30"
set "ffmpeg_variable_bitrate 2"
set "get_dir /media/webcam/motion"
set "webcam_port 8080"
set "control_port 8081" (important)
set "webcam_localhost off"
set "width 320" (important)
set "height 240" (important)
Note that these settings can be configured as necessary. Those labelled “(important)” should be left as they are.
Configuring Media Settings for Motion
We’re almost done, with just a few media settings and configuration of your wireless dongle to go.
Motion detection images need to be saved to the media folder – but first, you will have to change read/write permissions:
sudo chmod 777 /media
Next, launch the motion software:
sudo /etc/init.d/motion start
Motion can be stopped with:
sudo /etc/init.d/motion stop
You should see a message confirming that the software is running. Is it talking to your webcam? Run the following command to check:
tail -f /var/log/syslog
The output should confirm that the device is live and receiving data. The final step here is to boot the VNC client on your desktop computer and connect to your Raspberry Pi (run ipconfig to check the IP address) using the :1 suffix, which indicates X session number one.
Boxing Up and Positioning the Motion Sensor Security Camera
Although you might prefer to remove the webcam from its stylised case for improved portability and easier secretion, it’s best to assess the options for positioning before doing this.
Indoor testing will help you to establish the camera’s range and motion capture sensitivity, useful for when it comes to setting the device up outside.
For external use, you’ll need to use a suitable waterproof case, perhaps an old ice cream tub or a hobby box from an electronics store. Some drilling may be required in order to run your power cable to the mounted Raspberry Pi-powered security camera if you want to position it outside. However, you may also consider a battery-powered solution for your little computer (note that it will need regular recharging, however). Ethernet may be preferred under the circumstances, so you might want to go into full DIY mode and apply some mastic gun waterproofing, but if you want to use wireless networking, you can. Using a wireless repeater might assist with signal strength outdoors.
Post-Positioning Checks
With the RPi in position, you should head back to your computer and check everything is as it should be.
Begin by checking that the network connection is up and running. Although this should have been checked earlier, your wireless dongle may have trouble connecting in windy weather or if you have some unusual architectural structures or materials Wireless Feng Shui: How to Optimize Your House For Best Wi-Fi Reception Wireless Feng Shui: How to Optimize Your House For Best Wi-Fi Reception Setting up a Wi-Fi network should be an easy prospect, shouldn't it? I mean, a house is a closed-in box, and you'd think when you place a device that transmits wireless signals in all directions... Read More .
With the connection established, you should also head outside and run through several motion capture scenarios to ensure that your movement is detected and alerted.
Finally, for the first day of testing, keep an eye on the battery use if you have chosen a portable power source, as you will need to know how much recharging is required. Be aware that if you have the camera positioned indoors or relatively close to a powered out-building that using mains power is a better solution.
Conclusion: Home Security Solution with an Educational Tool!
Who would have thought that you could do so much with a case-free, stripped down mini-computer ostensibly designed to teach children programming with tools like Scratch?
Like the media centre solution Entertainment On The Cheap: The Most Affordable Ways To Set Up A Home Theater Entertainment On The Cheap: The Most Affordable Ways To Set Up A Home Theater While plenty of pricey pre-built solutions exist, there are a few ways to bring a streaming box of entertainment to your living room on the cheap. With the right some open source media software, a... Read More and the other remarkable uses, setting up your Raspberry Pi as a home security solution with motion capture is that it is something that can be done either with a single device co-opted into security whenever you head out, or with an affordable dedicated device.
The possibilities of the Raspberry Pi are seemingly endless. Find out more in our Raspberry Pi Unofficial Manual Raspberry Pi: The Unofficial Tutorial Raspberry Pi: The Unofficial Tutorial Whether you're a current Pi owner who wants to learn more or a potential owner of this credit-card size device, this isn't a guide you want to miss. Read More .
Explore more about: Home Security, Raspberry Pi.
Does anyone else have issues with the Pi shutting off after a while?
It worked great, then after a couple days, it stopped working. The red light is on, but I can't access it.
I was told that this may be a power issue. But I wanted a second oppinion.
Older Pis (and versions of Raspbian) often go into a low power mode after a while. Connecting a keyboard and tapping a couple of keys can help, as can finding a script to stop it changing modes.
I haven't had this problem with RPi2 and 3 using SSH however, only on the original Model B
Hope this helps.
hello i just did this project and i do not have any images. i have checked everywhere and still nothing, please help !
what output does the command line give when you begin?
Also, not that this project is two and a half years old. You should be able to find some far better solutions for a Pi security cam.
everything works but there is no images. do you know where i could find another security camera project using a webcam and not the pi camera module?
HELLO
I just did this and it works just don't know how to stop it. Have 11000 picture just tested it with both my webcams. Is it possible to do this as a program ? With option to delay the start or steam the video to your phone other computer or have email alerts etc ? Have just purchase my Raspberry pi this week and one of the project I got it for was to have a cctv type camera in my flat. But don't have a clue as to how to start writing programs yet !
Cheers
PB
I can't password after write''vncserver''.. what is password? Thanks
The password is user-configurable. When you first setup vncserver, it asks you to set a password. It's not asking you for an existing one :)
Can't make it work.
I followed the instructions but when starting the TightVNC Viewer in Windows, when connecting, it displays: "Xsession: unable to start X session --- no "/home/pi/.xsession" file, no "/home/pi/.Xsession" file, no session managers, no window managers, and no terminal emulators found; aborting."
Where did it fail?
Thanks!
Hi John
It's come to my attention that some of this tutorial is no longer relevant, especially if you've run an update or installed a more recent OS build. For the best results I would suggest checking the RPi forums at raspberrypi.org/forums or XDA-developers.com
Hi
I have followed the instructions
When i get to the:
The next step is to edit the VNC Server initialising script. Enter the following to switch to the correct directory:
cd /etc/init.d
stage the error "bash: cd: /ect/init.d: No such file or directory
Any help would be appreciated
All the previous steps work exactly as described
Thanks
hi Christian,
I had gone through your advice. But I got some error.
I am using Logitech C210 webcam for motion software to make motion activated cam. After connecting I ran "dmesg" command. My Kernal version is 3.6.117. Please help me.
[5.790301] Linux video capture interface: v2.00
[6.213422] uvcvideo: Found UVC 1.00 device. (046d:0819)
[6.610378] input: UVC camera (046d:0819) as /devices/platform/bcm2708_usb/usb1/1-1/1-1.2/1-1.2:1.0/input/input2
[6.856060] usbcore: registered new interface driver uvcvideo
[6.996478] USB Video Class driver (1.1.1)
[ 7.796439] usbcore: registered new interface driver snd-usb-audio
[10.301633] EXT4-fs (mmcblk0p2): re-mounted.Opts : (null)
[10.822316] EXT4-fs (mmcblk0p2): re-mounted.Opts : (null)
[ 23.426901] Adding 102396k swp on /var/swap. Priority:-1 extents:1 across:102396k SS
[ 26.301843] uvcvideo: Failed to query (SET_CUR) UVC control 3 on unit 2: -110 (exp.2)
[ 26.601848] uvcvideo: Failed to query (SET_CUR) UVC control 7 on unit 2: -110 (exp.2)
[ 26.601848] uvcvideo: Failed to query (SET_CUR) UVC control 2 on unit 2: -110 (exp.2)
[ 32.931814] uvcvideo: Failed to set UVC probe control : -110 (exp. 26).
[ 37.931908] uvcvideo: Failed to set UVC probe control : -110 (exp. 26).
[43.941808]uvcvideo: Failed to set UVC probe control : -110 (exp. 26).
[48.841809] uvcvideo: Failed to set UVC probe control : -110 (exp. 26).
[540.830427] uvcvideo: Failed to set UVC probe control : -71 (exp. 26).
[541.059081] usb1-1.2: USB disconnected, device number 4.
[544.621932] usb 1-1.2: new low-speed USB device number 6 using dwc_otg.
Does this work with the raspberryPi cameramodule?
In theory, yes. You'll have less messing around with camera drivers, of course, but a quick Google should point you in the right direction.
Great tutoriol.
Only i connot see the camera...
Where do i have to look for the problem?
Use nano instead of eMacs - it's already installed, and standard copy paste should work over SSH. CTRL-X, Y to save the file.