Introduction: Controlling a Relay Board From Octoprint on a Raspberry Pi
So you have a raspberry pi with Octoprint and even have a camera setup. Last thing you need is a way to power on and off your 3d printer and maybe control a light. This instructable is for you!
This is inspired and simplified from : https://github.com/foosel/OctoPrint/wiki/Controlling-a-relay-board-from-your-RPi
Make sure to check my previous instructable where I built an enclosure box / fume hood for my 3d printer, as this is the sequel.
Tested with :
Linux octopi 4.14.79-v7+ #1159 SMP Sun Nov 4 17:50:20 GMT 2018 armv7l GNU/Linux OctoPrint version : 1.3.11
OctoPi version : 0.16.0
Disclaimer : I don't take any responsibility for whatever bad could happen by you following these instructions.
- 5V relay board with optocoupler (ebay)
- Some jumper wires
- An electric box with outlets (optional)
Step 1: Connecting the Relay Board to Your Raspberry Pi
Although relay boards are made for 5V logic, they will trigger correctly at 3.3V. Knowing this, I was able to avoid any modification to the original board.
Relay shield Raspberry ------------ --------- Coils: JD-VCC - 5V VCC - Not connected GND - GND Logic: GND - Not connected IN1 - GPIO #23 IN2 - GPIO #18 VCC - 3.3V
It is important to remove the jumper between JD-VCC and VCC if you have one. This will allow feeding 5V for the coils while feeding 3.3V for the logic inputs. And since both GND are wired together internally, we only connect one of them.
Step 2: SSH to Your Raspberry Pi and Configure It
Using putty or your favorite ssh client, connect to your raspberry pi using the same ip address that you use to access Octoprint. The default username is pi and password is raspberry.
First thing I would do is verify that the relays respond correctly. In order to do this, issue these commands
gpio -g mode 18 out gpio -g mode 23 out gpio -g write 18 0 gpio -g write 23 0 gpio -g write 18 1 gpio -g write 23 1
The first 2 lines allow to set the GPIO as an output. Then you toggle the outputs on and off. That should make the relay click. When the inputs are low (0), the relay should switch on and when the inputs are high (1) they should switch off. So yeah, this is a bit counterintuitive, but that's how it is!
Step 3: Add New Menu Options to Octoprint to Control Your Relays
You will need to edit the file config.yaml :
sudo nano .octoprint/config.yaml
Inside that file, locate the section "system" and add the following :
system: actions: - action: printer on command: gpio -g write 18 0 name: Turn on the printer - action: printer off command: gpio -g write 18 1 confirm: You are about to turn off the printer. name: Turn off the printer - action: lon command: gpio -g write 23 0 name: LightOn - action: loff command: gpio -g write 23 1 name: LightOff
After saving the file (ctrl+x), reboot the raspberry pi :
You should now be able to control the relays from the Octoprint web interface!
Step 4: High Voltage Wiring (be Careful)
A relay act as a switch, only it is triggered by a controller, in this case a raspberry pi. The switch consists of an electromagnet that will connect pins together, that is the click you hear.
For wiring it you have two choices. Either you cut one of the wires of your 3d printer power supply and you connect the ends to the relay, or you make a power outlet for that. I prefer the second way, as this will allow moving the printer more easily. I will also use the second outlet to connect my exhaust fan (see my other instructable : Easy and Cheap 3D Printer Fume Hood).
Now the other relay is to control a light. It is the same principle, but in this case I recommend that you just cut one of the wires from the power cable and connect the ends to the relay, most likely it won't need to be moved.
Step 5: Configure GPIO Outputs and Prevent Your Relays From Turning on When the Raspberry Pi Reboots
To summarize the issue, GPIO are set low when raspbian reboots (at least for certain kernel versions). Apparently, there is not much we can do... oh well! So the idea is to call a script that will revert that once it finishes booting by setting the outputs high.
See this forum thread for more info :
But most importantly, this script sets the GPIOs as outputs, otherwise the menu items won't work in Octoprint.
Paste in this code and save the file.
#!/bin/sh echo 18 > /sys/class/gpio/export echo 23 > /sys/class/gpio/export udevadm settle echo high > /sys/class/gpio/gpio18/direction echo high > /sys/class/gpio/gpio23/direction
Make the file executable :
chmod +x /home/pi/setupgpio.sh
Edit the rc.local file :
sudo nano /etc/rc.local
And call the script you just created by adding these lines :
# setup the gpio pins for gate control /home/pi/setupgpio.sh
Reboot your raspberry and check that it is working properly.
Question 1 year ago
hi , i am trying to edit this fie config.yaml , but i cant find section system. please can you help me ?
2 years ago on Step 5
Thanks for sharing this.
Question 2 years ago on Step 5
Followed the instruction carefully and have the options showing in my system area. For a test, I am using LED's in place of the relays but they are not lighting up when I turn these on. In system is says it executed properly. ??? Can anyone help? Pulling my hair out!!!
Answer 2 years ago
Could it be the polarity of the LED? Also, it's better if you put a resistor with the LED, they offer almost no resistance... After that, I would troubleshoot directly in command line. Try to issue these commands (over ssh) and see if it works :
gpio -g write 18 0
gpio -g write 18 1
the you move to the graphical interface.
Reply 2 years ago
Thank you for your reply. I am certain the LED is correct and has the resistor, it lights by putting power to it.
I tried doing the command over ssh and it doesn't light the LED.
I'm a newbie so lots of learning ahead! lol
Reply 2 years ago
Update: I finally got the LED's to light up using SSH. All I did was to run (sudo apt-get install wiringpi) and then it worked. It said it was already installed and running the latest version so I'm not sure what the problem was. ??
Just wanted to thank you again for replying AND for this great source of info.
I guess I can stop hitting my head against the wall now. ;)
Question 2 years ago on Step 3
Answer 2 years ago
Try to restore the config file only, no need to reinstall, you probably have a typo somewhere. Never had any issues with that.