Setting Up a CCTV Stream with Raspberry Pi Zero 2W, Pi Camera V3
- Solomon Ankomah
- How to
- 11 Mar, 2025
If you’re looking for a lightweight and cost-effective way to set up a home CCTV system, the Raspberry Pi Zero 2W combined with a Pi Camera V3 and MediaMTX is an excellent solution. This guide will walk you through the process of setting up a real-time video stream that can be accessed from any device on your home network.
Requirements
- Raspberry Pi Zero 2W
- Raspberry Pi Camera Module V3
- MicroSD card (8GB or larger, with Raspberry Pi OS installed)
- Internet connection (for initial setup)
Step 1: Install Required Software
-
Open a terminal on your Raspberry Pi and create a directory for MediaMTX:
mkdir mediamtx cd mediamtx -
Check your Raspberry Pi’s architecture:
uname -mThis should return something like
armv7l, which you’ll need for the next step. -
Visit MediaMTX Releases and copy the link to the
.tar.gzversion corresponding to your architecture. For example, forarmv7l, the latest version might be:wget https://github.com/bluenviron/mediamtx/releases/download/v1.11.3/mediamtx_v1.11.3_linux_armv7.tar.gz -
Extract the downloaded file:
tar -xvzf mediamtx_v1.11.3_linux_armv7.tar.gz
Step 2: Configure MediaMTX
- Open the MediaMTX configuration file:
sudo nano mediamtx.yml - Add the following configuration under
path:(ensure proper indentation):cam1: runOnInit: bash -c 'rpicam-vid -t 0 --camera 0 --nopreview --codec yuv420 --width 1280 --height 720 --inline --listen -o - | ffmpeg -f rawvideo -pix_fmt yuv420p -s:v 1280x720 -i /dev/stdin -c:v libx264 -preset ultrafast -tune zerolatency -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH' runOnInitRestart: yes - Save and exit (
CTRL + X, thenYandEnter).
Step 3: Start MediaMTX
Run MediaMTX with:
./mediamtx
Now, the stream should be accessible on any device within your network by navigating to:
http://RASPBERRY_PI_IP_HERE:8889/cam1http://raspberry.local:8889/cam1
Step 4: Enable Auto-Start on Boot
To ensure MediaMTX starts automatically when your Raspberry Pi boots, set up a systemd service.
- Create a new service file:
sudo nano /etc/systemd/system/mediamtx.service - Add the following configuration:
[Unit] Description=MediaMTX Stream Service After=network.target [Service] ExecStart=/home/pi/mediamtx/mediamtx WorkingDirectory=/home/pi/mediamtx Restart=always User=pi [Install] WantedBy=multi-user.target - Reload systemd to recognize the new service:
sudo systemctl daemon-reload - Enable the service to start at boot:
sudo systemctl enable mediamtx - Start the service manually (optional):
sudo systemctl start mediamtx - Check the status of the service:
sudo systemctl status mediamtx
Conclusion
With this setup, your Raspberry Pi Zero 2W now serves as a lightweight, network-accessible CCTV streaming device. You can access the stream from any device on your network, making it a convenient and affordable security solution. If you encounter issues, check the service logs using sudo systemctl status mediamtx or restart the service with sudo systemctl restart mediamtx.
Enjoy your Raspberry Pi-powered CCTV system!