An IP camera does not have to cost much. With a Raspberry Pi Zero W and camera module you can make a good and particularly compact camera relatively easily, and use it to set up an rtsp streaming server, for example. In this article, you'll learn how to do that, and then use the camera in Surveillance Station on a Synology or QNAP NAS.
You can of course buy a dirt cheap IP camera with all the trimmings in China, but they are usually poorly secured and difficult to set up. It is not immediately cheaper if you build it yourself, but you are more flexible and above all it is fun and educational. We use a Raspberry Pi Zero W (about 11 euros) along with the official 8megapixel camera module v2 (29.95 euros). It has a great Sony IMX219 sensor. A NoIR variant is also available that provides a better image at dusk. If you combine them with infrared LEDs, you also have good night images (in grayscale). If you want to save some money, you can find affordable alternatives in China from around 8 euros. You also need a micro SD card for the software and a power adapter with micro USB that can preferably deliver at least 1.2 amps.
01 Which software?
There are many methods for streaming from a Raspberry Pi Zero W. In addition to the camera module, most methods also support a USB camera or webcam that you connect to the Pi Zero W via a USB-on-the-go cable. Whichever method you choose: given the fairly limited processing power, it is wise to leave the motion detection to the video surveillance software such as Surveillance Station. A well-known option is motionEyeOS, which has a nice web interface for configuring the camera. A disadvantage is the relatively large delay, even with the option Fast Network Camera with which you also lose many other options. We therefore choose to set up a manual stream.
02 Raspbian installation
We start with the installation of Raspbian Buster Lite. Get the most recent version. After downloading, extract the archive and use balenaEtcher to put the img file on a micro-sd card. Before you start the Pi with the micro-sd card, add in the root (which also contains the file kernel.img state) a text file called wpa_supplicant.conf with the rules below. put it in the back ssid the name of your WiFi network and fill in psk the password. In the same folder, create an empty file called ssh so that you can log in via a shell after starting.
country=NL
update_config=1
ctrl_interface=/var/run/wpa_supplicant
network={
scan_ssid=1
ssid="yoursid"
psk="yourpassword"
}
After the Pi Zero W boots up, you can find out the IP address assigned by your router's DHCP server from the router's configuration pages (if all goes well). Log in to that ip address via ssh with a program like PuTTY and use the default username pi and the corresponding password raspberry. Then preferably change those login details.
03 Installing the rtsp server
Make sure the Pi is up to date with the commands:
sudo apt-get update
sudo apt-get dist-upgrade
Sudo raspi-config opens the Pi's configuration page. Activate via Interfacing Options the camera module. You activate the video-for-linux kernel driver with:
sudo modprobe bcm2835-v4l2
Check that you have a /dev/video0 have with:
sudo ls -la /dev/vid*
Install the git and cmake tools with:
sudo apt install git cmake
After that, you can compile and make available the rtsp server, which supports h.264, among other things, with the following commands:
git clone //github.com/mpromonet/v4l2rtspserver.git
cd v4l2rtspserver
cmake .
make
sudo make install
04 Starting the rtsp stream
For example, you start a stream from the folder v4l2rtspserver of:
./v4l2rtspserver -F 10 -W 1920 -H 1080
After starting the rtsp server you can read any error messages and a link to the stream in the form rtsp://ipaddress:8554/unicast. You can use this in, for example, VLC player to connect to the camera, but also in Surveillance Station as we will show further on. With the option -You user:password enter a user and password to protect the stream. In that case, the link to the rtsp stream takes the form rtsp://user:password@ipaddress:8554/unicast. It is neat that the processor load remains very limited, the gpu does most of the work.
05 Server Autostart
We want the rtsp server to start automatically after turning on the Pi. To do this, create a simple script with:
sudo nano /etc/systemd/system/v4l2rtspserver.service
This includes the following rules:
[Unit]
Description=v4l2rtspserver rtsp-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=/home/pi/v4l2rtspserver/ v4l2rtspserver -F 10 -W 1280 -H 720
ExecReload=/bin/kill -HUP $MAINPID
Type=simple
user=pi
Group=video
Restart=always
[Install]
WantedBy=multi-user.target
Check back ExecStart the location of v4l2rtspserver. There are some options that you can experiment with (such as vertical_flip for image rotation). Make the script active with:
sudo systemctl enable v4l2rtspserver
And start the script with:
sudo systemctl start v4l2rtspserver
Add camera image to Surveillance Station
You can easily use the camera image in Surveillance Station by manually specifying a camera. With this software, the delay of the image appears to be quite limited. While in VLC player that is about two seconds, the delay in Surveillance Station is only about one second. That seems like a lot, but many other methods of streaming from a Raspberry Pi often have an even greater delay of four seconds or more.