Support 2 cameras in Printoid PRO and PREMIUM (tutorial for OctoPi)

Printoid is the only app to brings you the support of not only one but two video streamings!

This feature is only available in Printoid PREMIUM.

This tutorial is for those who have installed OctoPi only.

If you have installed Raspbian + OctoPrint please follow this one.

INTRODUCTION

Having two camera feeds is a must have for those who are accustomed to leave their 3D printers printing alone, all day and all night long. With the support of 2 cameras, you can keep an eye to your 3D printer from different angles. Or keep an eye to 2 3D printers at the same time 😉

PREREQUISITES

This tutorial assumes that the following facts are gathered:

  • You have a sufficient knowledge in Linux scripts and commands
  • Your Raspberry Pi is at least a Raspberry Pi 3B (for the CPU consumption)
  • Your Raspberry Pi has a great network coverage and the available bandwidth is sufficient for a second camera stream
  • You have (eventually) an USB powered hub for the second camera, or at least a 2.5A power supply.

Important note 1: this tutorial is based on OctoPi. It can not be applied to other system images, such as manual installation of OctoPrint on Raspbian.

Important note 2: this tutorial is based on the use of 2 USB webcams (Logitech C270). You can follow it if you have different USB webcams, or 1 USB webcam and the official Raspberry Pi Camera, but you may have to adjust the scripts by yourself.

PREPARATION

Before adding the second camera, please be sure you have followed first these tutorials:

Shutdown your Raspberry Pi and plug the two cameras on. Then, start your Raspberry Pi and wait for the boot completion. If everything is working well (OctoPrint is accessible and the first camera is still accessible, so you can go to the next step. If not, please get a better power supply (at least 2.5A) or please plug the USB cameras on a USB powered hub).

You should also check in /dev/ if you properly see both of the video0 and video1 devices (when connected to the Raspberry Pi over SSH, using a terminal) :

ls -l /dev/ | grep video

You should see at least these two lines:

crw-rw----+ 1 root video 81, 0 Mar 4 00:17 video0
crw-rw----+ 1 root video 81, 1 Mar 4 00:17 video1
 
 

MJPG-STREAMER CONFIGURATION

SSH on your Raspberry Pi, then edit the file ~/mjpg-streamer/start.sh using nano:

nano ~/mjpg-streamer/start.sh

Comment the following line (the one in red) by adding a # in front of it:

#/******************************************************************************
# #
# MJPG-streamer allows to stream JPG frames from an input-plugin #
# to several output plugins #
# #
# Copyright (C) 2007 Tom Stöveken #
#******************************************************************************/

## This example shows how to invoke mjpg-streamer from the command line

export LD_LIBRARY_PATH=”$(pwd)”
#./mjpg_streamer -i “input_uvc.so –help”

#./mjpg_streamer -i “./input_uvc.so” -o “./output_http.so -w ./www”
#./mjpg_streamer -i “./input_uvc.so -d /dev/video0” -i “./input_uvc.so -d /dev/video1” -o “./output_http.so -w ./www”
#valgrind ./mjpg_streamer -i “./input_uvc.so” -o “./output_http.so -w ./www”

Then add the following lines in this file (above of the line you’ve just commented would be great, or at the end of the file, as you want):

# Start video0 on port 8080
./mjpg_streamer -i “./input_uvc.so -d /dev/video0 -r 640×480 -f 5 -y” -o “output_http.so -w ./www -p 8080”
# Start video0 on port 8081
./mjpg_streamer -i “./input_uvc.so -d /dev/video1 -r 640×480 -f 5 -y” -o “output_http.so -w ./www -p 8081”

Save your changes using CTRL+X then type Y (yes)

These two lines will start two mjpg_streamer processes:

  • One for the first camera (/dev/video0) on port 8080
  • One for the second camera (/dev/video1) on port 8081

You can also notice the following parameters:

  • The resolution (-r 640×480) means it is limited to 640×480. You can change it to the desired value (or remove it to use the default value) at your own risk.
  • The framerate (-f 5) means it is limited to 5 images per seconds. You can also change it to the desired value, at your own risk.

Don’t try to stream 2 cameras at the same time at a HD resolution with a 20fps framerate… your Raspberry Pi will not support the CPU charge.

 
 

HAPROXY CONFIGURATION

You have now to configure haproxy to make the second camera reachable on the port 80 too. So you have to:

  • Give a suffix to the second camera streaming
  • Make it accessible on the port 80 too

Simply edit /etc/haproxy/haproxy.cfg :

sudo nano /etc/haproxy/haproxy.cfg

This tutorial assumes that you have configured haproxy to use the basic authentication. Please refer to this tutorial if it is not done yet: https://printoid.net/access-octoprint-from-the-internet/ (from step 3: Enable the basic authentication)

Don’t forget to replace your basic authentication username and password (hightligthed in red) with your own values. I advise you to edit the file step by step with the changes instead of copy-past the whole script.

Important note: Please take care to the haproxy.cfg file indentation. Do not use tabulations, please use spaces instead.

global
        maxconn 4096
        user haproxy
        group haproxy
        log 127.0.0.1 local1 debug

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        retries 3
        option redispatch
        option http-server-close
        option forwardfor
        maxconn 2000
        timeout connect 5s
        timeout client  15min
        timeout server  15min

frontend public
        bind *:80
        bind 0.0.0.0:443 ssl crt /etc/ssl/snakeoil.pem
        option forwardfor except 127.0.0.1
        use_backend webcam if { path_beg /webcam/ }
        use_backend webcam2 if { path_beg /webcam2/ }
        default_backend octoprint

backend octoprint
        acl needs_scheme req.hdr_cnt(X-Scheme) eq 0
        reqrep ^([^\ :]*)\ /(.*) \1\ /\2
        reqadd X-Scheme:\ https if needs_scheme { ssl_fc }
        reqadd X-Scheme:\ http if needs_scheme !{ ssl_fc }
        option forwardfor
        server octoprint1 127.0.0.1:5000
        errorfile 503 /etc/haproxy/errors/503-no-octoprint.http
        acl AuthOkay http_auth(L1)
        http-request auth realm octoprint if !AuthOkay

backend webcam
        reqrep ^([^\ :]*)\ /webcam/(.*)     \1\ /\2
        server webcam1  127.0.0.1:8080
        errorfile 503 /etc/haproxy/errors/503-no-webcam.http

backend webcam2
        reqrep ^([^\ :]*)\ /webcam2/(.*)     \1\ /\2
        server webcam2  127.0.0.1:8081
        errorfile 503 /etc/haproxy/errors/503-no-webcam.http

userlist L1
        group G1
        user USERNAME insecure-password PASSWORD groups G1
Save your changes using CTRL+X then type Y (yes)
 
 

IP TABLE UPDATE

Mjpg-streamer does not allow to bind to a specific interface to limit the accessibility to localhost only.

If you want your octoprint instance to be reachable from the internet you need to block access to port 8081 from all sources except localhost if you don’t want the whole world to see your webcam image.

To do this simply add iptables rules like this:

sudo /sbin/iptables -A INPUT -p tcp -i wlan0 ! -s 127.0.0.1 --dport 8081 -j DROP    # for ipv4
sudo /sbin/ip6tables -A INPUT -p tcp -i wlan0 ! -s ::1 --dport 8081 -j DROP         # for ipv6

Replace the interface with eth0, if you happen to use ethernet.

To make them persistent, they need to be saved. In order to be restored at boot time, the easiest way is to install iptables-persist:

sudo apt-get install iptables-persistent

The only thing left to do now, is save the rules you have added:

sudo /sbin/ip6tables-save > /etc/iptables/rules.v6
sudo /sbin/iptables-save > /etc/iptables/rules.v4

If these two last commands failed, then please send the command su before in order to get the privileges.

(source for this 6th point: OctoPrint GitHub)

At this point, please reboot your Raspberry Pi to take the changes into account.

 
 

CONFIGURE PRINTOID

Open the Printoid’s settings and go to the Video streaming settings. Scroll down until you find the parameters for the second camera:

You will be able to define the new streaming and snapshot path (or full URL) for your second camera, and to customize its rotation and flip options.

Please provide both streaming and snapshot path (or full URL) otherwise Printoid wont be able to display the streaming video in the app.

In the example of the screenshot above, I’ve defined the /webcam1/ path as my second camera for personal reasons 🙂

 
 

EXAMPLE IN PRINTOID

Here you are! Everything is configured and should work well right now 🙂 Here are some screenshots to present you how Printoid handles and displays the 2 camera feeds in its interface.

(The streaming panel in portrait mode)

(The streaming panel in landscape mode)

(The streaming by overlay (floating button) in portrait mode)

(The right panel in portrait mode, first camera selected)

(The right panel in portrait mode, second camera selected)

(The launcher widget in portrait mode, one widget per camera)

FILES

Here is a example of the configuration of one of my OctoPi server. This example uses 2 USB webcams (Logitech C270). It works like a charm 😉

https://nofile.io/f/1RnawTokbyX/OctoPi-2cam.zip

 

OCTOPRINT PLUGINS

There are OctoPrint plugins to see both cameras at the same time.

Both plugins are working well with the solution of this tutorial.

SPECIAL THANKS

You can find the official tutorial on the Foosel’s Github here. Thanks to Gina for her amazing work!


6 thoughts on “Support 2 cameras in Printoid PRO and PREMIUM (tutorial for OctoPi)

    1. Raul,
      There is nothing to install. That’s the ZIP which contains the files shown during this tutorial. Unzip it, you’ll get the files. Then follow the tutorial
      Anthony

      Like

  1. Excellent guide Anthony! I’ll try setting up this weekend with the new endoscope camera I got for my Ender 3.

    Like

  2. This appears to be no longer current in the new version of ocotpie. The Part about haproxy.cfg is correct however to add another camera you need to not edit start.sh but instead make a config file like so:

    In my case I have two USB cameras /dev/video0 and /dev/video2

    Make config dir:
    “`
    sudo mkdir /boot/octopi.conf.d/
    “`
    Create configs for webcam1 and webcam2
    “`
    sudo nano /boot/octopi.conf.d/webcam1.txt
    “`
    “`
    camera=”usb”
    camera_usb_options=”-r 640×480 -f 10 -d /dev/video0″
    #additional_brokenfps_usb_devices=()
    #camera_raspi_options=”-fps 10″
    #camera_http_webroot=”./www-octopi”
    camera_http_options=”-p 8080″
    “`
    “`
    sudo nano /boot/octopi.conf.d/webcam2.txt
    “`
    “`
    camera=”usb”
    camera_usb_options=”-r 640×480 -f 10 -d /dev/video2″
    #additional_brokenfps_usb_devices=()
    #camera_raspi_options=”-fps 10″
    #camera_http_webroot=”./www-octopi”
    camera_http_options=”-p 8080″
    “`

    Reboot system.

    Like

Leave a comment