USB Power Control via OctoPrint

I would like to be able to toggle the USB port(s) power On & Off via OctoPrint if possible.

I have a 4 relay control already wired and set up to control the printer's power, however, the Printers LCD screen stays on due to the USB connection powering it up all the time. Also, have some compact mUSB LED lighting I would also like to control via power on USB ports, instead of having to try and cut into the USB cables to use the control relays.

I do know it's possible to do outside of Octoprint, but would like to keep it simple.

My set-up is:
Pi 3 B+
4 Control Relay box (controls printer PSU)
Latest OctoPrint w/ PSU Control Plug-In
Wanhao i3 Plus 3D Printer (24v system)
ADVi3++ FW (not factory Marlin)

Adafruit sells a male-female USB cable with an inline switch. You might be able to just unscrew the back on that switch and wire the relay output to that for a clean-looking approach.

If you enjoy soldering tiny connectors, Adafruit's DIY USB connectors would probably be the way to go otherwise.

Really? You can control USB power on a Pi?

Thanks for the switch idea ... ya was going for a sleek clean look, but this might look clean too and not a hack job

Haven't tried it yet ... so I can't confirm myself it actually works ...

https://www.raspberrypi.org/forums/viewtopic.php?t=162539

https://www.raspberrypi.org/forums/viewtopic.php?t=196827

That's really cool.

Next step is to figure out how to get octoprint to gracefully reconnect to a printer that has been powered up + reconnected after it was powered down.

1 Like

I guess I missed a word that I thought was implied- "programmatically".

I'd love to do "power on to GPIO pin 3, USB on, now connect serial".

[edit] oh, there is a serial connection API: http://docs.octoprint.org/en/master/api/connection.html#post--api-connection

1 Like

It's how I control the SB Motor Shield that turns on and off my printer lights

Upon reflection, I should have used an Arduino. It's much more popular, and there's much more documentation available. As it is, I had to turn them on one at a time until I got it to work

So if you're looking to programmatically turn off USB power then uhubctl might be useful.

Assuming USB port 2:

uhubctl -a off -p 2
2 Likes

Any update on this? I’d love to find a way to do this without yet another relay.

You could use my last posted suggestion as combined with a GCode Systems Command that you'd create a pair of new commands like OCTO912 and OCTO902. The first would turn ON USB port 2 (per the example) and the second would turn it OFF.

Next, you could edit the config.yaml for the Controls section and add a pair of buttons which point to those new OCTO commands. After a restart, OctoPrint's Control tab would have two buttons for toggling ON/OFF USB port 2 in this case.

1 Like

Hey guys. I turn on an off my printer from GUI, using a relay to turn on and off the power supply, and a command line to turn off USB port. It was just simple to do.

I´ve installed a package called hub-ctrl. It controls USB power.

Step-by step was like this:
1- Downloaded the package here: https://github.com/codazoda/hub-ctrl.c
2- Copied to /tmp using WinSCP
3- Elevated privileges with sudo to get root permissions
4- Compiled the package with the following command (in the tmp folder): gcc -o hub-ctrl hub-ctrl.c -lusb
It created a file called hub-ctrl. I,ve copied that file to /usr/bin: mv hub-ctrl /usr/bin/
5- Created my scripts to power on and off the printer at /usr/local/bin/scripts/

Script to start the USB power, wait 5 seconds then activate the relay - printer_on.sh

#!/bin/bash
sudo hub-ctrl -h 0 -P 2 -p 1
sleep 5
gpio export 4 out
gpio -g write 4 1

Script to power off - printer_off.sh

#!/bin/bash
gpio export 4 out
gpio -g write 4 0
sudo hub-ctrl -h 0 -P 2 -p 0

Added to /home/pi/.octoprint/config.yaml the following lines, after server section:

system:
actions:

  • action: pon
    command: /usr/local/bin/scripts/printer_on.sh
    name: Turn Printer On
  • action: poff
    command: /usr/local/bin/scripts/printer_off.sh
    confirm: Are you sure you want to turn off the printer?
    name: Turn Printer Off
  • action: lon
    command: /usr/local/bin/scripts/light_on.sh
    name: Turn Light On
  • action: loff
    command: /usr/local/bin/scripts/light_off.sh
    name: Turn Light Off
4 Likes

I think technically, you only have to initialize a GPIO port once as either input/output and then you don't have to keep doing that. But it doesn't hurt to keep it in your script like this, though.

Hi guys, just registered in order to add to the discussion, hoping this isn't too old a thread to add to.

The easiest fix I found for this behaviour is simply to disconnect the red wire inside the USB cable! Easy as pie. I quite often do this with USB devices to prevent parasitic draw from the RPi.

Hope that helps

1 Like

Others use a small strip of tape.

I will note that the 5V in the USB is used by drivers to know when the serial cable is connected/disconnected. Or at least, this is the specification for USB.

I wasn't keen on the potential for glue from the tape fouling the port.
Interesting that you mention the driver side of things, I've not run into that issue yet and it's something that I've implemented for a number of years; Occasionally cables have been rewired to use the 5V from a discreet SMPS instead. Would that have overcome it?

You probably just leave the serial cable in at all times (like most of us).

You could wire the 5V as described to an ATX sort of power supply or switching power supply. And yet, the 5V on an ATX is always there (with a limited current) so that pretends like it's always on. As for the switching power supply, I'll note that my own wouldn't stay on if the load was too small. (It's fond of cycling ON/OFF with a too-small load.)

I’m trying to implement this with raspberry pi 3b. All works from SSH on command line, but I can’t figure out how to get Octoprint to execute the script. Trying to use system command editor. Have tried too many things to list here. When entering command as stated above I get no tty error. If I add an & like discussed in another thread, I get command executed successfully but nothing happens. Feel like this is a simple issue but I’m so new to Linux I can’t seem to work it out. A working example in system command editor plugin may help, any insight is appreciated. Thanks in advance!