Python script - Volumio Process

Hello everyone, let me start by saying that I have no knowledge in programming.
I wrote a simple script in Python to control WS2812B RGB LEDs on Raspberry-Pi and connected respectively to its GPIOs. This script will be made executable and started at Volumio startup by modifying the rc.local file. Its function is to flashing LEDs to X color for the first X seconds, then they must remain fix lit with another X color. I installed the all necessary libraries and everything works perfectly, but the main problem is that, when Volumio is turned off or restarted, the LEDs remain lit because they do not receive any off command from my script.
I would like to add a function that is able to interact with the Volumio processes and that when it is in the shutdown or restart phase, sends the off command to the LEDs.
Since there are people with very high skills in this group, I kindly ask you if you can help me complete this simple script in Python, it will be much appreciated!

Below the script, which obviously I didn’t write, but ChatGPT did :blush:

import board
import neopixel
import time
import signal
import sys

# Dichiarazione dei parametri
NUM_LEDS = 8
PIN_GPIO = board.D18
COLOR_RED = (255, 0, 0)
COLOR_ORANGE = (255, 165, 0)
BLINK_DURATION = 5  # secondi

# Inizializzazione del Neopixel
pixels = neopixel.NeoPixel(PIN_GPIO, NUM_LEDS, auto_write=False)

def turn_off_leds(signal, frame):
    pixels.fill((0, 0, 0))
    pixels.show()
    sys.exit(0)

# Imposta l'interrupt per spegnere i LED alla pressione di CTRL+C
signal.signal(signal.SIGINT, turn_off_leds)

# Lampeggia i LED di rosso per 5 secondi
for _ in range(BLINK_DURATION * 2):  # Moltiplica per 2 per ottenere il numero di cicli
    for i in range(NUM_LEDS):
        pixels[i] = COLOR_RED
    pixels.show()
    time.sleep(0.5)
    pixels.fill((0, 0, 0))
    pixels.show()
    time.sleep(0.5)

# Imposta i LED fissi di colore arancio
pixels.fill(COLOR_ORANGE)
pixels.show()

# Attendi la pressione di CTRL+S per spegnere i LED
try:
    signal.pause()
except KeyboardInterrupt:
    turn_off_leds(None, None)

If you’re running a rPi4 or less, you never actually turn off a unit, neither does the GPIO. So if you set the pin high, it will remain high.
You need to create a 2nd script, to turn the led off. (use ChatGPT :wink: )
When that script is ready you need a service that triggers the script on a Software shutdown, like:

sudo nano /lib/systemd/system/shutdown.service

[Unit]
Description=Deactivate LED before shutdown
Before=shutdown.target
Conflicts=shutdown.target

[Service]
Type=oneshot
ExecStop=/usr/bin/python3 /home/volumio/scripts/ledoff.py
RemainAfterExit=true

[Install]
WantedBy=multi-user.target

sudo chmod 644 /lib/systemd/system/shutdown.service && sudo systemctl daemon-reload && sudo systemctl enable shutdown.service && sudo systemctl start shutdown.service

And while you’re at it don’t use rc.local to start things, but use a service.

Hey Wow! Thanks for your quick response! With just one answer you have solved two problems or maybe more! I will try your solution soon and let you know. :smiley:
Since you’ve been so kind, I’d like to ask you another question, this one is a bit articulated. I would like to associate the flashing LED function with the complete startup of Volumio, i.e. it tells me when Volumio has completed its startup process and it is possible to use it. For example, when I turn on Volumio, I understand that I can start using it when it plays the welcome sound. Ok I could just time it, but obviously that wouldn’t be realistic because in case of boot problems it would give an invalid visual indication. Suggestions? :exploding_head:

that one is almost impossible to accomplish.
Services start as soonest after 11 - 12 seconds and you need to monitor until the latest completes.
Another way is breaking into vanilla code, which I will not going to explain.
You can try to see if you can do some monitoring on services. To see the first and last service that stars and completes:

systemd-analyze plot > ~/SystemdAnalyzePlot.svg

Sorry for the delay but I’ve been very busy. I followed your instructions and the shutdown feature works great! :smiley:
Since you advised me to use services, in the same way, I tried to create a startup service to start the ledon.py script at startup, but obviously it doesn’t work.
Forgive my ignorance, what did I do wrong? :worried:
I certainly see that the startup target does not exist so this is the first problem, but I think there is also something else…

sudo nano /lib/systemd/system/startup.service
[Unit]
Description=Activate LED on startup
Before=startup.target
Conflicts=startup.target

[Service]
Type=oneshot
ExecStop=/usr/bin/python3 /home/volumio/scripts/ledon.py
RemainAfterExit=true

[Install]
WantedBy=multi-user.target
sudo chmod 644 /lib/systemd/system/startup.service && sudo systemctl daemon-reload && sudo systemctl enable startup.service && sudo systemctl start startup.service

Read your service block and compare it to what you said: " I tried to create a startup service"
Hint: On the moment the system goes down, you call a script that needs to indicate the system is on.
ExecStop=/usr/bin/python3 /home/volumio/scripts/ledon.py

Not giving the answer straight, I’m in a teaching mode, so you have an understanding what it does. :wink:

ExecStart,
I just needed to think about it a little more :sweat_smile:
Now everything works perfectly, thank you very much for your precious help! :smiley: :beers:

1 Like