julianschill / klipper-led_effect

LED effects plugin for klipper
GNU General Public License v3.0
696 stars 116 forks source link

Turn off LEDs when system is shutdown #135

Closed thijstriemstra closed 1 year ago

thijstriemstra commented 1 year ago

When I shutdown the host RPI that runs klipper, the led strip stops it's effect and stays in that state.

I would like to turn off the led strip when the system is shutdown, is this possible?

julianschill commented 1 year ago

No, not possible. The LEDs will keep their state until they receive another command. Run STOP_LED_EFFECTS before shutting down.

thijstriemstra commented 1 year ago

Thanks for the feedback. Perhaps I can disable/instruct the RGB pin on my Octopus v1.1 board right before systemctl does it's shutdown? I'll do some more research. I really don't want to have to change hardware and install a relay that cuts the power to the LEDs on shutdown or something like that.

julianschill commented 1 year ago

You would have to send a dataframe to instruct each LED to black. Just switching off the data pin is not enough. You could connect the GND of the LEDs to a heater out and switch the power.

jangrewe commented 1 year ago

You could create a systemd unit that does nothing on start, and executes g-code when stopped - which will happen when the system gets shut down.

https://unix.stackexchange.com/a/41756

The "easiest" method would probably talking to the API via curl, and asking it to execute some g-code: https://www.klipper3d.org/API_Server.html#gcodescript

Just make sure that your systemd service is stopped before klipper/moonraker is stopped.

thijstriemstra commented 1 year ago

Great suggestion @jangrewe. I tested the following:

Install:

sudo apt install jq curl

Create /home/pi/stop-leds.sh:

#!/bin/bash

curl --fail --silent --request POST --data '{"jsonrpc": "2.0","method": "printer.gcode.script","id": 7466,"params": {"script": "STOP_LED_EFFECTS"}}' localhost:7125/printer/gcode/script?script=STOP_LED_EFFECTS | jq -r '.result'

Make it executable:

chmod 775 /home/pi/stop-leds.sh

Running the script outputs: ok and LEDs are turned off.

When I shutdown or restart Klipper the LEDs are left in last state. Adding the following ExecStop call to the [Service] section of /etc/systemd/system/klipper.service correctly stops the LEDs before Klipper shuts down or reboots:

ExecStop=/home/pi/stop-leds.sh

Unfortunately this doesn't get executed when shutting down the system 😢 Nevertheless this is still an improvement, having the leds turned off when klipper is not running.

Just make sure that your systemd service is stopped before klipper/moonraker is stopped.

Any suggestions for disabling them when the system is shutdown are welcome..

thijstriemstra commented 1 year ago

Any suggestions for disabling them when the system is shutdown are welcome..

Figured this one out as well for shutdown. Create /etc/systemd/system/stop-leds.service:

[Unit]
Description=Turn off Klipper LEDs on shutdown
After=klipper.service syslog.service network.target

[Service]
Type=oneshot
RemainAfterExit=true
ExecStop=/home/pi/stop-leds.sh

[Install]
WantedBy=multi-user.target

Enable and start the service:

sudo systemctl enable stop-leds.service
sudo systemctl start stop-leds.service

Reboot the system and it should work as expected when doing a system shutdown. It's not working for reboot though, so again, suggestions for that are welcome.

thijstriemstra commented 1 year ago

Reboot the system and it should work as expected when doing a system shutdown. It's not working for reboot though, so again, suggestions for that are welcome.

Looks like the script does run on both shutdown and reboot, but only when using the host shutdown/reboot buttons from the fluidd or mainsail UI.

Running sudo shutdown -h now or sudo reboot from the terminal doesn't run the script because it's not using systemd, whereas the web interfaces do use systemd, hence the scripts are run there. Since rebooting/shutdown from the terminal an edge case is for me, I'm happy with the current solution.