After a lot of tearing my hair out, I have now got a connection to the MPD server which doesn’t drop out and a stable, high quality music server pulling the music from my NAS – with a next button for the odd misstep on the playlist. Here is the code for your delectation.

buttonfinal

Rename it to button.py and run it using python button.py. Check this post for the build details.

 

I’ve been playing with the very excellent Volumio.  Having got it all up and running, with a Hifiberry DAC and an LCD from http://andypi.co.uk/?p=351 it was pretty good. However, Volumio only allows for one playlist and the occasional song I was not in the mood for crept in.

So I wanted a skip to the next song button. Entirely possible with the Pi, and I found a guy who had done it – but his code wasn’t complete. Here are the full instructions:

Connect a switch to two spare gpio pins. I used GPIO 4 to ground.  This was useful.

GPIOs

 

 

 

 

 

 

 

 

sudo apt-get update

sudo apt-get install python-dev python-pip
sudo pip install –upgrade distribute
sudo pip install ipython
sudo pip install –upgrade RPi.GPIO

Then make a file – mine is called button py

nano button.py

This is the code

[code language="python"]

TEST_MPD_HOST = "localhost"
TEST_MPD_PORT = "6600"
TEST_MPD_PASSWORD = "volumio" # password for Volumio / MPD

# Connect with MPD
client = mpd.MPDClient()
connected = False
while connected == False:
connected = True
try:
client.connect(TEST_MPD_HOST, TEST_MPD_PORT)
except SocketError as e:
connected = False
if connected == False:
print "Couldn't connect. Retrying"
time.sleep(5)
print("Connected")

while True:
if (GPIO.input(buttonPinNext)): # Play the next song in this play list.
print "Next track in play list"
client.next()
time.sleep(1)

[/code]

Ctrl x to save

Download it here and change the extentio from txt to py

Test the code:

python button.py

Make it run at boot – you need to edit /etc/rc.local (as root since this is the owner).

sudo nano /etc/rc.local

At the bottom, just above exit 0 we’ll add a call to our script.

python /root/python.py

Ctrl x to save

Here is a rubbish picture of the finished article. 

Tlk7K6K

 

 

 

 

 

 

Disclaimer. This may not be the worlds best Python. Please let me know if I can improve it!

25 years of the WWW

March 12th, 2014

25 years of the web today. From humble beginnings…

nextmachine

Just a link

November 28th, 2013

Banbury Plant & Skip Hire – new site

Emma Kane speaking at the NEC

January 31st, 2013

I’ll be speaking at Spring Fair International at the NEC on Monday the 4th and Tuesday the 5th February. Come and say hi.