Best Raspberry Pi Projects
Our pick of the most fun and useful things you can do with your Raspberry Pi
Make a Raspberry Pi CCTV security system
There’s no need to spend a fortune on a fancy security camera system to keep your home or business safe. We’ll show you how to put together a surveillance system for less than £50 and still get the same high-quality footage and extra features. All you need is a Raspberry Pi and a cheap camera. You can see an example of footage captured by our PiCam here.
To start you’ll need a Raspberry Pi (around £28), a Raspberry Pi Camera Module (£20) and a fresh install of the latest version of Raspbian (either download it or select Raspbian via the NOOBS software that’s pre-installed on the SD card of many Raspberry Pi kits these days). Check out this video guide if you’re not sure how to attach the fiddly camera module to your Pi. If you’re not using NOOBS, use SD Formatter 4.0 to prepare the SD card, and Win32 Disk Imager to flash the downloaded Raspbian ISO file onto the SD card. Once the Raspbian operating system is installed, insert the SD card into the Pi’s card reader and power it up; you should select Expand the File System and Enable the Camera from the blue menu that appears, before restarting the system.
Once all the setup text has stopped scrolling down your screen, log in using the default user name and password of pi and raspberry. As is best practice, update the OS and its pre-installed applications by typing ‘sudo apt-get update’ and then ‘sudo apt-get –y upgrade’. Sharp-eyed readers will notice that the syntax for the second job has changed subtly since we last talked about setting up Raspbian – such is Linux, which is why Linux administrators try not to update installations once they get everything working satisfactorily.
Before installing the software that will handle the motion detection and recording, the final preparation is to ensure that your Pi is running the latest firmware version. Type ‘sudo apt-get install rpi-update’ and then ‘sudo rpi-update’. It’ll take a while for the new firmware to install, but once done you need to reboot the Pi by typing ‘sudo reboot’.
Remote control
Because having a CCTV camera with a keyboard and HDMI screen hanging off it is wildly impractical, we want to start accessing and controlling the Pi remotely as soon as it’s been successfully updated. This is possible via SSH (short for Secure SHell protocol), which is installed by default with Raspbian. To make SSH run every time the Pi starts up, type ‘sudo update-rc.d ssh defaults’.
Use PuTTY to remotely control your Pi from another PC
To access the Pi remotely from a Windows PC, you need to download an SSH client – we used PuTTY. Download PuTTY from the putty.exe link. Once the .exe. file has downloaded to your Windows PC, place it somewhere convenient, such as on your desktop. When you launch it, you might feel overwhelmed by the options on offer, but all you have to do is enter the IP address of your Pi into the Host Name (or IP address) field and click Open. To save time later, you might want to click Save first and give the connection a relevant name – we’ve chosen RPi. You can find the Pi’s IP address by either finding it in your router’s management web page (you might also like to ask the router to assign the Pi a static IP address while there, if the option is available) or else type ‘ip addr show’ into the Pi. Once you have the IP address you’ll be able to log in to the Pi via PuTTY with the default pi and raspberry user name and password, and once you have remote control of the Pi you can unplug its keyboard and screen.
Find the IP address of your Pi by typing ‘ip addr show’; the IP address is highlighted in the screenshot
In motion
Now that you can access and control your Pi from the comfort of your main PC or laptop, we can really get to work on making a CCTV camera – from here on in, we’ll do everything via PuTTY. The software that’ll do all the heavy lifting in this project is called Motion. Install Motion in the usual way with ‘sudo apt-get install –y motion’.
However, Motion hasn’t yet been updated to work with the Raspberry Pi Camera Module, so we have to install a few hacks and workarounds – our thanks to dozencrows of the Raspberry Pi forum for doing much of the legwork. You need to type the following into the Pi’s terminal, pressing Enter after every instruction:
cd /tmp
sudo apt-get install -y libjpeg62 libjpeg62-dev libavformat53 libavformat-dev libavcodec53 libavcodec-dev libavutil51 libavutil-dev libc6-dev zlib1g-dev libmysqlclient18 libmysqlclient-dev libpq5 libpq-dev
wget https://www.dropbox.com/s/xdfcxm5hu71s97d/motion-mmal.tar.gz
tar zxvf motion-mmal.tar.gz
sudo mv motion /usr/bin/motion
sudo mv motion-mmalcom.conf /etc/motion.conf
To explain what that all does, the first line moves you to the temporary /tmp folder – a good place to download temporary files to, as the folder gets emptied every time the Pi starts. The next line is horrible, but is a belt-and-braces approach to ensuring the Pi has all the correct video libraries and codecs; some people have reported that merely libjpeg62 (type ‘sudo apt-get install –y libjpeg62′) is sufficient, so you might want to try that first and revert to our lengthy install line should that fail. The next line, wget…, fetches dozencrows’ tweaked files for Motion and the line after unpacks the .tar file (a .tar file is similar to a .zip or .rar file, in that it can contain multiple files). Then the two ‘sudo…’ lines move the two unpacked files from the .tar file into the appropriate places.
All the above should mean that the Motion software will now run on your Pi, but you need to edit Motion to tell it to run every time the Pi boots. Type, ‘sudo nano /etc/default/motion’ to edit Motion’s startup behaviour, changing the appropriate setting to ‘start_motion_daemon=yes’ (the term ‘daemon’ is Linux-speak for ‘service’.
Unfortunately, you’re not quite done yet, as you need to set the correct permissions for Motion to automatically create files, save files and generally be of any use. Again, we’ve gone for the belt-and-braces approach, as hopefully this will mean that this guide works even if Raspbian gets updated between the time of writing and whenever you have a spare couple of hours to follow it.
sudo chmod 664 /etc/motion.conf
sudo chmod 775 /usr/bin/motion
sudo touch /tmp/motion.log
sudo chmod 775 /tmp/motion.log
cd /home/pi
mkdir motion
sudo su
chmod 777 motion
Again, this isn’t a script, just single instructions typed into the command prompt, followed by Enter after each line. The first four lines set some basic permissions, the next two lines create a folder called motion in the /home/pi folder, which is the one you usually view and use. The penultimate line gives you Super User rights and the last line means that Motion can save files in the folder /home/pi/motion.
The last stage in setting up Motion – at least in a basic state – is to edit a few settings in its configuration file. Type ‘nano /etc/motion.conf’ to edit the file using the Nano text editor and navigate to the logfile setting. You can do this quickly by using Nano’s search function; type Ctrl+W, type ‘logfile’ and press Enter. Change the entry to read ‘logfile /home/pi/motion.log’. Then find ‘target_dir’ in the same way and change the destination folder to also be /home/pi/motion. Lastly, find ‘stream_localhost’ and set that to ‘off’.
For other recommended settings, and pointers to more specialised options, see the Motion.conf section at the end of this guide. For the moment, we’re only going to ensure that Motion is working, so hit Ctrl+X, then Y to save the changes you’ve made, followed by return to keep the file’s name.
If you have to reboot the Pi, you’ll have to restart your PuTTY session (once the Pi has fully booted) and re-enter the username and password
Now that Motion will output to its own folder (/home/pi/motion) and will live-stream what the camera sees (via the stream_localhost setting) we can reboot the Pi and check Motion is running properly. Type ‘sudo reboot’ and while you’re waiting, download the Firefox web browser to your PC and install it without allowing it to become your main browser, unless you wish to switch. Once the Pi has rebooted, you should be able to enter its IP address followed by the port number of the stream_port setting (in our case, 192.168.1.6:8081, as port 8081 is the default) into the address bar of Firefox and see a live stream from the camera; unfortunately Chrome and Internet Explorer cannot handle this feed.
You can see a live stream from the Pi’s camera by using the Firefox web browser – just enter the IP address and stream port into the navigation bar
Another test to ensure that Motion is working is to look at the Camera Module – the red LED should light up very soon after the Pi boots. Now that the camera has seen some motion (your face, as you checked the red LED) you can log back into the Pi via PuTTY and check to see whether it’s saved anything in the home/pi/motion folder. Type ‘cd motion’ to enter the motion folder, and then ‘ls’ to list its contents. You should see a load of images and a video file. If not, check the motion.log file in the same directory to find clues as to what’s gone wrong. You can copy files from the Pi to your Windows PC by using WinSCP; in WinSCP, enter the Pi’s IP address, user name and password (‘pi’ and ‘raspberry’) and the rest is fairly straightforward.
Use WinSCP to copy files from the Pi to your Windows PC
As the red LED on the Pi Camera Module can cause glare and reflections or even tip off a burglar, you might want to disable it once you know that Motion is working correctly. Type ‘sudo nano /boot/config.tx’ and add the line ‘disable_camera_led=1’ at the end of the file. Press Ctrl+X, then Y and then Enter to exit and save.
You never what kind of intruder your motion-powered Pi-cam will detect
More on Motion.conf
Motion has a hundred options to fiddle and play with in its lengthy configuration file; there are some we’d almost always change, some we might change if we had a certain task in mind, and others we might fiddle with if we had plenty of spare time and a very specific aim. Remember that you can skip to each setting using the Ctrl+W search tool of the Nano text editor; to edit the configuration file you need to type ‘nano /etc/motion.conf’.
Setting | Comment |
width 1280 | The Pi’s processor can only handle so much data per second, so you have to balance resolution with frame rate and bit-rate. The tweaked version of motion also typically hates resolutions that aren’t either 4:3 or 16:9 in aspect ratio. |
height 720 | |
framerate 10 | |
ffmpeg_bps 400000 | |
pre_capture 2 | Includes a number of frames in the final video before and after the motion itself was detected – 2 is a reasonable number, as too many puts too much strain on the Pi. |
post_capture 2 | |
max_movie_time 300 | The maximum time, in seconds, that any recording can be. You would use this setting to save storage space, but if you’re not bothered, leave it at 0 (infinite). |
ffmpeg_video_codec msmpeg4 | Use the msmpeg4 setting to create videos that can be played in Windows Media Player. |
output_pictures off | No pictures will be created, only a video |
movie_filename %d-%m-%Y-%H-%M | Name the video file according to the convention dd-mm-yyyy-hh-mm |
rotate 180 | We always seem to set up the camera upside down, check via the live-stream before changing this setting. |
Other options include ‘text_right’ if you don’t like the default date order, or ‘threshold’ if you find Motion is too trigger-happy. Trawl through the motion.conf file and you’ll see some very odd uses for Motion. For example, the ffmpeg_timelapse option allows you to start a timelapse video as soon as motion is detected (say, the builders turning up in the morning). The External Commands, Warning and Logging section even allows advanced coders to trigger certain actions once motion has been detected, an image has been created or a video has finished recording. Team Motion with some Smart Home kit and you could have the hall light automatically come on once someone approaches the front door, whether that be yourself or a nefarious interloper.