CabCam Goes UV4L

This is the updated CabCam software package running UV4L. It operates similarly to MJPEG Streamer but it has a WebGUI for on the fly changes and it generally runs better with less bandwidth useage. If you have already setup the hardware for your CabCam skip to step 3 for the updated software. JMRI’s WebThrottle with video will be the next post.

Overview

We describe how to set up a small computer to stream cab view video from an HO locomotive. This allows remote operation of trains at local or remote layouts, optionally connecting it to video conferencing software like Google Meet or Zoom.

Goals

  1. Easy to set up and maintain.
  2. Easy to access the cab view via a URL or via conference software eg. Zoom or Meet.
  3. Distribute the setup to others for the betterment of the hobby.
  4. Provide a low latency video stream to enable remote control via Engine Driver/WiThrottle
  5. Keep the hardware within HO NMRA clearances.
  6. Use commonly available components and open-source software where possible.

Implementation

Hardware: We are using one Raspberry Pi Zero W 2 per CabCam car. The original Zero works as well but is slower.

Camera: The Pi Zero Spy Camera works well for our purposes.

Software: The project uses Raspian Buster running UV4L conference streaming software on the CabCam car. This streams video from a URL on the local network. The video can be viewed locally or streamed to conference software via OBS and the OBS VirtualCam feature. This software runs Raspberry Pi OS Bullseye(Debian 11) 32bit.

Power: The Juiceb0x Zero is a “hat” for the Pi Zero. While the Pi Zero WH comes with the required header already installed, soldering is required to install header pins on the Juiceb0x Zero board.

Rolling Stock: The easiest approach is build a mounting sled out of styrene as shown below. With a little bit more work the setup can be installed inside a dummy engine, too.

IMG_20200710_191346

How To

This section describes the setup in more detail. Some familiarity with Raspberry Pi and respective software management is helpful.

1. Gathering the hardware

You will need for the self contained unit.

For the cheaper unit running a battery bank:

Raspberry Pi Zero 2 W

Small Battery Power Bank Amazon has some as well.
160 Degree Pi Zero Camera
SDHC card 8GB or larger
USB charger with microUSB charging cable

2. Setting up the hardware

Solder the male header to the Pi Zero 2. Connect the camera to the Pi Zero 2 W. Solder the header pins to the Juiceb0x Zero per the instructions that come with it and attach it to the Pi Zero 2. Connect the battery to the Juiceb0x Zero. Connect the USB charger to the Juiceb0x Zero to charge the battery.

Never connect power to the USB ports of the Pi Zero 2 W while the Juiceb0x Zero is attached. 

For the cheaper version you need plug in the camera then plug the USB micro cable into the power bank and then into Pi Zero. Be mindful of your NMRA gauge, if you need to you can build a sled to hold the Pi Zero on its side so the USB cable is vertical.

3. Burning the image and setting up the software. Those skipping to this step, do this on a second SD card so you don’t mess up your MJPEG Streamer install.

Head over to the Raspberry Pi Foundations website and download the Raspberry Pi Imager and install and run it.

Once open you will be presented with the option to CHOOSE OS, click this and select Raspberry Pi OS(other) and select Raspberry Pi OS Lite(32-bit). Once that is chosen click the gear icon on the main page.

Check Set hostname and set your hostname to whatever you want. I generally chose cabcam# where # is the number of the camera on the layout, eg cabcam1, cabcam2 etc. You want to then mark on the camera hardware which one it is.

Check Enable SSH which automatically checks use password authentication. Below that you can set the username and password. Make sure you write these down or otherwise remember them or you will be making yourself a new image.

Check Configure wireless LAN and set your SSID(Network name) and Password for the wireless network you are going to be using. This MUST be a 2.4ghz network for the Pi Zero W and 2 W. Set your Wireless LAN country to your country abbreviation.

Check Set locale settings and find your time zone. Then if you use a different keyboard layout than US, go ahead and set that.

Optional: Uncheck enable telemetry.

Click SAVE

Click CHOOSE STORAGE and choose your SD card from the options. Then click WRITE. Once the image is written eject your SD card and place it in your Pi. Power the Pi on and give it a few minutes.

4. Logging in to the Pi

Find the IP address your Pi is using via your router and log in using ssh. You may get away with just typing cabcam#.local

Use the credentials you set in step 3.

On a Mac use Terminal. On Windows use PuTTY (you may have to download it)  and put in the IP of your Pi in the Hostname block. Try cabcam.local if you can’t find the IP. When you connect use the credentials you set in step 3.

Windows 1909 and up can use the command prompt with the command shh username@ipadressofpi to connect now so you don’t need putty.

On many networks the following also works:

ssh username@cabcam.local

5. Configuring the Pi

Once on the Pi command line type

sudo rpi-update and update the firmware.

echo /opt/vc/lib/ | sudo tee /etc/ld.so.conf.d/vc.conf

sudo ldconfig

sudo raspi-config

to enter the configuration page for the Pi. Arrow down to 3 Interface Options and open I1 Legacy Camera. Follow the prompts to enable legacy camera drivers so we can use UV4L. Exit the config utility.

6. Install UV4L.

Now we are going to configure the software to allow us to stream the cab POV and you can configure the settings you want.

First run the following command to add the UV4L repo to your install.

curl https://www.linux-projects.org/listing/uv4l_repo/lpkey.asc | sudo apt-key add –
echo “deb https://www.linux-projects.org/listing/uv4l_repo/raspbian/stretch stretch main” | sudo tee /etc/apt/sources.list.d/uv4l.listing/uv4l_repo/lpkey

Then run sudo apt-get update, then run sudo apt-get upgrade and accept any prompts.

Run the following accepting any prompts.

sudo apt-get install uv4l uv4l-raspicam

sudo apt-get install uv4l-raspicam-extras

Now you are going to configure UV4L

sudo nano /etc/uv4l/uv4l-raspicam.conf

Set resolution to 640×480 20fps quality to 8(no joke it works great). You can increase this if you are only streaming localy over wifi.
I don’t suggest going much higher if you are going over the internet due to low upload speeds.

You may need to set rotation to flip the camera if your Pi camera is on its side, it goes by degrees so rotation = 180 will flip the image around 180 degrees.

Also find the following and uncomment them.

server-option = –connection-timeout=15

server-option = –enable-keepalive=yes

server-option = –keepalive-timeout=7

and make sure server-option = –max-streams=5 is uncommented and =5. The newer UV4L holds on to connections a little too long.

Run sudo reboot

Now it is time to test the stream.

Open a browser and head to http://IPofPi:8080/

Click MJPEG/Stills Stream and you should see the stream from your camera. If not turn off the Pi and check your cable seating. That is the issue 99% of the time.

The Internet uplink bandwidth at SVL is quite limited. For cab control with remote operators we found that SD resolution is sufficient and limits bandwidth demand. We are using a frame rate of 20 images per second. The stream doesn’t get smoother turning this up.

The camera stream is set to startup on boot.

Put your CabCam car on the front of a train, and run the train while watching the video stream in the browser.

Bernhard wrote down some thoughts on camera placement and car choice on his blog.

9. What’s Next?

To stream the video to remote operators we use OBS with the built in Start Virtual Camera option, configured with CabCam feeds. This is connected to Google Meet and can even stream to Youtube Live. Other video conferencing software like Zoom, Duo, Skype, etc. can be used, too. Remote operators join the video conference and use Engine Driver or WiThrottle to control trains via JMRI over a VPN. You can also run your train all from JMRI’s WebThrottle which will be covered in the next post. OBS will be covered after that because the real star of this show is the WebThrottle.

10. Things to Consider.

If you plan to have multiple cameras you should look into making an image of your SD card so you can burn them to other SD cards without having to do all the above steps. I will leave it up to you to Google “imaging raspberry pi SD card for (insert OS here).” Once you have the image backed up and burned to a new SD card you should run sudo raspi-config and change your hostname to increment your camera name.

Variations

Hardware: While we are using a Raspberry Pi Zero 2 W, any Pi with built-in Wifi will work.

Camera: Any compatible Raspberry Pi camera can be made to work. Possible options are the official Pi camera, the spy cameras, or other compatible cameras available from e.g. Adafruit.

Power: The Zero2Go board is an alternative to the Juiceb0xZero. However, the Zero2Go can’t charge the battery. In either case, do not power the Pi from the onboard USB while the power solution board is attached.

Some people had success with using a small form-factor USB power bank connected to a Pi Zero without additional hardware.

Revisions

Over time we have made corrections to these instructions, or fine tuned settings. Below is a list of important revisions.

  • 8/16/22 Added some information to make the CURL commands work in step 5.

Some sources:

Installation for ARM (Raspberry Pi)

Summer Virtual Open House Sat July 16th 10am

Join Silicon Valley Lines for our virtual open house Saturday July 16th at 10am-3pm pacific time. Run a train around our layout and get a idea what remote operations are like.

What: SVLs Virtual open house where you can take control of a train on our 23’x73′ layout and get a engineers POV of our layout and a light demonstration of our virtual operating sessions ran monthly.

When: Saturday July 16th from 10am to 3pm pacific in half hour running blocks.

Where: Your home, office, RV on a camping trip with good camp ground WiFi, another club, etc. The virtual throttle and camera feed can be run from an Android/iOS phone or tablet or a Windows/Mac/Linux based computer running a modern browser like Chrome or Safari.

How: Use this link to sign up, information will follow before the Open House.

If you are unsure about participating we have some demonstration and examples below.

We look forward to seeing you there!