Building a Cell Phone Accessible Home Security System Using Raspberry Pi Zero 2 and Zero Cam : jamie_maguire
by: jamie_maguire
blow post content copied from Jamie Maguire
click here to view original post
I was looking to create a simple home security system. The main requirements were
- being able to access a camera remotely
- available 24×7
- low cost
- no subscriptions
- available via cell phone
After a look around, the Raspberry Pi Zero 2 and Zero Cam looked suitable.
In this blog post, I document the software, hardware, pain points, and resolutions to build a camera stream that you can access remotely 24×7.
~
Hardware
To build the surveillance solution, you need the following hardware:
- 1 x Raspberry Pi Zero 2 W – (£17)
- 1 x Zero Cam – Camera for Raspberry Pi Zero – (£15.00)
- 1 x Pibow Case for Raspberry Pi Zero 2W – (£6.00)
- 1 x Official Raspberry Pi Zero UK Power Supply – (£7.60)
- 1 x Raspberry Pi OS Pre-installed Micro SD Card 32GB – (£9.90)
Total cost for this solution: £55.50.
~
Software
The following software is required:
- Raspberry Pi OS
- Rpicam
- VNC Viewer
The rpicam library may come installed with the OS.
~
Assembling and Testing the Camera
Assembling the camera module 2 with the Raspberry Pi Zero 2 is straightforward. A simple cable connection, not fiddly, and takes a few seconds.
You can interact with the camera module 2 by issuing commands from the command line.
The following command can be used to activate video capture: rpicam-vid -t 0
The -t 0
parameter runs the video capture continuously. It does not write a file to the file system.
This means you can use this command to make the Raspberry Pi Zero 2 behave like a live streaming camera.
~
Random Flashing Black Frame
When running the Pi in headless mode, you might get a random flashing black frame in a continually running video when viewing over VNC.
This can be cause by a few different things. The main culprit is the lack of a physical display.
I noticed that by connecting the Pi to the monitor, the random flashing black frame stopped, and video runs smoothly.
In the past, I had been able to adjust another Pi configuration to trick the OS into thinking a screen was connected (see image above)
Despite a few different attempts with a new Pi, I couldn’t get this to work.
The quickest solution is to add a dummy HDMI display or ghost display.
You can get these for less than £5. They connect to the USB port on the Pi. The device tricks the Pi into thinking it’s connected to a screen. See below.
Another thing to adjust before proceeding is changing the size of page/swap file. By default, the Pi Zero 2 is set to 200MB. It’s not enough.
I set mine to 1024 (1GB).
You can see how to do this here.
~
Creating a Script to Start the Camera
We need to write script that well let us start the camera. The script must take resource management enter consideration on defining appropriate parameters for video capture activation. Logging these events to the file system will be helpful too.
The script below handles this:
#!/bin/bash # Script to stop and restart rpicam-vid # Stop rpicam-vid if it is running pkill -f "rpicam-vid" # Wait a few seconds to ensure it's stopped sleep 2 # Start rpicam-vid with infinite time (-t 0) rpicam-vid -t 0 --width 640 --height 480 --framerate 10 --preview 50,50,640,480 # Log the restart echo "$(date): Restarted rpicam-vid" >> /home/admin/restart_log.txt
This script will fire each time the Raspberry Pi operating system loads.
In the event of a system failure this script will also run.
The Raspberry Pi Zero 2 has limited resources, because of this we must kill any rogue rpiccam-vid that processes that may be running each time the script is run.
During testing and I found multiple instances of rpicam-vid were being spawned and running in the background.
The following command kills all these processes: pkill -f "rpicam-vid"
We give the OS 2 seconds to close these processes gracefully:
sleep 2
Finally, we start rpicam-vid:
# Start rpicam-vid with infinite time (-t 0) rpicam-vid -t 0 --width 640 --height 480 --framerate 10 --preview 50,50,640,480
Again, due to limited system resources in the Raspberry Pi Zero 2, I found I had to experiment with various parameter to keep it running.
In the above command, I set a smaller width and framerate.
The preview window (which displays the live video) also had to be reduced in size. I found the OS would becoming unresponsive after 20 or so hours.
This is where logging and real-time performance monitoring helped.
~
Adding the Script to the Startup
I wanted to automatically launch the rpicam-vid
command on boot so the camera starts running without manual intervention.
Initially, I tried different methods like cron jobs and autostart scripts, but these led to various issues.
I tried adding a script to crontab. The script would run, but no camera window appeared. It could not access the GUI.
I tried LXDE and using an autostart command (/.config/lxsession/LXDE-pi/autostart
).
Either the script blocked the GUI from loading or resulted in a black screen.
If launched inside a terminal (lxterminal -e
), the camera started but prevented the desktop from loading behind it.
A real pain.
The solution: sudo nano /etc/systemd/system/start_camera.service
Add the following :
[Unit] Description=Start Camera on Boot After=graphical.target [Service] ExecStart=/home/admin/restart_rpicam.sh Environment=DISPLAY=:0 User=admin Restart=on-failure RestartSec=10 StartLimitBurst=5 StartLimitInterval=600 [Install] WantedBy=graphical.target
Explainer:
- service starts after the GUI has fully loaded (target)
- runs the script with the correct user permissions and display settings
- automatically restarts if the script crashes
Putting the script in startup service made the script more stable.
~
SSH Issues and Wi-Fi Power Saving Problem
After a few hours of running continually, I found the Pi would be inaccessible, I read that Wi-Fi power saving option can be checked. It turns out this is enabled by default in the distro my Pi Zero 2 was running.
You can check the status of this setting by running the command: iwconfig
Instructions on how to disable this setting can be found here.
This resolved connectivity issues for a while, but the system would still occasionally glitch out.
~
Random System Freezes, Monitoring RAM and CPU Usage
To keep an eye on how long the system would run, I ran htop
from the command prompt. This shows all current processes, resource usage etc.
I noticed the rpicam-vid
command was using most of the resources and over time, the system would gradually become unresponsive.
The Pi Zero 2 only has 1GB of RAM and struggles with running a continuous camera capture.
I experimented with several of the parameters to rectify this.
I reduced the size of the window, framerate per second (10), and reduced the size of the preview window.
A lot of trial and error here but after a few days of monitoring and adjusting, I found a reasonable configuration. You can see those in the above screenshot.
In the above screenshot, you can see the Pi had been running for 22 continual hours.
This was good enough for me.
~
Using Windows Terminal to Monitor Multiple Cameras
The earlier steps can be repeated for adding multiple cameras. To monitor resources across each in a single console, Windows Terminal can be used.
In this screenshot, I have 3 Raspberry Pi’s setup (two Pi Zero 2’s and Pi 4).
~
Accessing Cameras Using VNC Viewer from Cell Phone
Using htop to monitor resource usage and uptime is helpful. The final part of the puzzle is to access the camera remotely. VNC viewer is perfect for this.
You can set this up for free on your smartphone and laptop.
Having access via the cell phone is useful for quick checks when out and about. A web accessible server is also helpful.
~
Cost Savings
Consumer services such as Ring offer easier ways to do this, but you’re locked into a perpetual subscription and each pricing tier has its own restrictions.
For example, the Basic subscription covers only 1 camera with 180 days of video.
Your data is not stored on your own machine.
This custom solution is a one-time cost for the hardware of £55.
The code can be adjusted to capture images or video files can be recorded when motion is detected. I can write Python code to send me notifications if I wanted.
You can add as many devices as required.
More on motion detection and sending cell phone notifications in a future blog post.
~
Summary
Enjoy what you’ve read, have questions about this content, or would like to see another topic? Drop me a note below.
You can schedule a call using my Calendly link to discuss consulting and development services.
February 15, 2025 at 11:30AM
Click here for more details...
=============================
The original post is available in Jamie Maguire by jamie_maguire
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================

Post a Comment