In this article I will describe how to setup OctoPrint based on OctoPi on a Raspberry Pi 3 for multiple printers. For every printer a seperate OctoPrint instance with a different port must be started. If you want to use more than one webcam, you also have to start seperate instances of MJPG-Streamer for each webcam. I have also added a link to another article where I give a short overview about how you can switch on and off your printers with the use of a n-channel relay module. First we set up OctoPrint for just one printer and after that works, for multiple printers and webcams.

Necessary Components

To run OctoPrint on a Raspberry Pi not much components are needed.

Raspberry Pi 3 (RPi) or newer

MicroSD card with 8GB or more

Power supply with at least >=2.0A output current

A webcam if you like to see what your printer is doing

A printed case for the RPi

If you want to set it up by using a connected HDMI Monitor and a USB keyboard (I will!) you also need the following

HDMI monitor

HDMI cable

USB keyboard

That's all you need to setup OctoPrint on your RPi. If you want to be able to switch your printers on and off you can also have a look at this article: "How to switch on and off your 3d printer with your Raspberry Pi"

Where to start with ?

The first step is to install the OctoPi image on your SD card. There are a lot of tutorials out there which shows how to do it. The steps are quite straight forward if you use a Windows OS.

After installing the firmware onto the SD card we should be able to do some configurations in the octopi-network.txt and octopi.txt file on the boot partition on the SD card. This is the only folder you can see under a Windows system. I had some problems making some configurations with that file. I wanted to create a wifi connection with a static IP but nothing worked as it should because I did a mistake. If you make any mistake with your wifi connection in that file before the first boot, you have to correct it later directly in your system. So it seems to me, that these files just used once after the first boot for creating the wifi connection. I recommend to not make any configurations for a wifi connection in that file. If you connect your RPi via an ethernet cable to your router you don't have to change anything if getting a IP over DHCP is okay for you. Therefore a DHCP server must be running in you network.

Connect a monitor via a HDMI cable to your RPi and also connect a USB keyboard to it. It is then possible to do the configuration locally without using the octopi-network.txt file or a SSH connection at this point.

Set up the wifi connection

The configuration for the interfaces in this case for the wifi connection is done in /etc/network/interfaces

I also added some lines (the last 3) to prevent the wifi module from going into power saving mode. This was helpful because I got some weird connection problems. The rest of the interfaces file is untouched.Some additional german information about how to prevent the wifi module from shutting down to power save mode can be found here: https://www.elektronik-kompendium.de/sites/raspberry-pi/1912231.htm

Because we are using a WPA encrypted wifi connection we have to modify the /etc/wpa_supplicant/wpa_supplicant.conf file.

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

Add a network which includes the SSID and the PSK of your wifi connection.

network={ ssid="beehive" psk="supersecurekey"}

After a reboot the RPi should be able to connect to your router and you should be able to connect to it via SSH. Under a Windows systemyou can use PuTTY or KiTTY for that.

Basic configuration

Some basic configurations like "Expanding Filesystem", "Change User Password", "Localisation Options" and more can be done by using the tool raspy-config.

sudo raspi-config

After you expanded the file-system and have finished with the raspi-config configurations it is time to setup your OctoPrint user, your printer and the rest of the OctoPrint configuration. Therefore use your favourite browser to connect to the OctoPrint webserver: IP assigend by your router e.g. http://192.168.x.y

Connect the different printers to the RPi

Now it is your turn! Connect your printers, set up its profiles, play around with OctoPrint. Set up every printer you have and create a working profil for every. At this stage you are then able to plug in all of your printers but only able to connect to and print with one at once. During the first printer is printing, you are not able to connect to a second one without disconnecting the connection to your actually printing printer! When the first one has finished its job, than you are able to disconnect and connect to the second and print with this one. This is the reason why we need a seperate instance for every printer, more about that later.

Create some udev rules for individual printer device names

It can be a mess to find the corresponding device for a specific printer. The device naming /dev/ttyUSB0 is generally made in a sequential order. So in this case the first printer plugged in will get /dev/ttyUSB0 the second one /dev/ttyUSB1 and so on. It is not guaranteed, that this naming is the same after a reboot. So we will use udev to create some symlinks to devices which will have the same name and correspond to the same printer before and after a reboot. I use ttyMKC for my core printer and ttyANET for my Anet printer.

First we have to determine the differences of the respective devices, so we can give udev some attributes so it can differ between the devices (printers). If you are lucky your devices differ in a lot of attributes. It can also be that they are nearly equal or complete equal! In my case only the attributes "KERNELS" and "devpath" where different.

To determine the differences you can do it manually by looking at the outputs of udevamd for the different devices. The following command is for /dev/ttyUSB0.

udevadm info -q all -n /dev/ttyUSB0 --attribute-walk

You can also pipe the outputs to files and the use diff to get the differences.

and insert the following two lines of code. Each line is a rule for each printer we want to use later. These two lines will only work in my case! You have to use the attributes which differ in your case! Not every attribute has to be different, but at least one attribute must be different!

Setting up one more instance of OctoPrint

When your printers print over OctoPrint like they should and every printer has its individual device names e.g. /dev/ttyMKC and /dev/ttyANET we can go further to the next step, the setup of another instance of OctoPrint. Therefore 4 steps are necessary:

Copy OctoPrint directory

Copy and modify OctoPrint2 config script

Copy and modify OctoPrint2 init script

Add new OctoPrint2 init script to autostart

First we copy the OctoPrint directory

cp -R /home/pi/.octoprint /home/pi/.octoprint2

next copy the OctoPrint configuration script /etc/default/octoprint to /etc/default/octoprint2

OctoPi uses systemctl, so it is important to reload the daemon list after a new init script is added.

sudo systemctl daemon-reload

We also want that this starts automatically after booting, so we update the rc.d

sudo update-rc.d octoprint2 defaults

To start the service manual you can use

sudo /etc/init.d/octoprint2 start

To check if the octoprint2.service has been added successfully to systemctl run

systemctl status octoprint2.service

That's it for the moment, if you don't get any errors it works like it should.

Take a beer and make a break.

HaProxy Configuration

OctoPi uses HaProxy as a reverse proxy for mapping and security reasons. If HaProxy is disabled and not used

sudo /etc/init.d/haproxy stop

the webservers of the OctoPrint instances will be reachable on port 5000 and the second on port 5001 (e.g. http://192.168.x.y:5000)

I don't recommend to disable HaProxy because you will loose a neadful security barrier. But for now let it disabled.

If you use multiple OctoPrint instances and you want to make them reachable as a directory name instead of using a port, this can and must be done using HaProxy. Therefore modify the /etc/haproxy.cfg for your needs.

First make a backup of the original file.

sudo cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.old

Then open the config file.

sudo nano /etc/haproxy/haproxy.cfg

My haproxy.cfg looks like this and is configured for 3 printers also a CTC printer on port 5002:

For each backend except for the webcam backend, it is important to define the correct X-Script-Name! I wasted a lot of time finding this necessary configuration parameter.

How to use a second webcam?

The first webcam which is connected gets the device name /dev/video0 the second the device name /dev/video1 and so on. We run into the same trouble when we started connecting the different printers to the RPi. We have to create udev rules to get a device name which is the same every time we restart the RPi.

To determine the differences we pipe the outputs to a file and the use diff to get the differences.

and add the following two lines of code. Each line for each webcam we want to use. These two lines will only work in my case! You have to use the attributes which differ in your case! Be carefull with the devpath attributes. Only use them if find no other attributes which differ or if you now what you are doing!

Now we can to start the default instance of Mjpg-Streamer with the device parameter -p /dev/videoMKC and a second one with the device parameter -p /dev/videoANET. We can now make our settings in the daemon. First make a backup and the a copy for the second instance.

Because we use two daemons for each camera one, we cannot make our configuration in the /boot/octopi.txt file. We have to do the modifications direct in the deamon file but that's okay. We add the -d parameter and the device to the camera_usb_options variable and the port parameter to camera_http_options. This then looks like this: