Arksine / moonraker

Web API Server for Klipper
https://moonraker.readthedocs.io
GNU General Public License v3.0
1.06k stars 407 forks source link

Power down USB port when printer not in use #667

Open hughesjs opened 1 year ago

hughesjs commented 1 year ago

Is your feature request related to a problem? Please describe

Assuming we're using an RPi with MainSail OS, a lot of printers will draw 5V power from the USB port and remain on even though there's not enough power to power the stepper drivers.

This is kind of annoying.

Describe the solution you'd like

You can power down individual USB ports on most hardware using uhubctl.

It would be good to have this option from within the MainSail UI.

(Issue over on MainSail: https://github.com/mainsail-crew/mainsail/issues/1397)

Describe alternatives you've considered

You could modify your USB cable or board to disconnect the 5V line, but this is a pain and destructive.

Additional information

No response

SureshotM6 commented 5 months ago

I was considering implementing this or perhaps a more generic method that allowed running arbitrary shell commands on power on/off (such as uhubctl). However, I realized that there is already a relatively simple way of making this work today:

  1. Create the /etc/systemd/system/klipper.service.d/50-usb-power.conf file (and its parent directory) and add the following contents to it, customized for your required uhubctl commands:
    [Service]
    ExecStartPre=!/usr/sbin/uhubctl -l 1-1 -a on
    ExecStopPost=!/usr/sbin/uhubctl -l 1-1 -a off
  2. Run systemctl daemon-reload so that systemd loads the new configuration.

This will cause the USB port(s) to be powered off when klipper is stopped and powered on before klipper is restarted. Moonraker and Mainsail do not even need to know about this.

If you are using this in tandem with a smart plug (as I am), ensure that the power configuration block in moonraker.conf has bound_services: klipper listed so that klipper.service is automatically started/stopped with power on/off. This even works at system boot since klipper.service will start, Moonraker will notice that the smart plug is off, and moonraker will stop klipper.service triggering the USB port to power off.

If you are not using a smart plug or power block in moonraker.conf, this still works by being able to click the power button in Mainsail and then stopping klipper.