There are several methods how to switch your printer on and off with your Raspberry Pi. I picked out 3 of them.

  • 433MHz transmitter connected to one GPIO pin to control wireless power sockets
  • Relay modul connected to GPIO pins, for each relay one GPIO pin, selfmade main voltage wiring
  • TCP/IP based power switch sockets

 A simple solution is to use a 433Mhz transmitter to control these cheap wireless power sockets. You don't have to handle around with main voltage wires and other dangerous things. You can just connect the small module to your RPi and download a ready to build tool which does all the work for you. But in this situation it can be possible that your neighbour or some script-kiddies switch on or off your printer and may cause damage to your printer, your flat/house or your life.

 This will not happen if you use a relay module which is directly connected to your RPi. But therefore you have to do all of the connections for the main voltage by yourself and that should only be done by people who know what they are doing! You will risk your life and also your insurance coverage!

 The savest way will be to use a TCP/IP based wireless power socket. You don't have to do any wiring and don't have to be afraid of neighbours or script-kiddies. But I don't own one of these so you will find no more information about these kind of sockets in this article.

Controlling a relay module with Raspberry Pi

If you want to be able to switch your printers on and off with such a module, you need the following things:

  • 2, 4 or 8 channel relay module (I use an 8ch relay module)
  • Additional power supply for the relay module
  • A darlington array (e.g. ULN 2803A) on a pcb
  • Sockets for switch cabinet (amount depends on amount of channels)
  • Top-hat rail 35mm
  • Additional 13A main fuse for top-hat rail
  • Some flexible >=1.5mm² wires in green/yellow, blue, brown and/or black
  • Electrical engineering knowledge!
  • Also a case for the darlington array pcb

To control a common 1-2-4 or 8-channel relay module, the GPIO pins of the RPi can be used. These pins can work as inputs or outputs, and can have a high or low state. I have to control 6 switch cabinet sockets so I have to use a 8-channel relay module.

Working with the GPIO pins

The easiest way to work with the GPIO pins is to use the tool called WiringPi. OctoPi has it preinstalled.
First set the mode of a pin of your choice lets say pin 17 to an output,

gpio -g mode 17 out

and then set the state of the pin to high.

gpio -g write 17 1

To set the state to low the "one" in the command is changed to a "zero".

gpio -g write 17 0

We don't need to set a pin to an input, but just in case you want to do it, that's how it is done.

gpio -g mode 17 in

If you want to use the WiringPi library inside some code like c, c++ or python, have a look at this site http://raspberrypiguide.de/howtos/raspberry-pi-gpio-how-to/ for some code examples.

 

Connecting the relay module to your Raspberry Pi

If you securely want to connect the 8-channel relay module to the RPi a darlington-array must be used. Therefore you can find some connection schemes here: https://www.raspberrypi.org/forums/viewtopic.php?t=84337&p=597134

If you not connect a darlington(-array) between your RPi and your relay module, the relay will be switched if the corresponding GPIO pin is set to low. During bootup it is possible that your relays get switched on. Also you have to take care about the maximum current which can be sucked out or pushed into a GPIO pin. In case of the relay modul, the GPIO pin is used as a drain. If you want to use the relay modul without a darlington(-array) have a look at this page: https://github.com/foosel/OctoPrint/wiki/Controlling-a-relay-board-from-your-RPi

Connecting the relay module to the sockets

I will not show any informations about how to make the wiring for this. The wiring should only be done by people with enough electrical engineering knowledge.

You will risk your life and your insurance coverage!

433Mhz transmitter to switch on and off wireless power sockets

If you want to use a wireless 433MHz power socket switched on by a 433MHz transmitter connected to your RPi, only a few steps must to be done.

  • Get a wireless power socket for 433Mhz
  • Get a cheap 433Mhz transmitter module
  • Download, build and use raspberry-remote

All common wireless power sockets for 433Mhz will work, so you don't have to worry about that. You can set the SystemCode and the UnitCode for each socket. The cheap 433Mhz transmitters can also be found everywhere on the internet. The connection is quite simple:

  • Transmittor VCC => RPi 5v
  • Transmitter GND => RPi GND
  • Transmitter DATA => RPi GPIO pin 17 (default)

To get better transmission add a small antenna (17,3cm long wire, staight or as a coil) or a coil loaded antenna onto the module.

Then download and build the software.

cd /home/pi/
git clone git://github.com/xkonni/raspberry-remote.git
cd raspberry-remote
make send

The use of this tool to switch on a socket is also very easy.

/home/pi/raspberry-remote/send 10101 2 1

The first parameter "10101" is the SystemCode, the second "2" is the UnitCode (socket B) and the third "1" is the state (1=on and 0=off) in this case on. If you want to use another pin instead of GPIO pin 17 then you have to use the -p parameter. For futher informations use this command.

/home/pi/raspberry-remote/send -h

If you use OctoPrint you are able to run your command directly from the OctoPrint webinterface. Therefore you can edit a file manualy or you can use the OctoPrint plugin System Command Editor.

For some german informations about how to use a 433Mhz transmitter with your RPi and how to run the command directly from the OctoPrint webinterface, you can have a look at this website: https://blog.seidel-philipp.de/anleitung-3d-drucker-mit-octoprint-einschalten-und-ausschalten/

Alternative controlling of the GPIO pins with GpioControl

I wrote a simple programm called GpioControl for the controlling of the GPIO pins on my RPi 3. It uses the WiringPi library. To download and build the tool you can use the following commands.

cd /home/pi/
mkdir GpioControl
cd GpioControl
wget "http://thomas-messmer.com/GpioControl.cpp"
g++ GpioControl.cpp -o GpioControl -lwiringPi -std=c++11

When it's build you can get some information about the usage with the -h parameter.

./GpioControl -h

It is also possible to set a cron job to set the different GPIO pins to low or high. You can set the cronjob with a similar scheme like if you use cron direct

./GpioControl -c "minute hour day-of-month month day-of-week" pin-number pin-state

and you can set a cronjob in lets say 10 hours from now.

./GpioControl -n hours-from-now pin-number pin-state

This is usefull if you know that your printer is running for the next 8 hours so you can set a cronjob for switching off your printer in lets say 10 hours. Because cron didn't have a year attribute, these cronjobs will be executed every year!

Automatic shutdown after print with OctoPrint

If you like that OctoPrint shuts down the printer after the print has finished, you can use the events from OctoPrint. What should be done if one of these events triggers, can be configured in the config.yaml file.

In my case I had to add the following lines to the config.yaml file.

events:
  enabled: true
  subscriptions:
  - command: /home/pi/.octoprint/uploads/GpioControl -p 6 0
    event: PrintDone
    type: system

Be aware, that you have the correct amount of spaces in front of the different entities. To shutdown the printer I use the programm GpioControl which is currently in my upload folder. You can run whatever programm you like or send e-mails and more.

Modify your GCode-End-Scripts

It is realy important, that you add the M400 gcode at the end of your end-script. This leads to, that the printer waits for the moves to finish. When these reach the final position the print-job is done. This is important if you want to move your build table down after a print-job is done. If you don't have the M400 command set, the last move commands are send to the printer, but the printer signals that the job is done before the movement reach its final position. In case of my Core-printers, the build table moves down, and before it reach the lower end position, the printer is switched off and the build table falls to the bottom. This can decalibrate or break the printer, so its important that the final moves have finished before shutting down the printer. So remember, M400 is your friend in this case.