dec 02, 2023
🔗 Auto-shutdown a 3D printer with Raspberry Pi and ESPHome
One thing that I am not a fan of, is the 3d printer's fan staying on for hours after the print is finished. I know that the power consumption is not that high, but I like the feeling of waking up in the morning and finding the printer turned off and the finished print waiting for me.
This apparently simple project also introduced me to a bunch of new topics, like ESPHome, NodeRED and MQTT. Everything started with watching this fantastic video: Save Energy by automating your 3D-printer by Andreas Spiess.
I already owned a cheap 3d printer, the OG workhorse Ender 3, and I had a spare Raspberry Pi 4B so I decided to give it a try.
In my previous post, A single enclosure for the Raspberry Pi and the Ender 3 electronics, I shown how I already moved the electronics to the back of the printer, so I had the perfect spot to place the Raspberry Pi.
# Components
- Raspberry Pi 4
- Ender 3
- 2-to-1 XT60 adaptor
- Sonoff Basic RF R2 v1.3
- USB to Serial adapter with FTDI FT232RL chip, like this one: DSD TECH SH-U09C
- Momentary switch button
# Install the project on Raspberry Pi
Use Raspberry Pi Imager to install on the microSD the Raspbian OS with preinstalled Octoprint
Create a new user and login by connecting with a computer in the same network to http://octopi.local
.
After that you will be able to ssh into the Raspberry Pi, if you kept the default settings you can just type:
ssh octoprint@octopi.local
Download the repo from Github via https
git clone https://github.com/danieledep/raspberry-remote-shutdown.git
Go into the downloaded folder
cd raspberry-remote-shutdown/
Make the script executable
chmod +x install.sh
Run the install script
sudo ./install.sh
# Flash the Sonoff with ESPHome
Install desktop environment to use Esphome and NodeRED on the Raspberry Pi
sudo /home/octoprint/scripts/install-desktop
Set Desktop GUI to boot at startup and set the resolution at least at 1280x720px
sudo raspi-config
Install Chromium
sudo apt-get install chromium-browser --yes
Attach a screen, keyboard and mouse to the Raspberry Pi
Install the D2XX Drivers for using the FTDI chips plugged into the Raspberry Pi
Go into the project folder:
cd raspberry-remote-shutdown/
⚠️ Last time I tryed installing Esphome with
pip
it failed to installcryptography
because of incompatible dependencies, this fixed it:sudo pip3 install esphome cryptography==3.3.2
Start up Esphome, use sudo to have write permission for secrets.yaml
sudo esphome dashboard /config
Open Esphome web ui at octopi.local:6052
Press on Secrets to save wifi configuration
Create a project name sonoff
and copy the content of sonoff.yaml
into the file
Hit Save and then Install.
The first time you flash a new firmware you'll need to connect the Raspberry PI to the Sonoff via the FTDI Module wiring the pin like the following diagram.
FTDI Sonoff
GND ----> GND
VCC ----> 3V3
TXD ----> RX
RXD ----> TX
Sonoff wiring for flashing
The first time you flash a new firmware you'll also need to keep pressed the physical button of the Sonoff.
⚠️ Newer Raspberry Pi with installed 64bit OS might need to disable temporarely the 64 bit capabilities at boot time, in order to compile the binaries due to compatibility issues with Platformio tooling. To do so edit the
config.txt
:sudo nano /boot/config.txt
add to the part titled
[pi4]
a new line like this[pi4]
arm_64bit=0
# Setup NodeRED and MQTT
Open NodeRED at octopi.local:1880
import our flow nodered-flow.json
and click Deploy
The NodeRED flow
Finally let's enable remote access to the MQTT broker without requiring authentication. Open the configuration file
sudo nano /etc/mosquitto/mosquitto.conf
At the end of the file add these two lines:
listener 1883
allow_anonymous true
Save the file and restart Mosquitto for the changes to take effect.
sudo systemctl restart mosquitto
# Configure Octoprint
Open Octoprint, install the MQTT plugin, tick Activate event messages, and let's only keep Printing events messages enabled.
Add octopi.local
as the Broker host.
⚠️ By default the MQTT plugin has the Enable retain flag option checked. This is normally the preferred choice but for our setup where the MQTT broker shut itself down multiple times from the same machine, we don't want to have our server turn on and find retained messages from the previous session.
# Notes
Since I'm running the MQTT Broker directly on the same Raspberry PI that runs Octoprint too, which effectively self-shutdown, I don't need to run the systemd service shutdown.service
.
The service was used by the original author to listen for the MQTT message coming from a different server running the MQTT Broker telling the Raspberry PI running Octoprint to shutdown.
But since they both run on the same device I can simply run the Exec node from within NodeRED, which upon receiving the octoprint/shutodwn
MQTT message, it launches the command sudo shutdown now
to the OS.
I still have an updated version of the necessary files and NodeRED nodes in this repository, in case I wanted to change my setup in the future.
# Testing MQTT
Check the systemd services are enabled and running
systemctl list-unit-files
From terminal send MQTT messages to the NodeRED client which sends them to the Sonoff.
To turn on:
mosquitto_pub -d -t sonoff/switch/mains/command -m "on"
To turn off:
mosquitto_pub -d -t sonoff/switch/mains/command -m "off"
To turn off after 30 seconds delay:
mosquitto_pub -d -t sonoff/mains -m "off"
# Troubleshooting
To read log messages from NodeRED in real-time
node-red-log
To read Mosquitto logs file
sudo cat /var/log/mosquitto/mosquitto.log
To read NodeRED logs file
sudo cat /var/log/syslog
# Resources
- ESPHome
- NodeRED
- Save Energy by automating your 3D-printer
- D2XX Drivers - FTDI
- Sonoff Basic RF R2 v1.3
- Sonoff – $5 WiFi Wireless Smart Switch Introduction
- How to Flash a Custom Firmware to Sonoff
- Reprogram Sonoff Smart Switch with Web Server
- Install Mosquitto MQTT Broker on Raspberry Pi
- Testing Mosquitto Broker and Client on Raspberry Pi
- Using ESPHome With Sonoff Basic
- GPIO Binary Sensor
- Buck converters: Quieten your 3D printer with a Noctua fan & directly power your Pi
- Make your own Fritzing parts