Wednesday, 11 March 2020

RaspberryPi Zero WH Setup with RTSP feed

Documenting the process of the installation/setup of a Raspberry Pi Zero WH and an RTSP feed from a Raspberry Pi Camera Module. The stream will be picked by the Synology Surveillance Station.

Components:


The first thing to decide upon was which Pi to use, the Pi Zero W ticks all the boxes. It is small, not overloaded with ports (just enough for this project) and there is a cool mounting case for it (see below)

The Pi Hut: Raspberry Pi Zero W

If using an ethernet hat (see below) on the header then this version will be required:
The Pi Hut: Raspberry Pi Zero WH
ZeroView is absolutely perfect for this, the Pi and camera are mounted to the case and then the case attaches to the window via suction cups (time will tell how long they will hold for)

The Pi Hut: ZeroView case
PoE Splitter: I have used one of these before (see this article on powering the Philips Hue Hub). Power to the Pi is not going to be a big issue but getting a CAT5 cable is going to be easier and I have some spare PoE outlets on the switch. The Pi itself does not take a cabled network connection (as it is WiFi only) but there are hats for the Pi Zero to enable this.

Amazon.co.uk: Active PoE Splitter Micro USB
I am not too sure how much this will push the CPU on the Pi Zero (it is a single core device) so to help dispense the heat from the CPU I am going to add a ceramic heatsink to the Pi Zero.
Raspberry Pi Camera Module, there are various versions on the market. I was contemplating using the v1.3 but ended up opting for v2.1 as it was available for same day delivery on Prime:

Amazon: Raspberry Pi v2.1 8 MP 1080p Camera Module
The Pi Zeros camera port is smaller than the other Pi variants. Not all cameras come with the correct cable, so to play it safe I ordered one of these as well.

The Pi Hut: Pi Zero Camera Adapter





As previously mentioned under the PoE adapter, there is an option to expand the Pi Zero to include an Ethernet adapter. This one from eBay sits on top of the Pi, there are Micro USB adapter versions as well.

If using this option then the Raspberry Pi Zero WH is the Pi to go for, buying the Raspberry Pi Zero W means buying the header and soldering it to the board. The WH is just easier and actually works out cheaper than buying the header pins separately.

(I am still awaiting on the arrival of the Ethernet adapter, this posting will be updated later with more details).


Headless setup:

Using etcher, burn the raspberry pi image to a micro sd card, I am using an 8GB Sandisk. There is now also an official Raspberry Pi Imager, available here.
Before demounting the micro sd card a couple of files need to be added to it.

Enabling SSH

place a blank file titled 'ssh' (all lower case, and no extension), on to the boot partition of the sd card.

Configure WiFi

paste the following in to text file:
country=GB
update_config=1
ctrl_interface=/var/run/wpa_supplicant

network={
 scan_ssid=1
 ssid="NetworkSSID"
 psk="Password"
}
other country codes here

save the file as wpa_supplicant.conf in to the boot partition of the sd card, ensure there is no .txt extension on the file

Power on the Pi

Connect the Pi to power and wait about 2 to 3 minutes for it to power up and configure itself.

Find & connect to the Pi

Using AngryIP Scanner, scan the network for the Pi.




Once the IP address has been discovered, connect to it.

Using terminal/PuTTY
ssh pi@ipaddress

Default password is raspberry (will address this security issue later on)

Update the Pi

At the command line run
sudo apt-get update && sudo apt-get upgrade -y && sudo reboot
get the Pi software up to date. This can and will probably take some time, the Pi will reboot at the end of the process.

Auto update Pi

Once the Pi has updated and reboot, re-connect to the Pi using ssh again.
sudo apt-get install unattended-upgrades

Extra Configuration options

run:
sudo raspi-config


This will bring up the raspberry pi software configuration tool, make changes to the following:

2 Network Options > N1 Hostname

Change the name of the device to something more descriptive, in this case 'livingroompi'
3 Boot Options > B1 Desktop / CLI > B1 Console

Change the boot up so that it boots to console and not to GUI (do not require GUI as this will be running headless)
3 Boot Options > B2 Wait for network at boot > Yes
5 Interfacing Options > P1 Camera > Enable
7 Advanced Options > A3 Memory Split > Change to 256MB

7 Advanced Options > A1 Expand Filesystem

Then exit the configuration tool, it should prompt you to reboot the Pi select yes. If it does not then type:

sudo reboot

New User Creation

Time to get rid of the bigger security flaw, the standard 'pi' user account. Log in (for the last time as the pi user)

sudo adduser joebloggs

add user to sudo and video group

sudo adduser joebloggs sudo
sudo usermod -a -G video joebloggs

'logout' from as the pi user and login as the new user you have just created, then run the following (this will delete the default pi account, security issue resolved):
sudo deluser -remove-home pi

 Real Time Streaming Protocol (RTSP) 

Time to get a stream from the webcam so that it can be used on the network (I am using this for the Synology Surveillance Station).

sudo -i

apt update && apt install git cmake

git clone https://github.com/mpromonet/v4l2rtspserver.git

cd v4l2rtspserver && cmake . && make && make install

add the following command to your /etc/rc.local (sudo nano /etc/rc.local)
v4l2rtspserver /dev/video0 -W 1280 -H 720 -F 20 &
(other switches are available here: https://github.com/mpromonet/v4l2rtspserver)

sudo reboot

using VLC player on a computer (File > Open Network)

rtsp://{IPAddressOfYourPI}:8554/unicast
You should now have a working real time stream

Running top from within the ssh connection shows the process is not too tasking on the Raspberry Pi Zero's single core CPU:


Additional:

Set a DHCP reservation on the router for the Pi, so it always retains the same IP address on Pi/Network reboot.

As good as the camera module is during the day, it is terrible at night time (even with a security light directly outside). An IR camera would be better.

No comments:

Post a Comment