Building a Secure and Customizable Raspberry Pi Security Camera: A Comprehensive Guide

Building a Secure and Customizable Raspberry Pi Security Camera: A Comprehensive Guide

Aug 22, 2024

Raspberry Pi security camera setup with motion detection display, USB webcam, and connected components

A Raspberry Pi security camera is an affordable and flexible surveillance solution for homes or small businesses. Using a Raspberry Pi for a security camera offers several advantages, making it easy to customize and expand based on your needs.Using a Raspberry Pi for a security camera offers several advantages, making it an affordable surveillance solution — similar to how a Raspberry Pi can be used for smart home automation projects like Raspberry Pi Home Automation: The Ultimate Guide to Building Your Smart Home System — which shows the versatility of Raspberry Pi applications.
The Raspberry Pi is cost-effective, allowing you to build a fully functional security system without the high price tag associated with commercial options DVR/NVR systems. Its flexibility and customizability mean you can tailor the setup to your specific needs, making it perfect for a DIY home surveillance system. whether you want to use multiple cameras, integrate motion detection, or connect it to other smart home devices. Additionally,The wide range of software options, such as MotionEyeOS or the motion software on Raspberry Pi OS, can help you build powerful DIY projects — as discussed in our Raspberry Pi Operation System: Complete Guide to Versions, Features, and Setup article, allows for easy setup and management, giving you control over features like streaming, recording, and alerts.
We can choose to store video footage locally on an SD card, external hard drive, or network-attached storage (NAS), ensuring that your data remains under your control. Also, we can integrate cloud storage solutions like Google Drive, Dropbox, or custom servers for remote access and backup.

Implementation Raspberry Pi Security Camera

Comparison of Raspberry Pi 4 using a USB 720p webcam versus a CSI camera module for building a security camera system

Components

To build a Raspberry Pi security camera, you’ll need the following components:Raspberry Pi (any model with a camera interface e.g., Raspberry Pi 3, 4 or Zero W)
https://github.com/motioneye-project/motioneyeos/wiki/Supported-Devices
● Raspberry Pi Camera Module v2.1 (or a USB webcam)
● MicroSD Card (with at least 16GB storage, pre-installed with MotionEyeOS)
● External USB Hard Drive (with a least 500GB for video storage)
● High Quality Power Supply 5v

Steps to Set Up the Raspberry Pi Security Camera

1. Install Raspberry Pi Imager to a computer with an SD card reader. Put the SD card that you will use with your Raspberry Pi into the reader and launch Raspberry Pi Imager.
2. Select the Raspberry Pi 4 as Device
Choose OS and select Use Custom. Browse the MotionEyeOS image (available on the official GitHub releases page) and flash the Raspberry Pi

Raspberry Pi Imager interface showing device selection, operating system choice, and storage option for setting up Raspberry Pi 4

3. Complete your custom setting (WiFi Setttings)
4. Put SD Card in Raspberry PI and power on.

MotionEye on Raspberry Pi

This is an online interface of the software motion, a video surveillance application with motion sensor.

Logo and title graphic for Motion and motionEye, open-source software tools for video surveillance with Raspberry Pi

Features

Easy and friendly user interface for internet verses, mobile and tablets.
● It supports most USB cameras and also is compatible with Raspberry PI camera modules.
● Here we find support for IP cameras (network).
● Camera detection with alerts via email and functional days.
For still images using the JPEG format and for videos using AVI format.
● Timelapse movies
● It worked with your local Area Network through the use of ethernet cables or through wireless connections.
● File storing to the device ‘s local SD card, USB drive or SMB share of the local network
● Sharing of videos and pictures on social platforms (Facebook, Instagram), storing of files in cloud services such as Google drive and Dropbox.
● By default, media files are accessible in the local network as SMB shares.
● Media files can also be managed with the help of FTP server which is already installed in PIM, or SFTP server — for more ways to manage and access files on your Raspberry Pi, check out our guide on How to Set Up a Raspberry Pi Web Server

Installation

1.Install Python dependencies
sudo apt install python3-dev libcurl4-openssl-dev libssl-dev
2.Install PIP Python Manager
sudo apt install python3-pip
3.Download MotionEYE
sudo pip3 install 'https://github.com/motioneye-project/motioneye/archive/dev.tar.gz' --break-system-packages
4.Init installation script
sudo motioneye_init
5.Install libcamera package
sudo apt install libcamera-v4l2
6.Open a web browser on any device in the network and Access
 http://raspberry_pi_IP:8765 Login: admin Password: (empty)

Login screen of the motionEye web interface on Raspberry Pi, prompting for username and password

Configuration

1.Add a Camera

Add Camera dialog in motionEye interface showing Local V4L2 Camera and USB webcam selection

For your Raspberry Pi camera module setup, select "Local V4L2 Camera" if you are using the Raspberry Pi Camera Module or a USB webcam.Configure the camera settings as needed.

MotionEye interface showing live video feed from multiple security cameras and configuration settings for a network camera on Raspberry Pi

2.Set Up Motion Detection
Go to the Motion Detection tab:

Motion detection settings panel in motionEye interface showing threshold sliders, motion gap, and frame capture configuration

Enable motion detection alerts and configure the sensitivity, detection zones, and other preferences.

3.Configure Storage
Go to the File Storage tab.

File storage settings in motionEye showing custom storage path, disk usage, and media upload options for security camera footage

Set up the directory where we want to store recorded videos and images.
We can also configure cloud storage options like Google Drive or Dropbox, enabling remote access and automatic

Cloud upload settings in motionEye for Google Drive, including media file options, authorization key, and test service button

4.Secure MotionEye
The default port is 8765 used by MotionEye for its web interface. If this port is accessible from the internet without proper security measures (like HTTPS and strong passwords), our camera system could be vulnerable.
● For secure remote access via VPN, you can access the Raspberry Pi without exposing ports directly to the internet.This is one of the most secure methods for remote access.
● Change the admin password by going to General Settings > User Accounts.

General settings in motionEye showing language selection, admin username, and password field configuration

● Consider changing the default ports used by MotionEye to non-standard ones, making it harder for potential attackers to find our service.

Alternative Installation - MotionEyesOS

MotionEyeOS is a Linux distribution that allows one to convert the single board computer into a surveillance system.
1.Download and install Raspberry Pi Imager to a computer with an SD card reader. Put the SD card that you will use with your Raspberry Pi into the reader and launch Raspberry Pi Imager.
2.You can easily get started by downloading the latest stable version of MotionEyeOS, the recommended operating system for Raspberry Pi-based surveillance systems https://github.com/motioneye-project/motioneyeos/releases
3.Select the Raspberry Pi Device.
4.Choose a Use Custom image and flash your Raspberry Pi.

Raspberry Pi Imager interface with Raspberry Pi 4 selected and MotionEyeOS image chosen for installation

5.Put the SD card in your Raspberry Pi and power it on to start building your DIY home surveillance system.Continue with previous Configuration

Additional Configurations

Set up notifications via email or webhooks. You can set up motion detection alerts to receive notifications

Motion notification settings in motionEye showing email alerts, SMTP configuration, webhook, and command execution options when motion is detected

Troubleshooting

Cannot Access the MotionEye Web Interface
We can't reach the MotionEye web interface at http://raspberry_pi_IP:8765.
1.Run the following command to see if the service is running:
sudo systemctl status motioneye
If it's not running, try restarting it:
sudo systemctl restart motioneye
2.If you have a firewall enabled, ensure that port 8765 is open:
sudo ufw allow 8765/tcp

Camera Not Detected or No Video Feed
If you're facing issues, double-check your Raspberry Pi camera module setup to ensure the camera or USB webcam is correctly connected.For the Raspberry Pi Camera Module, double-check the ribbon cable is seated correctly in the CSI slot.
1.Test the Camera:
Use the raspistill command to take a test photo:
raspistill -o test.jpg
If this doesn’t work, the camera may not be properly connected or could be faulty.
2.Verify Camera Interface is Enabled:
Ensure the camera interface is enabled in raspi-config:
sudo raspi-config
Go to Interfacing Options > Camera and make sure it's enabled.


Low Performance or High CPU Usage
Reducing the camera resolution or frame rate can significantly reduce CPU usage. Adjust these settings in the MotionEye interface under Video Device — refer to the official MotionEye documentation for more details on video configuration.

Storage Issues (e.g., Not Enough Space or Cannot Save Videos)
Configure MotionEye to automatically delete old files after a certain period or when storage space is low.
Go to Videos Section and set the Movies Lifetime, example: 15 days

Movie recording settings in motionEye showing file naming format, video format, quality slider, recording mode, and video retention time

Official MotionEye Documentation: https://github.com/motioneye-project/motioneye

Raspberry Pi Update: Essential Steps for a Secure and Optimized System Back to News How to Set Up and Optimize a Raspberry Pi Minecraft Server: A Comprehensive Guide