Thank you for your donation!


Cloudsmith graciously provides open-source package management and distribution for our project.


Solved: LED indicator to show when music is playing
#1
Hi all,

I'm in the middle of giving my moode player a new enclosure and I'd like to use an LED light to show when moode/MPD is playing (I only use local files or radios, so I won't need it to work with Airplay or any other external service). 
Is there a way to implement this or does anybody know how to point me in the right direction? 

I've tried searching the forum but I couldn't find what I was looking for 

Thanks a bunch 

Luca
Reply
#2
Do you want to use the built-in LEDs on the rpi, or did you want to have external LEDs of some sort?

In terms of an external LED, I found this: https://github.com/francois2metz/MpdPi
and https://github.com/if1live/rpi-mpd-controller
Reply
#3
(02-22-2022, 08:43 PM)swinokur Wrote: Do you want to use the built-in LEDs on the rpi, or did you want to have external LEDs of some sort?

In terms of an external LED, I found this: https://github.com/francois2metz/MpdPi
and https://github.com/if1live/rpi-mpd-controller

Hi swinokur, thanks I will have a look. I wanted to use an external LED, which is currently wired to GND and to GPIO13
Reply
#4
If only using MPD then one way to determine play state is via the command below.

Code:
# Playing
pi@moode:~ $ mpc status | grep playing
[playing] #50/50   0:10/0:39 (25%)

# Not playing
pi@moode:~ $ mpc status | grep playing
pi@moode:~ $
Enjoy the Music!
moodeaudio.org | Mastodon Feed | GitHub
Reply
#5
(02-22-2022, 09:18 PM)Tim Curtis Wrote: If only using MPD then one way to determine play state is via the command below.

Code:
# Playing
pi@moode:~ $ mpc status | grep playing
[playing] #50/50   0:10/0:39 (25%)

# Not playing
pi@moode:~ $ mpc status | grep playing
pi@moode:~ $
Hi Tim, thanks for your help. I will look into it - I'm quite new to this so I will see how to put something together in python or adapt some of the scripts suggested by swinokur
Reply
#6
(02-22-2022, 09:18 PM)Tim Curtis Wrote: If only using MPD then one way to determine play state is via the command below.

Code:
# Playing
pi@moode:~ $ mpc status | grep playing
[playing] #50/50   0:10/0:39 (25%)

# Not playing
pi@moode:~ $ mpc status | grep playing
pi@moode:~ $

Here is a python script I use. I started with a display then found headless better. Just start this script in rc.local with
sudo python3 /home/pi/playled.py -d &

Code:
import os
import subprocess
import time

#os.system("sudo sh -c 'echo none > /sys/class/leds/led0/trigger'")
#os.system("sudo sh -c 'echo none > /sys/class/leds/led1/trigger'")

while True:
    output = subprocess.getoutput(["mpc -w status"])
    if "[playing]" in output:
        os.system("sudo sh -c 'echo 0 > /sys/class/leds/led0/brightness'")
        os.system("sudo sh -c 'echo 1 > /sys/class/leds/led1/brightness'")
#        print("Playing")
        os.system("mpc -q -w idle")

    else:
        if "[paused]" in output:
            os.system("sudo sh -c 'echo 0 > /sys/class/leds/led1/brightness'")
            os.system("sudo sh -c 'echo 1 > /sys/class/leds/led0/brightness'")
#            print("paused")
            os.system("mpc -q -w idle")
        else:
            os.system("sudo sh -c 'echo 0 > /sys/class/leds/led1/brightness'")
            os.system("sudo sh -c 'echo 0 > /sys/class/leds/led0/brightness'")
#            print("stopped")
            os.system("mpc -q -w idle")
Reply
#7
(02-22-2022, 09:48 PM)Macdelf Wrote:
(02-22-2022, 09:18 PM)Tim Curtis Wrote: If only using MPD then one way to determine play state is via the command below.

Code:
# Playing
pi@moode:~ $ mpc status | grep playing
[playing] #50/50   0:10/0:39 (25%)

# Not playing
pi@moode:~ $ mpc status | grep playing
pi@moode:~ $

Here is a python script I use. I started with a display then found headless better. Just start this script in rc.local with
sudo python3 /home/pi/playled.py -d &

Code:
import os
import subprocess
import time

#os.system("sudo sh -c 'echo none > /sys/class/leds/led0/trigger'")
#os.system("sudo sh -c 'echo none > /sys/class/leds/led1/trigger'")

while True:
    output = subprocess.getoutput(["mpc -w status"])
    if "[playing]" in output:
        os.system("sudo sh -c 'echo 0 > /sys/class/leds/led0/brightness'")
        os.system("sudo sh -c 'echo 1 > /sys/class/leds/led1/brightness'")
#        print("Playing")
        os.system("mpc -q -w idle")

    else:
        if "[paused]" in output:
            os.system("sudo sh -c 'echo 0 > /sys/class/leds/led1/brightness'")
            os.system("sudo sh -c 'echo 1 > /sys/class/leds/led0/brightness'")
#            print("paused")
            os.system("mpc -q -w idle")
        else:
            os.system("sudo sh -c 'echo 0 > /sys/class/leds/led1/brightness'")
            os.system("sudo sh -c 'echo 0 > /sys/class/leds/led0/brightness'")
#            print("stopped")
            os.system("mpc -q -w idle")
Brilliant, thanks Macdelf I'm going to try this. How do I specify which GPIO/PIN it will need to use for the LED?
Reply
#8
(02-22-2022, 10:00 PM)seipersei Wrote:
(02-22-2022, 09:48 PM)Macdelf Wrote:
(02-22-2022, 09:18 PM)Tim Curtis Wrote: If only using MPD then one way to determine play state is via the command below.

Code:
# Playing
pi@moode:~ $ mpc status | grep playing
[playing] #50/50   0:10/0:39 (25%)

# Not playing
pi@moode:~ $ mpc status | grep playing
pi@moode:~ $

Here is a python script I use. I started with a display then found headless better. Just start this script in rc.local with
sudo python3 /home/pi/playled.py -d &

Code:
import os
import subprocess
import time

#os.system("sudo sh -c 'echo none > /sys/class/leds/led0/trigger'")
#os.system("sudo sh -c 'echo none > /sys/class/leds/led1/trigger'")

while True:
    output = subprocess.getoutput(["mpc -w status"])
    if "[playing]" in output:
        os.system("sudo sh -c 'echo 0 > /sys/class/leds/led0/brightness'")
        os.system("sudo sh -c 'echo 1 > /sys/class/leds/led1/brightness'")
#        print("Playing")
        os.system("mpc -q -w idle")

    else:
        if "[paused]" in output:
            os.system("sudo sh -c 'echo 0 > /sys/class/leds/led1/brightness'")
            os.system("sudo sh -c 'echo 1 > /sys/class/leds/led0/brightness'")
#            print("paused")
            os.system("mpc -q -w idle")
        else:
            os.system("sudo sh -c 'echo 0 > /sys/class/leds/led1/brightness'")
            os.system("sudo sh -c 'echo 0 > /sys/class/leds/led0/brightness'")
#            print("stopped")
            os.system("mpc -q -w idle")
Brilliant, thanks Macdelf I'm going to try this. How do I specify which GPIO/PIN it will need to use for the LED?
None needed. This takes over the power/ status lights.
Red is playing
Green is paused
No light is stopped, that also means Moode has booted.
As you probably guessed just insert the code into a file called playled.py in your home directory then edit rc.local in the /etc/ one to add the python 3 command.
Reply
#9
Success!

So here's how I adapted Macdelf's script to my needs:

I have an LED light mounted on the front panel of my enclosure. This LED is connected to GND and to GPIO13 which was used as an activity status indicator. For that to work I added the following to /boot/config.txt:
Code:
#Use LED as SD indicator on GPIO 13
dtoverlay=pi3-act-led,gpio=13

Using Macdelf script made the status light (front panel) go off, and the power light go on when music was playing, so I simply inverted the behaviour of the script (echo 1 and echo 0) as following:
Code:
import os 
import subprocess
import time
                                                                                                                                                                                                           
#os.system("sudo sh -c 'echo none > /sys/class/leds/led0/trigger'")
#os.system("sudo sh -c 'echo none > /sys/class/leds/led1/trigger'")
                                                                                                                                                                                                           
while True:
        output = subprocess.getoutput(["mpc -w status"])
        if "[playing]" in output:
                os.system("sudo sh -c 'echo 1 > /sys/class/leds/led0/brightness'")
                os.system("sudo sh -c 'echo 0 > /sys/class/leds/led1/brightness'")
                os.system("mpc -q -w idle")
                                                                                                                                                                                                           
        else:
                if "[paused]" in output:
                        os.system("sudo sh -c 'echo 0 > /sys/class/leds/led1/brightness'")
                        os.system("sudo sh -c 'echo 0 > /sys/class/leds/led0/brightness'")
                        os.system("mpc -q -w idle")
                else:
                        os.system("sudo sh -c 'echo 0 > /sys/class/leds/led1/brightness'")
                        os.system("sudo sh -c 'echo 0 > /sys/class/leds/led0/brightness'")
                        os.system("mpc -q -w idle")
Now when music is playing the pilot light on the front panel will light up exactly as I wanted it to do. 

Thanks again to all of you for the help!
Reply


Forum Jump: