Pages

Raspberry Pi Zero Camera





While I personally would not bother to run ZoneMinder directly on a Raspberry Pi Zero, or any of the older armv6 Raspberry Pi's, the Pi Zero does make a great camera, which can stream RTSP to another device running ZoneMinder. With the addition of a camera port on the newest Pi Zero, we can do just that with just a small load on the cpu, even at 1080p resolutions.

List of Materials

A $5 computer this is not. At the time I purchased my Pi Zero, they were hard to come by. Adafruit only had the more expensive Pi Zero camera pack. I paid out the nose for the kit, but it was still a fun project.

V4L2 Driver

The way to stream video from the picam, with little cpu overhead, is to use the bcm2835-v4l2 video-for-linux-2 (v4l2) driver. This driver is part of raspbian, but if you've chosen a different distro then, like me, you have some work to do. I installed Pignus on my Pi Zero, which is a Fedora 23 spin.

Unfortunately, Pignus did not appear to have this driver so I did the following to get a Raspberry Pi kernel with the modules I needed. If you have attached your pi camera and the device /dev/video0 exists, then skip the following steps.

sudo dnf install git
git clone https://github.com/Hexxeh/rpi-update
cd rpi-updates
sudo ./rpi-update
echo "bcm2835-v4l2" > /etc/modules-load.d/picamera.conf

Now reboot. You should now have a /dev/video0 device.

V4L2RTSPServer

This piece of software does exactly what the name implies. It turns your device into an RTSP server, and it is compatible with the raspberry pi camera!
git clone https://github.com/mpromonet/v4l2rtspserver
cd v4l2rtspserver
cmake .
make
sudo make install
If you are running a Debian based distro, such as Raspian, the above steps will just work. If you are running an RPM based distro, like I was, you will have to manually install the software's dependencies first.  I've gone ahead and packaged it into an rpm, which will do all the work for you, including the dependencies. However, I am not quite done preparing a repo for the rpm and its dependencies. PM me if you want the v4l2rtspserver rpm and I'll finish up the repo.

Followup Configuration

In order to get v4l2rtspserver to start automatically after boot, I created a systemd service file. Create the file /etc/systemd/system/v4l2rtspserver.service and add the following contents:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# systemd configuration for v4l2rtspserver
# /etc/systemd/system/v4l2rtspserver.service
 
[Unit]
Description=v4l2rtspserver rtsp streaming server
After=network.target
 
[Service]
#ExecStartPre=/usr/bin/v4l2-ctl --set-ctrl vertical_flip=1
ExecStartPre=/usr/bin/v4l2-ctl --set-ctrl h264_i_frame_period=5
ExecStart=/usr/bin/v4l2rtspserver -F 5 -W 1920 -H 1080
#ExecReload=/bin/kill -HUP $MAINPID
Type=simple
User=root
Group=video
Restart=always
 
[Install]
WantedBy=multi-user.target

Adjust the file paths accordingly. Notice that you can use the v4l2-ctl tool to adjust certain parameters. For example, if you have mounted the camera such that the image is upside down, then you can set the vertical_flip flag to right the image. Since I am running at 5 frames per second, I chose to set the i-frame to 5 frames, so I didn't have to wait up to 30 seconds to get a picture.

Now start the service;
sudo systemctl start v4l2rtspserver
You should now be able to view the camera stream, using ZoneMinder or any media player, using the following url:
rtsp://{ipaddress}:8554/unicast

RPOS - An ONVIF Server

For bonus points, if you want to take this a step further, checkout the rpos project on github:
git clone https://github.com/BreeeZe/rpos
I have not had a moment to try this yet, but in theory it should allow the ZoneMinder ONVIF probe to find the your Pi Camera automatically.

Camera Performance

CPU usage stays under 10% when streaming 1080p video @5fps! The GPU is doing all the work.

One thing I will note is that the standard color camera I used in this project would not make a good security camera. It only works well in well lit areas. Once the light level drops, this camera quickly looses sight of everything in the field of view. You should look at one of the other pi cameras if you plan to use this as a security camera. One with IR capability would be ideal.