Controlling a Relay Board From Octoprint on a Raspberry Pi

by Kev_MTL in Circuits > Raspberry Pi

12532 Views, 17 Favorites, 0 Comments

Controlling a Relay Board From Octoprint on a Raspberry Pi

all.png

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.

Supplies

  • 5V relay board with optocoupler (ebay)
  • Some jumper wires
  • An electric box with outlets (optional)

Connecting the Relay Board to Your Raspberry Pi

rasp and relay with wires.png

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.

SSH to Your Raspberry Pi and Configure It

commands putty 1.png

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!

Add New Menu Options to Octoprint to Control Your Relays

octoprint adding menu options.png
putty edit config file.png

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 :

sudo reboot

You should now be able to control the relays from the Octoprint web interface!

High Voltage Wiring (be Careful)

relay with outlet wiring.png

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.


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 :
https://www.raspberrypi.org/forums/viewtopic.php?t=35321

But most importantly, this script sets the GPIOs as outputs, otherwise the menu items won't work in Octoprint.


Create a script using nano :

nano /home/pi/setupgpio.sh

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.