There are several methods of 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 module connected to GPIO pins, for each relay one GPIO pin, self-made 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 neighbor 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 safest 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 neighbors 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 the 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 an 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 do not connect a Darlington 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 of the maximum current which can be sucked out or pushed into a GPIO pin. In case of the relay module, the GPIO pin is used as a drain. If you want to use the relay module without a Darlington 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 information 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 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:

  • Transmitter 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, straight 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" correspond 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 further information use this command.

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

If using OctoPrint you can run your command directly from the OctoPrint web interface. Therefore you can edit OctoPrints configuration file manually or you can use the OctoPrint plugin System Command Editor.

For some german information about how to use a 433Mhz transmitter with your RPi and how to run the command directly from the OctoPrint web interface, you can have a look at this website https://blog.seidel-philipp.de/anleitung-3d-drucker-mit-octoprint-einschalten-und-ausschalten/ or scroll down a bit and have a look at "Switching over OctoPrints web-interface".

Alternative controlling of the GPIO pins with GpioControl

I wrote a simple program 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/images/downloads/GpioControl.cpp"
g++ GpioControl.cpp -o GpioControl -lwiringPi -std=c++11

When it's built 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 as if you use cron direct.

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

You can also set a cronjob in let's say 10 hours from now.

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

This is useful 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 let's say 10 hours. Because cron didn't have a year attribute, these cronjobs will be executed every year!

Switching over OctoPrints web-interface

If you want to switch your printer on and off using the OctoPrint web interface, you can add new actions into your OctoPrint configuration file.

nano /home/pi/.octoprint/config.yaml

Nearly every command can be executed in an OctoPrint action also sending an e-mail and stuff like that. It is also possible to execute more the one command per action, therefore separate the commands with an ";".

The following example shows how to add new actions and a dividing line between the actions.

system:
  actions:
  - action: set port 1 to high
    command:  gpio mode 25 out; gpio write 25 1
    confirm: Sure?
    name: MKC On
  - action: set port 1 to low
    command: /home/pi/GpioControl/GpioControl -p 1 0
    confirm: Sure?
    name: MKC Off
  - action: divider
  - action: Light 1 (433MHz) ON
    command: /home/pi/raspberry-remote/send 10101 2 1
    confirm: ''
    name: Light 1 ON

Automatic shutdown after print with OctoPrint

If you like that OctoPrint switches the printer off 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.

nano /home/pi/.octoprint/config.yaml

In my case, I like to switch my printer off after the print is done. So I had to add the following lines to the configuration file.

events:
  enabled: true
  subscriptions:
  - command: /home/pi/GpioControl/GpioControl -p 1 0
    event: PrintDone
    type: system

 

Be aware, that you have the correct amount of spaces in front of the different entities in the config.yaml file!

Modify your GCode-End-Scripts

It is really 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 sent to the printer, but the printer signals that the job is done before the movement reaches its final position. In the case of my core printers, the build table moves down, and before it reaches 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 it's important that the final moves have finished before shutting down the printer. So remember, M400 is your friend in this case.

Comments powered by CComment