Trick #6: Send SSH commands to your server (for ex. to command a relay)

With Printoid PRO and PREMIUM, you have an access to the commands panel and you can create your own SSH commands.

SSH commands are helpful if you want to read or execute something on your Raspberry Pi (independently to OctoPrint).

You can for example control a relay to switch ON/OFF your LED lightning, a power supply… but in a more general case you can send any command to your host machine (Linux or Windows).

PREREQUISITES

In this tutorial, we will consider that you want to switch ON / switch OFF a LED lightning system on your printer, powered with the help of a relay driven by a GPIO pin of your Raspberry Pi.

So you are using (for example) the GPIO pin number 8 to drive the relay.

You can create a bash script on your Raspberry Pi to do that, for example in the ~/scripts/ folder. In this example, create the file “super_light_on” to switch ON our LED lighting.

The content of ~/script/super_light_on is:

#!/bin/bash

gpio mode 8 out

gpio write 8 1

Then, execute the following command to make this script executable:

sudo chmod +x ~/script/super_light_on

That’s all. You can create a second bash script to switch OFF the LED lightning, called “super_light_off“:

#!/bin/bash

gpio mode 8 out

gpio write 8 0

And finally, make it executable too:

sudo chmod +x ~/script/super_light_off

Learn more about the GPIO of the Raspberry Pi: GPIO usage

Learn more about the bash scripts: What is a bash script?

CONFIGURE PRINTOID

Open the OctoPrint Server profiles panel in Printoid, select your profile and click on the “Extended settings” button to open them.

In order to be able to send SSH commands, Printoid needs to know:

  1. The SSH login (if you have installed OctoPi, or Raspbian, the login is pi by default)
  2. The SSH password (the password for pi, by default it is raspberry but I advise you to change this password)
  3. The port* (optional in LAN, the default port 22 will be used because it is the default SSH port – but this field requests a value in a distant access (WAN))

* Important: the port value is mandatory if you wanna use the SSH commands over the Internet. This value is not 22 in WAN, and it is not the same public port than the one you have opened to make OctoPrint reachable over the Internet (as indicated in the dedicated tutorial). This time you need to open another port in order to forward another public port (the value of your choice, for example your birthyear+1 is a good value) to the local port 22.

Please note that Printoid will not read or execute any command on your machine without your consent.

CREATE A COMMAND

Simply click on the “Commands” action (with the terminal icon).

The menu design has changed in Printoid 8, but that’s still the same way to open the commands panel.

This is the commands panel in Printoid.

On the right, it’s the list of your own commands. Scroll to the end and click on the “+” button to create a new command.

  1. Select ‘SSH’ in type.
  2. Give a name to this new command
  3. Enter the command to execute
  4. Choose an icon and a color
  5. Check the box if you wanna add this new command to the main panel (if the “joystick” mode is not enabled only)

Please note that, for the “command” field:

  • As you can see, I my own case, I execute a bash script on my Rapsberry (super_light_on). In that case, the most important thing to know is that you SHALL specify the FULL path to the script to be executed, otherwise Printoid will not be able to execute it (the app will not search the script by itself, unless you have exported its path to an environment variable).
  • In Linux, the “~” symbol in the paths replaces the directory of your home. By default on the Raspberry Pi, “~” is equal to “/home/pi/”
  • You can, if you want, execute directly a Linux command. For example, sudo shutdown -r now to reboot the system, or gpio -g mode … to perform an action on a GPIO, instead of calling a bash script.
  • For some SSH commands, you want to read the return value. You can check the “show command response”. Otherwise Printoid will only execute the command without feedback.

SAVE AND USE

If you have check the “show on home” box, you will get a new icon near the axis control arrow to execute your command.


You might also read the tutorial to send custom GCODE commands here.


8 thoughts on “Trick #6: Send SSH commands to your server (for ex. to command a relay)

  1. Simply desire to say your article is as surprising. The clarity in your publish is just excellent and that i can suppose you are a professional in this subject. Fine along with your permission allow me to grasp your feed to stay up to date with impending post. Thanks 1,000,000 and please continue the enjoyable work.

    Like

  2. How do the more security conscious amongst us enter an ssh key? None of my Pi’s allow password access.

    Like

  3. Use this code to have it toggle a relay with one button!

    #!/bin/bash
    gpio mode 1 out

    if [ $(gpio read 1) == 1 ]
    then
    gpio write 1 0
    else
    gpio write 1 1
    fi

    Like

Leave a comment