Steve,
my way of doing things has moved on quite a bit since I posted this. However, my first bit of advice is to only power down the 3D Printer, not both the Raspberry Pi & Printer together. The Pi will happily run forever & uses virtually no power, & it is far more useful when it is running. I just use my relays to cut the mains power to my 3D Printer PSU (2 relays, set to trigger simultaneously, one for the Live & the other for the Neutral). My Pi stays turned on, & I have some additional menu items in Octoprint so I can turn the printer on or off, and the Shutdown System field in my settings just points at my shitdown bash script. I'll give a quite detailed answer, as there's bound to be loads of people reading this who are only just starting to play around with relays, & who need friendly explanations.
I also had some interesting times playing with cheap relays bought online. I discovered that if you buy ones with two different sets of jumpers, it made life much easier.
Block of three jumpers
JD-VCC | VCC | GND
Block of three (or more) Interface Jumpers (depending on how many relays the board has)
GND | IN1 | VCC (if the board only has one relay)
or
GND | IN1 | IN2 | INx... | VCC (if the board has 2 or more relays)
The relay comes with a Jumper fitted, jumping JD-VCC to VCC on the Block of three jumpers. UNJUMP this to separate the power supply for the trigger circuits and the relay circuits. The relays are driven through the opto-isolators, so that the actual current required to trigger each relay is <2mA - which means that they are safe to connect to any GPIO pins on the Pi. We are connecting VCC on the trigger circuits to the +3V3 GPIO pin on the Pi, as this is enough to power the opto-isolators. Meanwhile, the relays will only work on +5V, so they are powered from the +5V GPIO pin on the Pi instead. This has worked flawlessly for a couple of years for me, so it's well tested.
Each relay has an associated LED indicator which shows its state:
OFF = relay coil unenergised
ON = relay coil energised
The layout for the connectors to each relay is a common terminal in the middle, with a normally open terminal and a normally closed terminal on either side, all with Optocoupler isolation. I have my mains cables wired in so everything defaults to disconnected, on safety grounds.
It has been said that GPIO 8 is very noisy on boot, which can flip the relays, so avoid using it. I havn't verified this as true, but I haven't disproved it either, so YMMV - but that's why I don't use GPIO 8.
Wiring
Block of three pins
JD-VCC Raspberry Pi +5V Provides 5v power to drive the relays
VCC Leave disconnected
GND Raspberry Pi 0V or GND Provides the ground connection
Block of Interface pins
GND Raspberry Pi 0V or GND Provides the ground connection
IN1 A suitable GPIO output pin Switches relay 1 when GPIO goes Low
INx A suitable GPIO output pin Switches relay 2, or 3, or 4 etc, when GPIO goes Low
VCC Raspberry Pi +3V3 Provides +3V3 power for the opto-isolators
################################
/usr/local/bin/printer_on.sh
#!/bin/bash
gpio export 17 out
gpio export 18 out
gpio -g write 17 0
gpio -g write 18 0
################################
/usr/local/bin/printer_off.sh
#!/bin/bash
gpio export 17 out
gpio export 18 out
gpio -g write 17 1
gpio -g write 18 1
################################
/home/pi/.octoprint/config.yaml
excerpt which adds my menu items.
I haven't used full paths because my scripts are in /usr/local/bin, & thus on the path, but YMMV.
Interestingly, GPIO commands don't require SUDO, which makes things simpler.
system:
actions:
- action: pon
command: printer_on.sh
name: PrinterOn
- action: poff
command: printer_off.sh
confirm: Are you sure you want to turn off the printer?
name: PrinterOff
(edited to fix my markdown)