Controlling an LED by play state

Raspberry Pi related support

Controlling an LED by play state

Postby littledogie » 11 Jun 2017, 00:37

I'm sure the answer is already here in the forums, but I am so new to this all that I don't know how to ask the question to find answers. I am working on mounting my Raspberry Pi with Hifiberry DAC+ into the body of an old flip-style clock radio. My goal is to replace the light behind the frequency display with an LED tied into the GPIO that turns on when RuneAudio is playing.

The DAC+ already has a light that does this, but I'm not sure if that function can be "redirected" to a GPIO pin. There's probably an easy way to use a Python script to detect RuneAudio's play state and use that variable to set the LED status... I'd welcome any help directing me to the best place to read up on how.

Thanks!
RuneAudio release version: 0.3 (build: beta-20160313)
Raspberry Pi 3B
Hifiberry DAC+
littledogie
 
Posts: 7
Joined: 05 Aug 2016, 23:09

Re: Controlling an LED by play state

Postby littledogie » 19 Sep 2017, 02:19

Well, I continue to fumble along in this tiny little project... the pi and DAC+ are nicely mounted in the clock radio, and the LED is wired up and I can get it to turn on and off manually using a python script. But now I'm stuck at the stage of tying the LED state to the MPD state. Instead of posting a fresh thread, I'm hoping that adding some additional information to this Beginner's Question will help generate some suggestions...

I have installed python-mpd2 which I think is supposed to allow python to read the state of the MPD player --- I installed it into the root directory... because that's where I happened to be when I ran the installation.

Also, along the way I installed python 3. I see that my lib folder now includes python2.7, python3.5, and python3.6.

So... just to see if I could make something happen, I used some code from Frank on another thread using python-MPD, and stuck a print command at the end of what I guess is the "header" stuff:

Code: Select all
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#IMPORTS
import sys
import time

from mpd import (MPDClient,CommandError)
from socket import error as SocketError

HOST = 'localhost'
PORT = '6600'
PASSWORD = false
##
CON_ID = {'host':HOST, 'port':PORT}
##

print(client.status())


When I run this using python OR python2 I get an error on line 8: "Import Error: No module named mpd"

I have a hunch that the problem originates in either the multiple versions of python that are installed, OR in the fact that various components are saved in different locations. But of course, it may originate in something I don't know about - since I am a total noob...

Any advice for this greenhorn is much appreciated -- when I get past this basic issue, I look forward to messing around with the actual commands! :D
littledogie
 
Posts: 7
Joined: 05 Aug 2016, 23:09

Re: Controlling an LED by play state

Postby ianhaynes » 19 Sep 2017, 09:35

The simplest way to do this is to take the Pi activity LED onto an external LED that you can mount on the case. This doesn't need any Python script. The following code needs adding to the config.txt.

This uses pin 13 on the GPIO (Broadcom GPIO27).

Code: Select all
dtparam=act_led_gpio=27
dtparam=act_led_trigger=default-on


Connect your LED between pin 13 and a ground pin, with a suitable resistor.

I have my Pi and DAC in a box with a linear PSU and use this extended activity LED to indicate when it's safe to switch the power off.

HTH
Pi 2, Ver. 0.5b, IQAudio DAC+
ianhaynes
 
Posts: 207
Joined: 04 Mar 2015, 12:51

Re: Controlling an LED by play state

Postby hondagx35 » 19 Sep 2017, 16:07

Hi ianhaynes,

he wants a play indicator!

Hi littledogie,

I have installed python-mpd2 which I think is supposed to allow python to read the state of the MPD player --- I installed it into the root directory

How did you do this?
Install it like this and it should work:
Code: Select all
pacman -Sy python2-mpd


Your code has errors and will fail!

Frank
User avatar
hondagx35
 
Posts: 3042
Joined: 11 Sep 2014, 22:06
Location: Germany

Re: Controlling an LED by play state

Postby littledogie » 19 Sep 2017, 19:14

Thanks Frank! I must have been mixed up since both python-mpd2 and python2-mpd exist...

I'll give it a test tonight and see how it goes.

(BTW - I assumed there would be errors in my script --- I'm looking forward to code errors instead of complete failures to launch... :) )
littledogie
 
Posts: 7
Joined: 05 Aug 2016, 23:09

Re: Controlling an LED by play state

Postby hondagx35 » 19 Sep 2017, 20:28

Hi,

I must have been mixed up since both python-mpd2 and python2-mpd exist...

Yes and both will work if installed the correct way!
Code: Select all
git clone https://github.com/Mic92/python-mpd2.git
cd python-mpd2
python setup.py install


Frank
User avatar
hondagx35
 
Posts: 3042
Joined: 11 Sep 2014, 22:06
Location: Germany

Re: Controlling an LED by play state

Postby littledogie » 20 Sep 2017, 02:51

Frank - you're a scholar and a gentleman. I got python-mpd2 working, and am now happily generating terrible code that doesn't work - but it's not working for the right reasons, and I can proceed to learn from my mistakes. Thanks again for your help - there is an LED glow at the end of this tunnel ;)
littledogie
 
Posts: 7
Joined: 05 Aug 2016, 23:09

Re: Controlling an LED by play state

Postby littledogie » 25 Sep 2017, 13:56

For those beginners like me who may be struggling to get some of these basics figured out, I thought I would post what I did to successfully get the LED to turn on and off based on whether RuneAudio is playing (on) or paused/stopped (off).

Preliminary notes: I connected the LED to the pin assigned to GPIO17... if you use a different pin you need to make sure you specify that in your code. I also appear to have done this all with Python 3, and if you are using Python 2 you will probably need to make adjustments to the code - but don't ask me what those should be :D ... finally - the code is probably not very clean or efficient - I'm sure there are better ways of doing what I want it to do - maybe for example using the MPDClient.idle() command... but I was becoming impatient to get this working and I found a path that brought me where I wanted to go. So, without further ado:

Step 1: I used nano to create a python script called statusled.py in my root folder:

Code: Select all
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# IMPORTS
import sys
import time
import RPi.GPIO as GPIO

from mpd import (MPDClient, CommandError)
from socket import error as SocketError

GPIO.setmode(GPIO.BCM)
GPIO.setup(17,GPIO.OUT)

#definitions
client = MPDClient()
client.connect('localhost', 6600)

# Poll the playstate and set GPIO.output accordingly

while True:
    status=client.status()
    if status['state']=='play':
        GPIO.output(17,GPIO.HIGH)
    else:
        GPIO.output(17,GPIO.LOW)
    print('next')
    time.sleep(1) # Delay loop for 1 second.

client.disconnect()



Step 2: I used nano to create a service in the etc/systemd/system folder called statusled.service:

Code: Select all
[Unit]
Description=Test
After=network.target

[Service]
ExecStartPre=/usr/bin/sh -c "sleep 20"
ExecStart=/usr/bin/python /root/statusled.py
TimeoutSec=0
Restart=on-failure

[Install]
WantedBy=multi-user.target


Step 3: I enabled the service by typing this into the command line:

Code: Select all
systemctl enable statusled.service


Step 4: I rebooted the system using the RuneAudio web UI to do this.

Step 5: I tried playing and pausing to see what happens... and the light went on and off accordingly!

A big thank-you to all the experienced people who have posted their work here on the forum for us to learn from - and another big thank-you to all those who take time to help with our questions when we post them -- you're all champs! :D
littledogie
 
Posts: 7
Joined: 05 Aug 2016, 23:09

Re: Controlling an LED by play state

Postby Tall Person » 06 Nov 2017, 03:08

Hi guys,

I wanted to get an external LED on my Pi Zero W box and so did a bit of searching into the forums as you do. I have come up with a better way that works for me, where the external LED serves as the Power and Activity LED. I have hooked up the LED to pin 15 (GPIO22) and grounded to pin 14, with a 330R resistor and then added my version of the code to /boot/config.txt as below:

Code: Select all
dtparam=act_led_trigger=cpu0
dtparam=act_led_activelow=on
dtparam=act_led_gpio=22


I found that when streaming, the processor only just flicked the LED, not enough to see or be worth seeing, so I thought, why not reverse it? With the activelow set as on, the LED works in reverse, so the LED generally stays on and then flicks off to indicate the CPU running. So it acts like a power LED as well as showing nice cpu activity when streaming. It's good on shutdown to indicate once it has stopped working too.

Thanks to Ameer on http://raspberrypi/stackexchange.com for LED status alternatives and http://www.jeffgeerling.com for otherclues on the setting of the LED - between these and some trial and error I found many of the codes worked, but this one is perfect.

Now to get that shutdown switch sorted.....!
-Raspberry Pi B rev 2, Hifiberry DAC
-Raspberry Pi Zero W, Hifiberry DAC, Runeaudio 0.4
Tall Person
 
Posts: 16
Joined: 16 Sep 2017, 19:43

support RuneAudio Donate with PayPal


Return to Raspberry Pi

Who is online

Users browsing this forum: No registered users and 9 guests
cron