Comprehensive CCTV systems can often be quite expensive particularly for smaller projects or just for home use, so this post was created to inform you on how to set-up a fairly cheap alternative using the Raspberry Pi, a webcam and some open source software.
This isn’t the best security solution in the world but it’s definitely a fun project to get stuck in to and does work well for people looking for a simple security solution.
The end result will be a Raspberry Pi running Minibian (a lightweight version of Raspbian) and Motion – a command line based tool that detects and records motion. All of this will be hooked up to an everyday USB webcam that provides both a live stream in your browser of choice and snapshots/video recordings when motion is detected.
- Raspberry Pi (I used the B model, however B+ should work fine too)
- USB Webcam
- Ethernet cable (Wifi dongle can be used too but some additional set-up will be needed)
- 5V power supply for Raspberry Pi
- SD card for Raspberry Pi
- Minibian image for Raspberry Pi
- SD Card Formatter
- Win32DiskImager (assuming you are completing this tutorial on Windows)
So let’s dive straight into it…
Format SD Card
The first thing you need to do is format your SD card ready to install the the Minibian image onto it.
To do this I use SDFormatter. Simply choose the right drive letter for the SD card, click ‘Options’, change ‘Format Size Adjustment’ to on and set ‘Format Type’ to ‘Full (Erase)’.
Then click format and wait for it to finish.
Next you need to grab yourself a copy of the Minibian image ready to move over to your Rapsberry Pi.
MINIBIAN is a minimal Raspbian-based Linux image for Raspberry Pi.
The main focus is to have a small, updated and stable distribution that is fully compatible with official Raspbian “wheezy” image, without GUI and unneeded tools.
So this image is perfect for embedded projects, or wherever you need to use all RPi resources for your specific tasks.
(Taken from the Minibian About page)
It is perfectly possible to use other images if you please but I have chosen Minibian as it is nice and lightweight so most of the Raspberry Pi power can be focussed on the CCTV system itself.
Install the Minibian image of the SD card
Now you need to install the Minibian image onto your SD card. I will explain briefly how to do this on Windows with Win32DiskImager. If however you are on Linux here is a nice tutorial of how to write the image to the SD card or alternatively here is a quick tutorial for Mac users.
Install and open Win32DiskImager as an administrator (right click run as admin) and point it toward the Minibian image file. Then make sure that the correct drive letter is selected for your SD card. I’ve added an image below of the configuration to help.
Once you’ve done this simply click the write button and wait for the success message.
Plug everything in and SSH into your Raspberry Pi
Once you have successfully written the Minibian image to your SD card it is now time to plug everything in, connect to your Raspberry Pi and start setting everything up, almost done!
So, attach the Ethernet cable, SD card, USB webcam and finally the power supply. Give it a second to boot up and then you can connect via SSH.
To connect to the Raspberry Pi we must first find the IP address of the machine. There are many ways to do this either through your router or through lucky guess work but to make things simple I usually just use the run only version of Advanced IP Scanner.
Next we are going to need Putty (if on Windows) to connect to the machine. After you have that installed simply add the IP address of your Raspberry Pi to the ‘Host Name (or IP Address)’ field (as shown below), then click open.
In the above example you can see that my Raspberry Pi’s IP address is 192.168.1.7, yours may well be different.
Now all you need to do is type in the default username/password which is root/raspberry.
Resize the SD Card
Next we quickly need to resize the SD card so that as much space can be utilised as possible. Here is a link to an easy tutorial on how to get this done.
Time to update
Run the following to update the system:
Install and configure Motion
To install Motion run:
apt-get install motion
Edit /etc/default/motion and set “start_motion_daemon” to yes
Now we need to configure Motion to do what we want.
This is a link to my configuration file if you want to use it as a template or of course you can use the default config and go from there.
If you do use my one as a template there are many settings that you might want to adjust for yourself. I will first let you know what this config file is set-up to do, then I’ll run you through the main settings that you may want to change.
This configuration is set-up to:
- Take a snapshot and save it as a .jpeg every hour on the hour.
- Record and save any detected motion as a video.
- Make a time-lapse by taking an image every minute and saving it as a file per day.
- Have a live stream that you can access locally in your browser at the Raspberry Pi IP address.
There is more to it than this but this covers the main purpose of the webcam. I’ve also edited the directory paths so that all of these are sectioned up into neat folders arranged by date (all of this is in the DD/MM/YYYY format so if you are not in an area that uses this format bear this in mind).
Make /media writable
All of the snapshots and videos are going to be saved in the /media directory so it is important to double check that the folder is writeable. To do this we use the following command:
chmod -R 755 /media
Here are the main settings in the configuration files that you might want to play around with a little.
The width of recordings / live stream:
I’ve set this to 768 but that is entirely up to you.
The height of recordings / live stream:
The target directory for saving recordings. You may want to change this to be an external HDD or possibly a USB but do remember to double check the permissions of the new directory if you do decide to change it.
Maximum number of frames to be captured per second:
Amount of frames to capture before and after motion is detected:
That’s about it for the main ones as everybody will want a different set-up. If you want to look into it some more here is a link giving you a comprehensive list of all the config options.
Finally, we want to set-up FTP access so that it is nice and simple to download and view all the videos/images on the Raspberry Pi.
Here is a very quick tutorial on how to do this and will get you up and running in no time at all.
Take a look at the webcam live stream
Looks like that is all for now. If you want to access the live stream of your webcam simply head over to your Raspberry Pi’s IP address using port 8081 (as defined in Motion’s config file). For example mine would be 192.168.1.7:8081.
A couple of handy links for further configuration:
Overclock your Raspberry Pi:
Making a new user:
Changing root’s default password:
And lastly, if you are looking for a more comprehensive but still greatly priced CCTV system with much less configuration then be sure to take a look through some of our great security packages.
If you need any help with this tutorial or have a general question feel free to comment below!