Controlling a Relay Board From Octoprint on a Raspberry Pi

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.

Supplies:

  • 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 :

sudo reboot

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 :
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.

Be the First to Share

    Recommendations

    • Battery Powered Contest

      Battery Powered Contest
    • Plywood Challenge

      Plywood Challenge
    • Plastic Contest

      Plastic Contest

    7 Discussions

    0
    dpcons
    dpcons

    2 months ago on Step 5

    Thanks for sharing this.

    0
    bobs409
    bobs409

    Question 4 months 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!!!

    0
    Kev_MTL
    Kev_MTL

    Answer 4 months 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.

    0
    bobs409
    bobs409

    Reply 4 months 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

    1
    bobs409
    bobs409

    Reply 4 months 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. ;)


    Bob

    0
    vuorijoonas
    vuorijoonas

    Question 4 months ago on Step 3

    why can't i get octoprint after doing that command? i have to install octoprint again.



    0
    Kev_MTL
    Kev_MTL

    Answer 4 months ago

    Try to restore the config file only, no need to reinstall, you probably have a typo somewhere. Never had any issues with that.