3D Printer Automation with Raspberry Pi
Following our previous post, we continue our 3D printer upgrades series with the most popular remote control modification – OctoPrint!
What is OctoPrint and why do you need it?
OctoPrint is a free and open source software, that allows users to monitor and control printers through a web based GUI. Although you could install it under Windows , it usually runs on a Raspberry Pi computer with the Octopi operating system. Given that the core software includes plenty of useful tools and the OctoPrint community has developed a number of cool plugins, we are sure this upgrade will greatly improve your 3D printing experience.
OctoPrint grants you:
- remote access – With core OctoPrint you can, for example, set temperatures, upload G-code and start/stop each connected printer in your local network. Imagine being able to start a print job with a few mouse clicks from the comfort of your desk (or bed, for that matter).
- monitoring – If you choose to invest in a camera for your OctoPrint, you will be able to watch your 3D printer in real time. This is quite useful if you are in a different room and want to make sure the printer is working fine. Nobody likes a ruined print but having your shop on fire because you left the printer unattended is a far bigger problem. Along with the added safety, the camera lets you make time lapse videos of all your prints – great for showing your friends (or customers!).
- additional functionality – There are some OctoPrint plugins that allow you to control the GPIO (general-purpose input / output) pins of the Raspberry Pi. This means you could extend the reach of the software beyond the printer itself, adding sensors or controlling lights / fans, turn on/off the main power, etc.
- plugins – Being open source, OctoPrint has a great number of free plugins. Some of them modify existing OctoPrint features (TheSpaghettiDetective plugin lets you control your printer from anywhere through your smartphone) and some add new functionality (PrintJobHistory lets you keep detailed information and an image of past print jobs). No matter what you want to achieve, chances are somebody has already made it for you!
Installation and setup guide
Step 1: Buy hardware
There are only a few pieces of hardware that you need to get OctoPrint up and running:
- Raspberry Pi – The official OctoPrint website recommends Raspberry Pi 3B, 3B+ or 4. If you have a Raspberry 2 around, you can follow this guide. On the other hand, if you are buying a new one, the brand new, (up to) 8 GB RAM Raspberry Pi 4 might be an overkill for the task at hand. We are using the 3B version and (except for some long time lapses) everything works fine, including the live stream and the plugins we have installed.
- Raspberry Pi camera – The Raspberry Pi camera is not a requirement but it greatly improves the remote control experience. One that will work out of the box is the official camera from the Raspberry Store but you can use a USB camera as well. If you get the Raspberry Pi camera module, make sure the connection strip is long enough for your project. The default length is about 20 cm so we had to order a longer replacement in order to connect the camera on the print bed to the Raspberry outside the box.
- SD card – Raspberry Pi itself does not have a memory storage (like the hard disk your PC has). It uses a micro SD card to store both the OS and all other files you use. We recommend using a 16 or 32 GB card. You can read more here.
- USB cable – You need a USB cable to connect the Raspberry Pi to your printer. For Ender 3 that is USB A (male) to USB B mini (male). Check if your printer comes with a cable and if not have a look at the USB port on the printer to determine what size exactly you need.
- Power supply – Raspberry Pi can be powered from a PC / laptop USB port, but if you want it to work independently, you need a power adapter. You can get one from the official website or check the details on their webpage and buy one at your local store (look for the USB micro option). We use a 5 V / 3 A power supply.
Step 2: Download software and burn the SD card
Once you have the computer itself you will need to install the OS – in our case Octopi. To do that, download the Octopi OS image as well as Etcher – a tool to burn the OS on the SD card. Open the download location of the Octopi and extract the archive. You should see a file with an “.img” extension. Insert the SD card in your PC and open Etcher. Select ‘Flash from file’ and the .img file you have just extracted. Then choose the appropriate drive where the SD card is and click Flash!
Step 3: Prepare the Octopi OS
After Etcher is done, a pop-up window might appear, prompting you to format your SD card. You must not do that but instead click cancel and open the SD card folder instead (if you click Format disk the card will be swept clean and you will have to start over).
Once in the ‘boot’ folder, locate the ‘octopi-wpa-supplicant.txt’ file and open it using a text editor (such as Notepad).
What we are doing here is setting up automatic wifi connection for our Raspberry PI computer. To do that you need to uncomment (remove the # signs) the four lines in the WPA/WPA 2 block and replace the text between the quotes to match your network credentials. You can copy and paste the block multiple times if you want to give the Raspberry access to more than one network (don’t forget the bracket } at the end).
Also, make sure you select the correct country code at the end of the file (you can replace the currently active ‘GB’ with the two-letter country code for your location).
Save the file and close the editor.
One last thing: in order to enable remote access through the secure shell (ssh) you need to create an empty file called ‘ssh’ in the boot directory. To do that, check the ‘file name extensions’ box from Windows explorer options menu. Then create a new text file and delete the .txt extensions. Confirm the change and you are done. You can now eject the SD card.
Step 4: Connect and set up
Insert the SD card in the Raspberry Pi, plug in the printer and connect the power. There is no ‘On/Off’ button so as soon as you plug in the power USB, your Pi will turn on with the OctoPrint server following – give it a minute to load.
To access the GUI you need to open a browser and go to http://octopi.local . Alternatively you can type the IP address of the Raspberry Pi directly (something like 192.168.1.5). If you don’t know that IP address you can use one of many software tools to scan the network (we recommend Angry IP scanner) or connect to your router and check there.
On your first login you will need to go through the setup wizard, choosing name and password as well as some other options. Chriss Riley has a great video on installing OctoPrint in case you would rather watch than read.
So, here you are – OctoPrint is ready to go!
On the left you have the Connection panel where you will select the USB port in which your printer is plug in. Below that is the State panel which shows you if the printer is currently running, remaining printing time, etc. Next is the Files panel where you can upload G code files and select a print job.
The section on the right consists of tabs where you can, among other things, set and monitor the temperature of the tool and the bed or control the printer axes and stream live video if you have a camera installed.
Step 4: Installing plugins
OctoPrint plugins can be easily accessed through Settings (wrench icon) / Plugin manager. To add new ones click on ‘Get more’ button at the bottom.
Here we list some of the plugins we use to optimize our work:
- Navbar Temp – Brings the tool and bed temperatures from the Temperature tab to the navigation bar where you can easily see them at all times. More, Navbar Temp lets you run a custom Linux command and display the result on the Navbar. Check the bonus section at the end to see how we added a custom sensor to the printer enclosure!
- Access anywhere / The Spaghetti detective – A great tool that allows you to expand your reach and control the printer from anywhere in the world! Timelapse videos, G code upload and AI failure detection are also available!
- Printjob history – A very useful tool to keep track of past prints. If you collect information on the printer settings, times and materials you can easily debug problems and see what changes lead to good or bad results.
- Full-screen webcam – This plugin is a simple one but makes printer monitoring so much more comfortable!
- GPIO control – This plugin lets you create a custom button for the GUI that controls a specific GPIO pin. Great for controlling a power relay for a LED lamp or the printer power supply (check the bonus section of this post to see how it’s made).
Depending on your specific requirements and setup, you might find other OctoPrint plugins useful. We recommend you have a look at the Octoprint plugin repository and pick the ones you like.
Bonus 1: Temperature and humidity sensor
In our previous post we built a 3D printer enclosure in order to keep the inside temperature high and improve the ABS print quality. In order to check that the design really works we added a temperature sensor and connected it to OctoPrint. That way we can always check the conditions inside the enclosure directly from our main control window.
We selected the Adafruit HTS221 – low voltage (3 ~ 5 V), high range (-40 ~ 120 deg C), accurate and cheap temperature / humidity sensor, that will easily connect to the Raspberry via the I2C protocol. There are many other sensors that you can choose from depending on your goals and measurement requirements (check the DHT11 / DHT22).
If you choose the HTS221, check the guide below on how to make it work on Raspberry Pi. If you are using a different sensor, you can jump straight to how to link the data to OctoPrint.
To make HTS work via the I2C protocol we need to install several software packages. We tried to summarize the articles* listed at the bottom so that you can quick and easy set the system up. Assuming you have already installed Octopi, run the following in your project directory:
sudo apt update
sudo apt-get -y install python-pip3
sudo apt-get update
sudo apt-get upgrade
sudo pip3 install –upgrade setuptools
pip3 install RPI.GPIO
pip3 install adafruit-blinka
pip3 install adafruit-io
git clone https://github.com/adafruit/io-client-python.git
sudo apt-get install -y python-smbus
sudo apt-get install -y i2c-tools
sudo pip3 install adafruit-circuitpython-hts221
select Interface options and Enable I2C protocol.
Reboot the Raspberry Pi.
Here is the sample python code (i2c_test-single.py) we used to read data from the sensor:
i2c = busio.I2C(board.SCL, board.SDA)
hts = adafruit_hts221.HTS221(i2c)
print(round(hts.temperature,2),’ C —‘)
Note: ‘board’ library is part of the Adafruit package. If you get a ‘no module board’ error, you should not install it with a command like ‘pip install board’ because there is a different standalone library called the same way and your problem will not be solved. Instead, try to install all required Adafruit packages.
Connect the sensor to OctoPrint
In order to access your sensor data, you will need to install the Navbar Temp plugin (check Step 4 above). The other thing we will need is a python script that will print() the data you want to display to the console (print it only once; Navbar Temp has internal cycle that checks your script every few seconds to update the data). Once you have those you go to OctoPrint / Settings / Navbar Temperature plugin / custom command and add the command that you would use to run the python script for the sensor as if you are typing in the Raspberry terminal. In our case we want to run a python3 file called i2c_test-single.py located at /home/pi/io-client-python3/ (this is the same file we showed in the ‘Install software for HTS221’ section). The resultant data is shown in the red rectangle.
The only problem we had was that the custom command update time was so long that at the beginning we thought it doesn’t update at all. After reading the (free and open, you can download it from github) Navbar Temp source code, we found that the update time is 30 seconds. In order to fix that we had to manually edit the Navbar Temp init file. Here are the steps:
- connect to the Raspberry via ssh (either through Windows 10 Power shell or via Putty)
- locate the Navbar Temp folder typing the following:
sudo find -path ‘*navbartemp’
- with cd go to the navbartemp directory that you just found
- open the __init_.py file with nano and change the interval in on_after_startup function to 2 seconds
interval = 5.0 if self.debugMode else 2.0
- do the same in on_settings_save function
- Press Cnrt+X to exit and save with the same name
- Restart OctoPrint. Much better!
Bonus 2: Power ON / OFF switch
Another handy feature we added to our OctoPrint ‘command center’ was a button to turn on and off the main power supply for the printer. You still need to run the Raspberry all the time to support the OctoPrint server.
As this section deals with high power electronics, in case you have any doubts what you should do, please find a professional who can help you.
On the hardware side, you need to connect the relay to the Raspberry Pi as follows (you can choose different GPIOs as long as you change the number in the plugin):
VCC (relay) -> 3.3 V (Raspberry)
GND (relay) -> GND (Raspberry)
IN (relay) -> GPIO14 (Raspberry)
Then, disconnect the power cable from your 3D printer and the main power supply. You need to remove the outer most protective layer and find the live wire (usually red or brown) and cut it so that you can insert the relay in the circuit (if you cut all wires, just reconnect them). On the relay, use the common terminal (COM) and the normally open terminal (NO; with ‘normally open’ the relay will keep the circuit disconnected until you send a signal to the IN pin) – insert the wires and turn the screws. Make sure they are well connected and reconnect the power cable to the printer.
Next, open OctoPrint / Settings / GPIO control and click the blue plus icon on the right to create a new button. Choose an icon, label and the appropriate GPIO pin (the same that you connected the relay to; in our case GPIO14). Make the active state LOW – at least in our case the relay is ON when the GPIO is LOW (no voltage). Click save.
On the left side of the main screen you should see a GPIO control panel with your button. When you turn it on, you should hear a ‘click’ sound from the relay, even if the printer is not connected to the main power. If that works, plug in the power and see if the magic happens.
So that’s it for now. We hope this guide will help you optimize your workflow. Combined with the 3D printer enclosure, the printing process becomes more fun and less work.
See you in our next blogpost!
Adding the Raspberry Pi to the enclosure box calls for a nice box to put it in. You can find our custom design for the model 3B on GrabCad – https://grabcad.com/library/raspberry-3b-enclosure-1
We designed a mounting system for the camera as well – the time lapse videos are much nicer to watch if the print you are looking at doesn’t move. With Ender 3 this means that the camera must be attached to the print bed. The design you can see below has four different axis you can tweak. It is attached to the metal frame below the print bed (we had to drill a hole for that to work). Another option is to mount it on one of the bed leveling knobs. We ordered aluminum parts from the laser cutting shop as the 3D printed ones melt and bend when heated. 3D models and DXF drawings can be found here – https://grabcad.com/library/raspberry-camera-holder-for-3d-printer-1