* Notice: I make a small commission on any amazon purchase recommendations given, however, I only recommend products that I have used and believe to be of good quality. I do not recommend any products that I have not used myself. Thanks! *

Overview

Security cameras are a great way to keep an eye on your home or business, but they can be expensive. If you’re looking for a cheaper alternative, you can turn your Raspberry Pi into a surveillance system. This guide will walk you through the process of creating your own security camera using a Raspberry Pi and a camera module. We’ll also cover how to set up a motion detection system that will send you an email or text message whenever the camera detects motion.

What You’ll Need

1. A Raspberry Pi running Raspbian or a similar debian-based Linux distribution.

The Raspberry Pi Zero W is an ideal choice for a security camera, as it’s small and compact, making it easy to mount in a variety of locations. It also has built-in Wi-Fi capabilities, so you can easily access the camera feed remotely. However, any Raspberry Pi model will work, as long as it’s running a debian-based Linux distribution.

This is the one I used:

Raspberry Pi

Raspberry Pi Zero W (Wireless) ( 2017 model) Amazon Link

2. A Camera Module

The Raspberry Pi Camera Module is a great choice for a security camera, as it’s small and compact, making it easy to mount in a variety of locations. It also has built-in Wi-Fi capabilities, so you can easily access the camera feed remotely. However, any camera module should work. A USB webcam will also work, but it will require a bit more setup.

Here is the official Raspberry Pi Camera Module:

Raspberry Pi Camera Module V2-8 Megapixel,1080p (RPI-CAM-V2)

Raspberry Pi Camera Module V2-8 Megapixel,1080p (RPI-CAM-V2) Amazon Link


Here are some other options:

  • Arducam 5MP OV5647 NoIR Camera

    • This camera module offers high-resolution video and still image capture, as well as infrared (IR) capabilities for night vision.
  • Adafruit PiTFT Mini Kit

    • This is a small, portable camera module that includes a touch screen display for easy viewing and control.
  • Pixy2 CMUcam5 Image Sensor

    • This camera module is designed for use with robotic projects, and it offers advanced image recognition capabilities.

3. A MicroSD Card

You’ll need a MicroSD card to install the operating system on your Raspberry Pi. The card should be at least 8GB (You’ll probably want more if you want to locally record video) in size, and it should be formatted as FAT32.

I got one with more storage than I needed at the time since I was planning on using it for other projects as well. But a 16GB card should be enough for this project.

SanDisk Ultra 16GB microSDHC UHS-I Card

SanDisk Ultra 16GB microSDHC UHS-I Card with Adapter Amazon Link

4. A Power Supply

You’ll need a power supply to power your Raspberry Pi. The power supply should be rated at 5V and at least 2.5A. You can use a USB power supply, but it’s recommended that you use a dedicated power supply for the Raspberry Pi.

This is the official Raspberry Pi power supply, but any 5V 2.5A power supply should work.

Raspberry Pi 5V 3A Power Supply

Raspberry Pi 5V 3A Power Supply Amazon Link

You could also use a battery pack, solar panel, or even a PoE (Power over Ethernet) adapter if you want to get really fancy.

5. A Case

This isn’t necessary, but it’s a good idea to protect your Raspberry Pi from the elements. You can use a case that’s designed for the Raspberry Pi, or you can use a generic case that’s designed for a similar-sized device.

I personally really like the look of this off-brand case (plus it has some nice passive cooling capabilities), but you can use whatever you want.

Raspberry Pi 4 Case

Flirc Raspberry Pi Zero Case Amazon Link

Setting Up Your Raspberry Pi

(I will be demonstrating the CLI the commands based on both Raspian and Ubuntu)

The first step is to install the operating system on your Raspberry Pi.

You can use any debian-based Linux distribution, but I recommend using Raspbian Server, as it’s the most popular and it’s designed specifically for the Raspberry Pi (Without Desktop). Raspbian

Once you’ve downloaded the image, you can use a program like Etcher to write it to your MicroSD card.

Once you’ve installed the operating system, go ahead and boot up your Raspberry Pi. You’ll need to connect it to a monitor, keyboard, and mouse to complete the setup process.

Connect to the internet and update your Raspberry Pi (Using CLI).

  1. Open the terminal and type the following command to update and upgrade your PI:

     sudo apt update && sudo apt upgrade -y
    
  2. Open the terminal and type the following command to open the network configuration file:

    sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
    
  3. Add the following lines to the file, replacing “SSID” and “password” with your network’s name and password:

    network={
        ssid="SSID"
        psk="password"
    }
    
  4. Save the file and exit the editor and restart the networking service with the following command:

    sudo systemctl restart networking
    

Camera Module Setup

  1. Connect the camera module to the Raspberry Pi’s camera port. The camera port is located near the HDMI port on the Raspberry Pi.

  2. Open a terminal window on the Raspberry Pi and enter the following command to enable the camera module:

     sudo raspi-config
    
    1. In the raspi-config menu, navigate to “Interfacing Options” and select “Camera”.

    2. Choose “Yes” to enable the camera module.

    3. Exit the raspi-config menu and reboot the Raspberry Pi.

  3. To test the camera module, enter the following command in the terminal window:

    raspistill -o image.jpg
    
  4. Alternatively, you can use the following command to preview the video stream from the camera module:

    raspivid -t 0
    

This command will take a picture with the camera module and save it as “image.jpg”.

Setting up a local Web Server

We need to set up a local web server to stream the video from the camera module to the web browser. I decided to use Nginx as my web server because it’s lightweight and easy to configure. Apache2 is another popular option.

  1. Install Nginx with the following command:

    sudo apt install nginx -y
    
  2. Next, use the raspivid command to capture video from the camera module and stream it to the nginx server. The following command will capture video at a resolution of 640x480, with a frame rate of 30 frames per second, and will stream it to the nginx server on port 8080:

    raspivid -w 640 -h 480 -fps 30 -o - | nc localhost 8080
    
  3. To access the video stream from a web browser, you will need to configure nginx to serve the stream. To do this, add the following lines to the nginx configuration file (usually located at /etc/nginx/nginx.conf):

    sudo nano /etc/nginx/nginx.conf
    
    server {
       listen 8080;
       server_name localhost;
    
       location /video_stream {
          rtmp_server localhost;
          rtmp_app live;
          rtmp_playpath video;
          root /var/www/html;
       }
    }
    
  4. Restart Nginx with the following command:

    sudo systemctl restart nginx
    
  5. You can now access the video stream from a web browser by visiting the Raspberry Pi’s IP address and adding “/video_stream” to the end of the URL (for example, http://192.168.1.100/video_stream).

If you are unsure of the IP address of your Raspberry Pi, you can use the following command to find it:

ifconfig

The video should be displayed in the web browser, and will be updated in real-time as new frames are captured by the camera module!

Optional Features

There are several optional features that you can add to this setup to enhance its functionality. For example, you can add security to the system by configuring nginx to require authentication before allowing users to access the video stream. This can be done by adding a username and password to the nginx configuration file, and then requiring users to enter this information before they can access the video stream.

Of course, you could also use a more advanced authentication method, such as a two-factor authentication system. But that would require a lot more work, and I don’t think it’s necessary for this guide.

You could also add additional cameras to the setup by using multiple camera modules and configuring nginx to serve multiple video streams. This allows you to view video feeds from multiple cameras simultaneously, giving you a more comprehensive view of your surroundings.

If you want to record the video stream continuously as well, raspivid can be configured to save the video to a file instead of streaming it to the nginx server. Read more about this feature in the raspivid documentation.

Conclusion

Overall, this guide provides a simple and straightforward way for users to set up and stream video from a Raspberry Pi zero w to a web browser. With a few basic steps and commands, users can quickly and easily view live video feeds from the Raspberry Pi on a web browser.

Thank you for reading this guide on how to stream video from a Raspberry Pi zero w to a web browser. We hope that this guide was helpful and provided you with the information and steps needed to set up and use this system.