Setup Multiple Webcams with OctoPrint

Posted on 07/01/2021

It is most useful to setup multiple webcams with OctoPrint. Personally I find that have a nozzle camera in addition to a camera viewing the whole bed is very useful. It allows you to clearly view the effects of flow and feed rate adjustments and can offer much insight into why prints don’t adhere properly or why they are failing. Additionally with Mattacloud you can truly leverage the advantages of multiple cameras improving the reliability of the AI error detection in addition to being able see more when working remotely.

There are a few challenges with using multiple webcams with OctoPrint, these are to do with:
- Power
- CPU
- USB Bus Capacity
- Internet Speed
These four will impact your ability to run multiple cameras with OctoPrint.

Power

If you a using a Raspberry Pi the first challenge you will encounter will be power. It is likely that the Pi on its own will not support 2 active webcams from its USB ports. I have managed to get two working at the same time, but frequently there can be power drops. How do you check this? Well, if you run the command vcgencmd get_throttled and a non-zero value is returned you’re likely having power drops. There is hope though, you can get hold of a USB hub to power the device completely. I recommend using the RPi Powered USB Hubs list to find one that works for you.

CPU

If your OctoPrint is installed on a normal PC then you are fine both for CPU and Power. If however like most people you are running it on a Raspberry Pi then it may be a little more challenging. The majority of modern web cameras have some encoding done on the device itself, this reduces the work that your RPi has to do. The main thing to look for is called USB Video Class or UVC. Webcams without this are going to require far more computational power from your RPi so make sure you choose a UVC webcam. I have had good results with Logitech C270s.

USB Bus Capacity

You might need to be concerned about the overall capacity of the USB bus on your device. With a standard PC will you probably be okay as you likely have 2 USB hubs internally - this however is not the case with the RPi. You might max out with 2 webcams, if you do on Linux you will receive a no space left on device error if you are using all the bandwidth on the bus when setting up the second camera. USB 3.0 offers a greater bandwidth so you will probably have better results if the device has USB 3.0.

Internet Speed

This normally is not a problem and may depend upon your network and current activity. When streaming two videos streams at the same time, especially if at high resolutions and frame rates, you may notice a hit on your network speed. This is very unlikely though and I would be surprised if you encountered this issue.

Setup

For this guide I am assuming that you are running some version of Linux on your Raspberry Pi or PC. It is still doable on both Windows and Mac - maybe I will cover this in a future guide.

Once you have plugged in your two webcams open your terminal to check they are being picked up. Type in ls /dev/ to check the devices available, your cameras will show up as video{n} with {n} being a number. Some cameras such as the Logitech C270 may appear has two video devices, usually the number to remember if the first of the two.

/dev/video0 
/dev/video1

You can check which camera connected to which USB port corresponds to each number by unplugging a camera and doing ls /dev/ again to see which video device is left.

Now that you know which camera is which it is time to set up the second camera. Head over to the /root/bin directory.

cd /root/bin
ls 
gencert  git  webcamd

You now need to make a copy of the webcamd file and also copy the octopi.txt configuration file.

sudo cp /root/bin/webcamd /root/bin/webcam2d
sudo cp /boot/octopi.txt /boot/octopi-cam2.txt

You should now edit the configuration files for your cameras, in this case octopi.txt and octopi-cam2.txt.

sudo nano /boot/octopi.txt
sudo nano /boot/octopi-cam2.txt

I will assume that all the cameras you are using are USB webcams, but you could use a RPi camera with a USB webcam. The process will be the same just look for camera_raspi_options in the settings.

Locate the camera_usb_options in the file and point this to the webcam of your choosing. You can also set the resolution and frame rate.

camera_usb_options="-r 1280x720 -f 30 -d /dev/video1"

The above configuration sets the resolution with -r to 1280x720, the frame rate with -f to 30 FPS and device with -d to /dev/video1. You can just change the device to suit your setup.

Now go to the bottom of the file to make another change. The default port set will be 8080. One file can stay as 8080 and the new file for the second camera we shall change to 8081.

camera_http_webroot="./www"
camera_http_options="-p 8081"

We now need to tweak the webcam2d file to point to the correct configuration file.

sudo nano webcam2d

Once the file is opened find where is says octopi.txt and replace the snippet with the following:

if [ -e "/boot/octopi-cam2.txt" ]; then
    source "/boot/octopi-cam2.txt"
fi

Now you can test if the camera is working by running the shell script webcam2d. It should output something like the following:

Starting up webcamDaemon...

--- Configuration: ----------------------------
camera:        auto
usb options:   -r 1280x720 -f 30 -d /dev/video1
raspi options: -fps 20
http options:  -w ./www-octopi -n
-----------------------------------------------

If you are not getting any error messages, all is good and we can plough on!

The next section will vary depending on your particular network and OctoPrint setup. To check your current webcam settings head to the “Webcam & Timelapse” settings tab in OctoPrint.

Now we need to make the second webcam start automatically at boot. Two files need to be copied and slightly modified for this to happen.

sudo cp /etc/init.d/webcamd /etc/init.d/webcam2d
sudo cp /etc/default/webcamd /etc/default/webcam2d
sudo nano /etc/init.d/webcam2d
sudo nano /etc/default/webcam2d

In the new webcam2d files you should look for any instance of webcamd and replace it with webcam2d.

This will now enable the second webcam at boot and if you reboot it should start up and be accessible.

If it is not working it may be worth checking the permissions of all the files you have just created / copied. You can do this with ls -la. If the permissions seem incorrect you can change them with chmod.

In some newer versions of OctoPi you may not find the /etc/init.d/webcamd file. Instead the webcams are run through systemd. In this case you will have to copy over the following file and again change the contents of the new file from webcamd to webcam2d.

sudo cp /etc/systemd/system/webcamd /etc/systemd/system/webcam2d
sudo nano /etc/systemd/system/webcam2d

You will then need to enable your new camera and start it.

sudo systemctl enable webcam2d
sudo systemctl start webcam2d

Recent tutorials


Setup Multiple Webcams with OctoPrint

Posted on 07/01/2021

It is most useful to setup multiple webcams with OctoPrint. Personally I find that have a nozzle camera in addition to a camera viewing the whole bed is very useful. It allows you to clearly view the effects of flow and feed rate adjustments and can offer much insight into why…


FDM 3D Printer Materials Guide

Posted on 18/12/2020

This guide summarises a wide range of properties and characteristics for a variety of 3D printable materials. This overview by no means covers all the possible materials which are available to users of extrusion 3D printers, however we have aimed to cover the most frequently u…


OctoPrint Installation and Setup

Posted on 14/11/2020

OctoPrint is an open source web interface for your 3D printer that allows you to control and monitor all aspects of your printer and print jobs, right from your browser.

OctoPrint can run on a number of different systems a…


Connecting your printer to Mattacloud

Posted on 03/11/2020

In this tutorial we will walk you through the simple process of hooking your 3D printer up to the Mattacloud so that you can benefit from all of the features Mattacloud provides.

At present Mattacloud requires you to be using OctoPrint with your 3D…