In this tutorial, you will learn how to make a Raspberry Pi Security Camera. You will learn how to install and use the Raspberry Pi camera module to create a webcam server. We will also discuss the motion software.
By the end of the tutorial, you should have a working live stream of your Raspberry Pi camera accessible to all your devices in your LAN (Local Area Network) that will also give you a better understanding of how the Raspberry Pi camera works.
- Raspberry Pi
- Raspberry Pi Camera
- Basic knowledge in Raspberry Pi and Linux
If you’re rusty on Linux, read this article about its fundamental commands before you proceed.
Essentially, our project today is a webcam server. A webcam server contains a webpage that displays the output of an IP camera. An IP camera is a video camera that communicates via an Internet Protocol network. This means it receives and sends control and image data through a computer to a computer network, such as the Internet. The camera feed is accessed by its own or its host computer’s IP address. Since we are using the Raspberry Pi, we will use its address to access the live stream.
1. First, open the terminal. Press CTRL + ALT + T from the desktop or click the black screen icon on the menu bar.
2. Next, update your system by entering the following:
sudo apt-get update sudo apt-get upgrade
3. Then, install Motion. Motion is a highly configurable program that monitors video signals from many types of cameras. Motion supports a wide array of devices including the Raspberry Pi camera. The software lets you make media files, create a live stream, and even use motion detection for your projects.
To install motion, install first its dependencies. These are the software that motion needs to work properly. Enter the following on your terminal.
sudo apt install autoconf automake build-essential pkgconf libtool git libzip-dev libjpeg-dev gettext libmicrohttpd-dev libavformat-dev libavcodec-dev libavutil-dev libswscale-dev libavdevice-dev default-libmysqlclient-dev libpq-dev libsqlite3-dev libwebp-dev
4. Download the Motion deb file using the wget command. We will use the current 4.3.1 version of Motion in this tutorial. You can check the latest version in Motion’s releases page.
sudo wget https://github.com/Motion-Project/motion/releases/download/release-4.3.1/pi_buster_motion_4.3.1-1_armhf.deb
5. Finally, install the deb file.
sudo dpkg -i pi_buster_motion_4.3.1-1_armhf.deb
By now, you already have the Motion software in your Raspberry Pi. What’s left is to change its configuration so that it works as a proper security camera.
- Open the motion configuration file using the following command:
sudo nano /etc/motion/motion.conf
2. Search for the following lines and change it respecitively.
- daemon off -> daemon on
- stream_localhost on -> stream_localhost off
- picture_output on -> picture_output off
- movie_output on ->movie_output off
By turning on the daemon, you allow Motion to run as a background service. The other three lines are essential because it makes your live stream work seamlessly, with no delays and obstructive pink and green lines.
Other Common Settings
These are common settings you can change to your preference.
- stream_maxrate 100 – allows more latency to have a real-time feed.
- framerate 100 – sets how much frames the camera captures in a second.
- width 640 – changes the width of the image output.
- height 480 – changes the height of the image output.
3. Turning daemon on in the motion configuration file is just part of setting up daemon. Next, we need to edit the original motion file.
sudo nano /etc/default/motion
4. Initialize daemon by changing
start_motion_daemon=yes. Save the file then exit.
5. Finally, you can start motion using the following command:
sudo service motion start
On the other hand, if you need to stop motion, enter the following command:
sudo service motion stop
You can also restart motion if the video lags or if you changed a setting while the camera is active.
sudo service motion stop
Setting up the Raspberry Pi Camera
By now, if you’re using a USB camera, you should already be able to visit the webcam stream using the IP address of the Raspberry Pi and the port number 808, but the tutorial is for the Camera Module so we need a few more steps.
Installing the Camera Module
1. First, make sure the board has no power. Pull up on the edges of the camera port’s plastic clip.
2. Insert the ribbon cable to the port. The silver lines on the cable should face the silver lines on the port.
3. Gently push the clip back. The cable should be on tight fit with the port that it won’t slide out when jerked.
Setting up the Raspberry Pi
In order to make the camera work with the Raspberry Pi, we will need to configure the Pi’s settings first.
1. First, startup your Raspberry Pi.
2. Open the Raspberry Pi Configuration tool using either main menu or command line. In the main menu, you can find it under Preferences.
Otherwise, enter this command on the terminal.
3. Next, select Interfaces. The camera is disabled by default. Enable it then save the changes to raspiconfig.
4. Restart your Raspberry Pi.
5. Next, add the camera to the list of your Raspberry Pi’s modules. If your camera is not connected properly, this command will return an error.
sudo modprobe bcm2835-v4l2
Alternatively, you can manually add the camera by editing the modules file. Enter the following command:
sudo nano /etc/modules
6. Add the following line to the bottom of the text file. Save and Exit.
5. Finally, restart the Pi.
Accessing the Webcam Server
Congratulations! You’re already streaming. You can now access the webcam stream using the IP address of the Pi. Using any device connected to your LAN, open any web browser, and enter the following address:
YOUR PI's IP ADDRESS:8081
Port 8081 is the default for web camera servers. You can also change this by revisiting the motion configuration file in the second part of the procedure.
The stream also works on the phones on your network. Also, the port number is different because I got curious, so I changed it to see if it works. You can also change the label on the bottom right. Both are available in the motion configuration file.
Hope this tutorial spark some ideas on your side. As usual, keep rockin’ and see you next time!