How to build a Raspberry Pi security camera

CCTV

One of the most endearing things about the Raspberry Pi is how it can be used as the computational brain for all most any tech project. Its open-source nature means it has been used to create tiny PCs to being used as a form of Hadoop data base cluster; there's a huge swathe of projects that have used the micro computer at their core.

For the security conscious, the Raspberry Pi can be used to power a fledgeling home security camera system, providing one is willing to get involved in a little bit of DIY coding.

Choosing your hardware

With the Raspberry Pi you can create a basic security camera to tinker with, but there's also scope to create a more fully-fledged system should you so desire.

That means there's an element of choice in how you approach a security camera project; you'll not only need to decide which Raspberry Pi model to go for, which will dictate the size of the camera system, but you'll also need to consider how it will connect to your home network and what type of camera module you'll want to use.

For example, a USB webcam will work as a makeshift security camera when connected to a Raspberry Pi, but you can get the official camera board to get more out of the security setup, as well as avoid any incomparability issues than might pop up.

There are two official options for this: the Raspberry Pi Camera v2 and the Raspberry Pi NoIR Camera v2, the latter being specifically designed for low-light applications. They both feature an 8 megapixel Sony IMX219 image sensor, which supports 1080p video at 30fps, 720p at 60 and 640x480p at 90fps. They connect direct to the Pi via a dedicated ribbon socket, and are supported natively in Raspbian. In short, one of these is your best choice, depending on whether you're planning mostly daytime video, or a significant amount of night/twilight work.

If you're planning a security camera system with dedicated hardware, then the cheaper Pi Zero W paired with one of the many night-vision kit cameras is worth considering, as this option keeps hardware costs, size and unwanted additional components to a minimum. Note, though, that the Pi Camera v2 needs an additional adaptor to work with the Pi Zero range.

Aside from your choice of Pi and camera combo, you'll need the usual USB power supply, an 8GB minimum SD/Micro SD card with Raspbian installed and either a monitor/keyboard/mouse or remote access enabled. Although you'll be able to get this project up and running without a case, you'll need to buy or build a suitable item depending on where you plan to monitor - weatherproof options are usually a good idea unless they''ll be living exclusively indoors.

Also bear in mind that motion-based camera systems can generate large volumes of video, which will require large amounts of either local or network storage if you want to view it later. This project can accommodate either, depending on your needs.

Once you've connected everything up - and carefully clipped the camera board in place - you're ready for the software bit. There are two simple routes to go down here - either install Motion and tweak it to suit, or go for an all-in-one option, like MotionEye, a Linux distribution that turns a single-board computer into a video surveillance system. The OS is based on BuildRoot and uses Motion as a backend and MotionEye for the frontend.

We'll go with MotionEye here, as it rounds off some of the corners without adding much in terms of complexity.

Downloading and writing MotionEye image to disk

First, download the latest version of MotionEye for your device, extract the image file from the archive and write it to your SD card. You can use Etcher.io on Windows, or in Linux there is a writeimage.sh script that will do everything for you.

Just run the script as follows (replacing the arguments with appropriate values):./writeimage.sh -d /dev/mmcblk0 -i "/path/to/motioneyeos.img"

Note that you'll need to specify the device path to the disk and not to some partition (for example /dev/mmcblk0 instead of /dev/mmcblk0p1).

You can preconfigure a wireless network connection - or a static IP if you'd rather not use DCHP - with the following command. Just use this command for Wi-Fi:./writeimage.sh -d /dev/mmcblk0 -i "/path/to/motioneyeos.img" -n 'yournet:yourkey'

...and this command for a static IP:./writeimage.sh -d /dev/mmcblk0 -i "/path/to/motioneyeos.img" -s "192.168.1.101/24:192.168.1.1:8.8.8.8"

Boot and setup

This complete, you're ready to boot the image. This will take a few minutes as the system auto-configures. Your motionEyeOS needs an IP address before you can communicate with it so you'll have to use the ethernet connection with DHCP enabled, unless you have preconfigured a static IP address.

As soon as your motionEyeOS setup is complete, it will listen on port 80 and present you with a web user interface - simply enter the IP address of your board in the address bar of your browser. Use your router to display devices on your home network, or use a network scanner to hunt for a device called "meye-".

Once found, you'll be able to click on the user icon on the upper-left side of the page to switch user to administrator. Use admin with no password when asked for credentials - the user setting is for remote monitoring, the admin for changing settings.

MotionEyeOS has a broad range of settings to play with, but the first thing you'll want to do is set up passwords for both admin and user, set the timezone and enable wireless. After that, choose the desired resolution, frame rate, and other details of your video feed, and configure file storage if you want your pictures and movies saved on a network or USB drive.

You'll be able to monitor your camera remotely (including on a smartphone) by visiting the relevant IP, inputting your 'user' login, and then using the web interface. The interface also allows you to browse, preview and download videos and images from each connected camera by using the media browser window which opens by clicking on the pictures or movies buttons.

This project gives you the beginning of a great security camera system which can scale up to monitor almost any situation, and is far more flexible than most comparable-price commercial systems. In addition, the Pi and Camera board can be the basis for a wide range of other projects too, including wildlife cameras or timelapse photography.

Thanks to Calin Crisan for his MotionEyeOS