Reply
Highlighted
Senior Member
Posts: 3,797
Registered: ‎09-12-2010
Kudos: 1326
Solutions: 31

[Tutorial] Raspberry Pi 4-Cam Matrix Viewer Appliance

Hi everyone,

 

Here is a quick, newbie friendly tutorial on how to set up a Pi so that when you power it on, it automatically loads up a 4-camera matrix of RTSP streams on a 1080p screen.

 

I myself am pretty green with Linux.  I know there are always better ways to do things, so I'm hoping you all can help improve the tutorial.  My goal is to make it easy and clear enough for someone who's never used a Pi or Linux before.

 

This script is currently limited to running a fixed 4-cam matrix, but I'd like to set it up in the future for a 1-cam full screen, 9 cams (?), switching/cycling, auto-resolution detection, etc...

 

As usual, anyone's free to use it as long you remember to share back any improvements you make!

 

 

 

What you'll need:

- A display with an HDMI input, preferably 1080p.

- A Raspberry Pi or Raspberry Pi 2 ($35)

- A 4+ GB SD (for Pi) or 4+ GB MicroSD (for Pi 2)

- A quality USB Power Adapter that is rated at least 1 Amp, best to get one that can do 2 or 2.5A.

- An HDMI cable to connect from the Pi to the display.

- A supported WiFi dongle or a network cable (wired is always better)

- A case for the Pi (optional)

- A USB keyboard (temporary, used during config)

- A computer with an SD card reader (temporary, used during config)

- A UniFi Video NVR Running the latest software, and at least 4 Cameras

 

If you don't already own a Raspberry Pi, the Pi and Pi 2 MSRP is the same. Because the Pi 2 is in higher demand, you might be charged a premium if you purchase it by itself. I highly recommend choosing the Pi 2 as it is about 6x as powerful as the Pi, and the price is the same. Canakit sells complete packages for the Pi 2 with everything you need (except display and keyboard). Search Amazon for “CanaKit Raspberry Pi 2 Complete Starter Kit with WiFi” for a good bundle.

 

1) Download the latest Raspbian image.

Go to https://www.raspberrypi.org/downloads/ and scroll down to download the Stand-alone Raspbian image. The image itself is close to 1GB and is available as a .torrent or direct download.

 

2) Put Together and Connect Your Pi

While you wait for the image to download, stick your Pi in it's case (if you bought one, or build one out of Lego!), plug the HDMI into the Pi and the TV, plug in a network cable to the Pi to your network, plug in the power adapter but don't plug it into the wall yet, and don't stick the memory card in just yet.

 

3) Write the Raspbian image to the SD Card

Using a computer with an SD Card reader, follow these guides on how to write the image to your SD card. If you purchased an SD card that has Noobs pre-loaded, these steps will overwrite Noobs, but you can always overwrite it in the future. This walkthrough can be done using Noobs, but I won't go into details on how to set it up.

Linux: https://www.raspberrypi.org/documentation/installation/installing-images/linux.md

Mac OS: https://www.raspberrypi.org/documentation/installation/installing-images/mac.md

Windows: https://www.raspberrypi.org/documentation/installation/installing-images/windows.md

 

4) Power Up Your Pi

Now that your card is ready, insert it in the Pi, turn on your display to the right input, and plug the power adapter into the wall. Wait a bit and the raspi-config menu should display on-screen.

 

5) Raspi-Config

The raspi-config application is used to configure the basic settings for your Pi and Raspbian.

 

Choose Option 1 to expand the filesystem so it uses all the free space on the card. Next, choose Option 2 to change the password for the default user “pi”. Note that when you type in the password, nothing will be displayed on screen. Be sure to write down this password and keep it somewhere safe, you will need it to log in after we exit the raspi-config menu. Finally, we want to allocate 128M of memory to the GPU, so choose Option 8 Advanced Options, then Memory Split and choose 128M. Don't make any other changes at this point.

 

Choose Finish from the main menu and let the Pi reboot.

 

Raspi-Config can be ran in the future by entering “sudo raspi-config” at the command line. This may be necessary if you wish to change your keyboard layout or locale settings. Overclocking is also possible, but not required or recommended for what we are doing. For more information on raspi-config, see https://www.raspberrypi.org/documentation/configuration/raspi-config.md

 

6) Time to Update the OS

Once your Pi has rebooted, you will be prompted with the command line and asked to log in. The default username is “pi”. Type “pi” and hit enter. It will then ask for your password. Enter the password you chose in the previous step. Note that once again nothing will be displayed on screen as you type it. Once you're logged in, you'll be at the command prompt.

 

To start, we're going to make sure all the software packages are up to date. Type the following into the command line and follow the prompts, entering your “pi” password when requested.

 

sudo apt-get update && sudo apt-get upgrade

 

7) Install Necessary Packages

We need a couple packages to be able to display the feeds on-screen. These packages will let us play the feed without having to load a full desktop environment, which would be a waste of resources. Run the following command in the command line and follow the prompts:

 

sudo apt-get install screen omxplayer

 

8) Configure the UniFi NVR Cameras for RTSP Streaming

Log into your UniFi NVR web interface as an Admin and decide what four cameras you want to display on-screen.

 

On the Cameras tab in UniFi Video, for each Camera, open the Camera details pane, and under Video, at the bottom, open the RTSP Service dropdown. On a 1080p display, each 1/4 is 960 x 540 pixels. Enable the RTSP Service that's most appropriate for your hardware and network conditions.

 

For 720p cameras (UVC, Dome, Micro), you can choose the High stream. For 1080p cameras (UVC-Pro) choose the Medium stream. If you are trying to use WiFi or experience studdering in general when we're done, you may need to change to a lower stream, or lower with the bitrate settings for that stream in your camera settings.

 

Copy down the RTSP URI for each of the cameras (and remember which camera they correspond to). You will need to enter this into a script in a future step. Here's an example of what an RTSP URI may look like:

 

rtsp://10.0.10.10:7447/364c6eda-1cb0-62d1-a76e-45eacc23bf4b_1

 

For your reference, the RTSP URI format is: rtsp://<NVR_IP>:7447/<CAMERA_ID>_<CHANNEL>

 

The <CHANNEL> can be 0, 1, or 2, so you can easily switch between channels (resolutions) by changing that number – but keep in mind you have to have that enabled in the camera settings in UniFi Video to make it work.

 

9) Download Player Shell Script

Back on the Pi command line, we need a shell script that will draw and start playing the four streams to the screen. You can download the script direct to your Pi from the internet by running the following command:

 

sudo wget -O /etc/init.d/play4ubntcams http://ticobytes.com/ubnt/play4ubntcams.txt

 

10) Setting Camera RTSP URIs

Now we need to edit the startup script we just downloaded to specify the RTSP URIs of the cameras we got in Step 8. To do so, we will use an editor called “nano”. Run the following command to open the file for editing:

 

sudo nano /etc/init.d/play4ubntcams

 

Once editing the file, use the arrow keys to go to the section that says: # Set the RTSP URIs here for each camera.  This is the part of the script that actually draws the stream to the screen.  Look for the part that says <RTSP_STREAM_URI_HERE> and replace it with the RTSP URI for each camera.

 

When done, hit CTRL+X to exit Nano, hitting Y when prompted if you want to save the file.

 

11) Configuring the Script to Auto Start

Lastly, we need to tell Raspbian to execute the script when it boots. To do so, run the following two commands in order:

 

sudo chmod 755 /etc/init.d/play4ubntcams

 

sudo update-rc.d play4ubntcams defaults

 

12) Reboot the Pi

Finally, let's reboot the Pi by running the following command:

 

sudo reboot

 

At this point, the Pi should reboot, and in theory, start back up with the camera matrix running!

 

 

 

 

Troubleshooting:

 

Display Issues:

If you're having strange display issues, such as banding, flickering, or the display just shuts off, it's somewhat expected for the Raspberry Pi.  To troubleshoot, you're going to have to make some changes to the /boot/config.txt file in order to force certain modes and resolutions.  I will update this further in the future, but at least for now, that should give you an idea on what to search for on The Google.

TicoBytes.com | www.ticobytes.com
Ubiquiti Solutions and English / Spanish IT Support in Costa Rica
UBRSS, UBWA, UEWA - Ubiquiti Certified Trainer
Senior Member
Posts: 3,797
Registered: ‎09-12-2010
Kudos: 1326
Solutions: 31

Re: [Tutorial] Raspberry Pi 4-Cam Matrix Viewer Appliance

[Reserved]

TicoBytes.com | www.ticobytes.com
Ubiquiti Solutions and English / Spanish IT Support in Costa Rica
UBRSS, UBWA, UEWA - Ubiquiti Certified Trainer
Senior Member
Posts: 3,797
Registered: ‎09-12-2010
Kudos: 1326
Solutions: 31

Re: [Tutorial] Raspberry Pi 4-Cam Matrix Viewer Appliance

[ Edited ]

[Reserved]

TicoBytes.com | www.ticobytes.com
Ubiquiti Solutions and English / Spanish IT Support in Costa Rica
UBRSS, UBWA, UEWA - Ubiquiti Certified Trainer
SuperUser
Posts: 3,626
Registered: ‎10-12-2010
Kudos: 2012
Solutions: 28

Re: [Tutorial] Raspberry Pi 4-Cam Matrix Viewer Appliance

Nice Work!!

 

UBRSS, UBWA, UEWA - Ubiquiti Certified Trainer
Member
Posts: 176
Registered: ‎03-03-2013
Kudos: 25
Solutions: 2

Re: [Tutorial] Raspberry Pi 4-Cam Matrix Viewer Appliance

Legendary !!!

Emerging Member
Posts: 53
Registered: ‎04-13-2015
Kudos: 31
Solutions: 1

Re: [Tutorial] Raspberry Pi 4-Cam Matrix Viewer Appliance

Wow! Fantastic write up. Will be right on it!

Senior Member
Posts: 3,797
Registered: ‎09-12-2010
Kudos: 1326
Solutions: 31

Re: [Tutorial] Raspberry Pi 4-Cam Matrix Viewer Appliance

Wait until you all test it and see if it works first lol Man Happy

TicoBytes.com | www.ticobytes.com
Ubiquiti Solutions and English / Spanish IT Support in Costa Rica
UBRSS, UBWA, UEWA - Ubiquiti Certified Trainer
New Member
Posts: 3
Registered: ‎05-04-2014

Re: [Tutorial] Raspberry Pi 4-Cam Matrix Viewer Appliance

I used your how to, it's well written btw, but I get vertical lines appearing randomly through the images, any tweaks that may improve the image quality, it's as if the Pi doesn't have enough resources for the continual refreshing?

Senior Member
Posts: 3,797
Registered: ‎09-12-2010
Kudos: 1326
Solutions: 31

Re: [Tutorial] Raspberry Pi 4-Cam Matrix Viewer Appliance


@pmacdona wrote:

I used your how to, it's well written btw, but I get vertical lines appearing randomly through the images, any tweaks that may improve the image quality, it's as if the Pi doesn't have enough resources for the continual refreshing?


The Pi has enough resources, if you log in remotely via SSH you'll see it uses around 25% CPU at most.

 

What kind of vertical lines are we talking about?  Can you share a picture?

 

It could be either that you need a better power supply, or you need to tweak the /boot/config.txt HDMI video output settings.

 

Can you try on another TV?

TicoBytes.com | www.ticobytes.com
Ubiquiti Solutions and English / Spanish IT Support in Costa Rica
UBRSS, UBWA, UEWA - Ubiquiti Certified Trainer
New Member
Posts: 3
Registered: ‎05-04-2014

Re: [Tutorial] Raspberry Pi 4-Cam Matrix Viewer Appliance

I'm not sure what the problem was, but I managed to overcome it by adding a 30 second delay until pulling up the first feed (Sleep 30), and then adding a 5 second delay in between each of the cameras, now trying to trouble shoot random feeds dropping off after a number of hours, how long have you managed to stream successfully for without any of the feeds dropping out?, also any idea how the script could be modified to swap feeds around, I know I can use the sleeep option to stagger full screen shot of each camera, but how do you make the script loop endlessly?

 

 

Senior Member
Posts: 3,797
Registered: ‎09-12-2010
Kudos: 1326
Solutions: 31

Re: [Tutorial] Raspberry Pi 4-Cam Matrix Viewer Appliance

I'm not much of a scripting guru, but if you want to kill all screens, just do "killall omxplayer.bin" and then you can run the same four screen commands again with new camera feeds.  Then make the whole script loop and add delays as appropriate.

TicoBytes.com | www.ticobytes.com
Ubiquiti Solutions and English / Spanish IT Support in Costa Rica
UBRSS, UBWA, UEWA - Ubiquiti Certified Trainer
Regular Member
Posts: 491
Registered: ‎12-01-2013
Kudos: 10
Solutions: 3

Re: [Tutorial] Raspberry Pi 4-Cam Matrix Viewer Appliance

Nice write up, only thing now is a sticky

Emerging Member
Posts: 59
Registered: ‎11-14-2013
Kudos: 18
Solutions: 1

Re: [Tutorial] Raspberry Pi 4-Cam Matrix Viewer Appliance

How does the Pi 2 + Raspbian handle loading the Web UI just in a browser?

New Member
Posts: 8
Registered: ‎02-05-2015
Kudos: 1

Re: [Tutorial] Raspberry Pi 4-Cam Matrix Viewer Appliance

Nice work Man Happy

but ...

Can i ask : " What delay you get ?"

Emerging Member
Posts: 53
Registered: ‎04-13-2015
Kudos: 31
Solutions: 1

Re: [Tutorial] Raspberry Pi 4-Cam Matrix Viewer Appliance

[ Edited ]

Hi,

 

Finally got time to try this out and having a bit of a problem...

 

When I reboot the Pi I see that the script has started but get nothing else. 

 

When I try to run a stream directly from the command line I get the attached. 

 

I assume its an issue/setting with omxplayer but thats where my abilities end...

 

Any suggestions gratefully received.

 

James

Capture.PNG
Senior Member
Posts: 3,797
Registered: ‎09-12-2010
Kudos: 1326
Solutions: 31

Re: [Tutorial] Raspberry Pi 4-Cam Matrix Viewer Appliance


@abbaZaba wrote:

How does the Pi 2 + Raspbian handle loading the Web UI just in a browser?


I'm pretty sure it won't run...

TicoBytes.com | www.ticobytes.com
Ubiquiti Solutions and English / Spanish IT Support in Costa Rica
UBRSS, UBWA, UEWA - Ubiquiti Certified Trainer
Senior Member
Posts: 3,797
Registered: ‎09-12-2010
Kudos: 1326
Solutions: 31

Re: [Tutorial] Raspberry Pi 4-Cam Matrix Viewer Appliance


@anarkikrzysiek wrote:

Nice work Man Happy

but ...

Can i ask : " What delay you get ?"


The same amount of delay as live view via the Web UI.

TicoBytes.com | www.ticobytes.com
Ubiquiti Solutions and English / Spanish IT Support in Costa Rica
UBRSS, UBWA, UEWA - Ubiquiti Certified Trainer
Senior Member
Posts: 3,797
Registered: ‎09-12-2010
Kudos: 1326
Solutions: 31

Re: [Tutorial] Raspberry Pi 4-Cam Matrix Viewer Appliance


@ceejm1 wrote:

Hi,

 

Finally got time to try this out and having a bit of a problem...

 

When I reboot the Pi I see that the script has started but get nothing else. 

 

When I try to run a stream directly from the command line I get the attached. 

 

I assume its an issue/setting with omxplayer but thats where my abilities end...

 

Any suggestions gratefully received.

 

James


That "have a nice day Man Wink" text at the end means the omxplayer has quit.  I don't know what's up with the framerate though - have you tried a different stream (from a different camera or a different resolution)?

TicoBytes.com | www.ticobytes.com
Ubiquiti Solutions and English / Spanish IT Support in Costa Rica
UBRSS, UBWA, UEWA - Ubiquiti Certified Trainer
Emerging Member
Posts: 53
Registered: ‎04-13-2015
Kudos: 31
Solutions: 1

Re: [Tutorial] Raspberry Pi 4-Cam Matrix Viewer Appliance

[ Edited ]

I've tried it with both _1 and _2 streams and it just sits for a while then quits.  No framerate errors though.

 

All streams are enabled and play fine on VLC....

Senior Member
Posts: 3,797
Registered: ‎09-12-2010
Kudos: 1326
Solutions: 31

Re: [Tutorial] Raspberry Pi 4-Cam Matrix Viewer Appliance

I would try the stream from another camera, and/or rebooting camera and NVR.

 

It sounds like the stream itself is bad.  Have you tried opening the stream in VLC?

TicoBytes.com | www.ticobytes.com
Ubiquiti Solutions and English / Spanish IT Support in Costa Rica
UBRSS, UBWA, UEWA - Ubiquiti Certified Trainer
Reply