OctoPi plugin to control relays


This entry is part 7 of 7 in the series RepRap
Hey. This page is more than 3 years old! The content here is probably outdated, so bear that in mind. If this post is part of a series, there may be a more recent post that supersedes this one.

I started developing a plugin for Octoprint (more specifically Octoprint-on-RPi aka Octopi). It allows you to control relays on a Pi-Plates RELAYplate hat. See here (with instructions) if you are interested in the plugin.

The plugin works by sub-classing Octroprint’s EventHandlerPlugin mixin to listen for and react to certain Octoprint events: PRINT_STARTED, PRINT_RESUMED, PRINT_CANCELLED & PRINT_DONE. Fans on for the first two & fans off for the second.

Why?

My old RepRap has a RAMPS1.4 control board. Unmodified this allows control of a fan intended for cooling the print object (Gcode M109) , but no other fans. I have a fan cooling my stepper drivers and one on the hot end.

There is a 12V output on the RAMPS1.4 for such fans but it is always on, and my fans are loud. To date, I have just powered fans directly off the power transformer, with a manual switch for control. I switch it on when printing, off when I want some quiet. Sometimes I forget to switch them on though. Tthis results in a blocked hot end and/or missed steps on overheating drivers.

I wanted fans to turn on automatically only when printing and cooling is needed.

I suppose one could use it to turn off power to non-RPi printer components. Or (in similar vein) an E-Stop.

Pi-Plates RELAYplate

Pi-Plates RELAYplate

I had this leftover from a previous project which went in another direction. It’s an RPi hat with 7 relays on it. It mounts as an SPI device. This essentially means it uses up no (? not 100% sure on that) RPi GPIO pins – there are similarities with USB I suppose in this respect, and that you can daisy chain a number SPI devices.

An alternative way to control servos would be via the GPIO pins, with one pin per servo. I have not implemented this. One could, and would probably be more useful to more people.

There is an official Pi-Plates library on pyPi, pip install pi-plates,

Octoprint runs in a Python virtual environment, and in this environment you’ll probably get the following error if you try and import RELAYplate from the official library:

AttributeError: 'module' object has no attribute 'getsitepackages'

This is caused by the following lines in pi-plates source.

import site
...
localPath=site.getsitepackages()[0]
helpPath=localPath+'/piplates/RELAYhelp.txt'

This issue is not specific to Pi-Plates, and I found more general explanation here. I get the impression it might work on some virtualenv Python versions, but not others (like the one my octopi is using).

I ended up just taking the RELAYplate.py code from the official Pi-Plates repo and copying it as a standalone file in my project with the following modifications:

try:
    localPath=site.getsitepackages()[0]
    helpPath=localPath+'/piplates/RELAYhelp.txt'
except AttributeError:
    print ("Help disabled in venv mode")

Series navigation

<< Octopi: If it ain’t broke…