Pages

Window Mount Raspberry Pi Zero Camera



When Pi Camera support was first added to the Raspberry Pi Zero, I had written a short article describing my first experience with the camera. This article is a rehash of that first experience, with some additional tweaks.

Target Audience

This article will appeal to different audiences:
  • Those interested in mounting their pi zero to a glass surface (without using tape!)
  • Those interested in streaming their Pi Camera, on any flavor of Raspberry Pi, using industry standard H264 over RTSP
  • Those looking to record an RTSP stream from a Pi Camera, using a third party Video Management System (VMS).

List of Materials

Here is what you need to get started:

Install Raspbian

Install Raspbian in the normal fashion. Here are the basic steps:
  1. From your pc, download the Lite version of Raspbian from the Raspbian Downloads page. Do not unzip the download.
  2. Write the compressed image to the SD card using a program like Etcher.

Configure Raspbian for Headless Operation

Remove, then reinsert, the sd card adapter in your pc. This should prompt the operating system to mount the sd card, which needs to happen before we can progress.

Your freshly written sd card contains two partitions. We are interested in the one labeled "boot".
Open a command prompt and navigate to the boot partition. How you do this will vary by operating system.

For example, on my Ubuntu desktop, I issue the following from the command line:
cd /media/{my user account}/boot
We need ssh enabled on first boot, so enter the following:
sudo touch ssh
We also want wifi enabled on first boot, so create a text file named wpa_supplicant.conf using your favorite text editor. Add the following content to wpa_supplicant.conf:
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=<Insert country code here>

network={
 ssid="<Name of your WiFi>"
 psk="<Password for your WiFi>"
}
Make sure to enter your country code and wifi network credentials correctly. Taken from the Raspberry Pi Documentation.

When finished, insert your sd card into the Pi Zero. You are good to go!

Assemble the Pi Zero

Use This Adapter


The dash cam mount I used included 10 different adapters. At the time, I wasn't sure how exactly this would go together, so I wanted options. The adapter found to work best is shown in the figure on the left. Using epoxy, attach the dash cam adapter to the Pi Zero case.

Carefully hold the dash cam mount in place while the epoxy dries. I had 30 minute epoxy on hand, so I had to wait a while. Tick, tock.



Close Up Showing Where to Attach



Insert the sd card into the Pi Zero. Now insert the Pi Zero and Pi camera into the case. Make sure to connect the camera ribbon cable with the correct orientation. See the Raspberry Pi Documentation for details. Plug in the Wifi adapter if you are using an older Pi Zero. 

Finally, supply power to the pi.


Configure Raspbian

At this point, your Pi Zero should be booted and on your wireless network. Ssh into it from your pc. You may want to set a unique password for the Pi user, or create your own account. See the Raspbian documentation or forums for additional details.

Add yourself to the video group:
sudo gpasswd -a {your account} video
We need to enable the pi camera:
sudo raspi-config
From the menu, select Interfacing Options -> P1 Camera. Select Yes to enable the camera interface, then reboot.

Upon reboot, verify the camera's video devices are present:
$ ls -lh /dev/video*
crw-rw---- 1 root video 81, 3 Apr 16 15:12 /dev/video0
crw-rw---- 1 root video 81, 0 Apr 16 15:12 /dev/video10
crw-rw---- 1 root video 81, 1 Apr 16 15:12 /dev/video11
crw-rw---- 1 root video 81, 2 Apr 16 15:12 /dev/video12
We don't want the swapfile on the sd card that ships with Raspbian:
sudo systemctl stop dphys-swapfile.service
sudo systemctl disable dphys-swapfile.service
sudo systemctl mask dphys-swapfile.service
sudo rm /var/swap
Zram uses 100MB of RAM as compressed swap space. The following is not needed either for this project. I am a creature of habit, however, and prefer to manage all my Pi's the same:
sudo apt-get install zram-tools
sudo sed -i 's\.*ALLOCATION=.*\ALLOCATION=100\g' /etc/default/zramswap
sudo systemctl enable zramswap
sudo systemctl restart zramswap
Speaking of habits, here is another one:
sudo apt-get install ntp
sudo timedatectl set-timezone America/Chicago
To avoid using any memory at all, one can use ntpdate, instead of ntp, to sync the clock on the Pi.

 

Install v4l2rtspserver

First, let's install some prerequisites:
sudo apt-get install \
liblivemedia-dev liblog4cpp5-dev libasound2-dev git gdebi-core cmake build-essential v4l-utils

Now enter the following:
git clone https://github.com/mpromonet/v4l2rtspserver
cd v4l2rtspserver
cmake .
make
cpack .

You should now have a package with a .deb extension in the current folder. The precise name of the file will vary. Mine was called v4l2rtspserver-0.2.0-6-g3542b6f-Linux-armv6l.deb.

Install the package to your system:
sudo gdebi v4l2rtspserver-0.2.0-6-g3542b6f-Linux-armv6l.deb
The package you just installed ships with its own systemd service file, but we need to create our own. Using your favorite text editor create /etc/systemd/system/v4l2rtspserver.service and add the following content to it:
# 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 rotate=90
ExecStartPre=/usr/bin/v4l2-ctl --set-ctrl h264_i_frame_period=5
ExecStart=/usr/local/bin/v4l2rtspserver -F 5 -W 1920 -H 1080
Type=simple
User=root
Group=video
Restart=always
 
[Install]
WantedBy=multi-user.target
This configuration rotates the image 90 degrees and sets the 1080p frame rate to 5fps. Adjust this to suite your own requirements. Note that this example does not use any kind of authentication. Anyone will be able to view this stream without a password. Fortunately, authentication is supported. See the v4l2rtspserver documentation for details.

Enable and start the service:
sudo systemctl enable --now v4l2rtspserver.service
Verify the service is running correctly:
sudo systemctl status v4l2rtspserver

Testing the camera

The camera stream can be viewed from a pc with the following url:
rtsp://{pi zero ip}:8554/unicast
Use VLC Player to verify you can view this network stream. Do this now.

Adjust the Focus

Near Objects Out of Focus


Out of the box, the Pi Camera is focused for far objects. As you can see from the photo on the left, near objects are out of focus.




Use Tweezers to Adjust the Focus


Fortunately, the lens can be adjusted. To do that, carefully grab the lens with a pair of tweezers, and twist firmly, counterclockwise. I turned the lens about 3/4 of a turn. This may be more difficult than it sounds. If small blobs of glue are present around the edge of the lens, scrape those off to allow the lens to be rotated more easily.



Near Objects In Focus



Near objects are now in focus following the lens adjustment. Notice far objects are now out of focus.



 

Add Pi Camera to VMS

If the VMS in question requires a driver, select a generic driver that allows one to directly enter the RTSP stream url. You won't get any fancy features common with today's modern surveillance cameras, like edge based analytics or motion detection, but you will be able to see and record the stream from your favorite VMS software.

The instructions that follow are for ZoneMinder.

General Tab of Monitor Config








Begin by creating a new monitor from the ZoneMinder web console. Give the new monitor a human friendly name, select a source type of ffmpeg, and set the function to modect or monitor. Do not modify the other  fields. When finished, select the Source tab.








Source Tab of Monitor Config





From the Source tab, enter the RTSP url of the Pi Camera into the Source Path field. If you recall, the RTSP url has the format rtsp://{pi zero ip}:8554/unicast. Set the method to TCP, the colorspace to 32 bit, and the resolution to 1080p. When finished, select the storage tab.









Storage Tab of Monitor Config



For the best performance, disable the Jpeg engine and enable H264 Camera Passthrough. When finished click the Save button.

There are other considerations outside the scope of this article such as: setting the timestamp overlay, configuring buffers, and tuning motion detection. For additional details refer to the ZoneMinder documentation.