Python script stops due to mpd disconnect

Hi everyone :smiley:

I’ve been loving volumio and built myself an internet radio with a button for changing the stations (on raspberry pi). I also added some voice prompts between stations to help find them (I’ll post this later). Works great, except the Python script stops due to an error.

I start the script at startup in /etc/rc.local with no problems.

The error is due to the connection with mpd being lost (I think). Is there a good way to keep running the script by catching the error or such like. I’m fairly new to coding and have never used python until now.

Error as follows:

File "", line 36, in <module> File "/usr/lib/python2.7/dist-packages/", line 167, in <lambda> return lambda *args: wrapper(command, args) File "/usr/lib/python2.7/dist-packages/", line 213, in _execute return retval() File "/usr/lib/python2.7/dist-packages/", line 312, in _fetch_nothing line = self._read_line() File "/usr/lib/python2.7/dist-packages/", line 229, in _read_line raise ConnectionError("Connection lost while reading line") mpd.ConnectionError: Connection lost while reading line

The script looks like this (should say this is adapted from … utton.html) thanks to the author.

[code]import RPi.GPIO as GPIO
buttonPinNext = 4
import mpd
import time

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
client.connect(TEST_MPD_HOST, TEST_MPD_PORT)
except SocketError as e:
connected = False
if connected == False:
print “Couldn’t connect. Retrying”

while True:
if (GPIO.input(buttonPinNext == False)): # Play the next song in this play list.
print “Next track in play list”


To control mpc with a button you don’t have to use python-mpd etc.
At work I have a headless Pi (analoque output) with " One button control"

When you press the button once, it will sounds a beep en it will jumps to the next song. When you press and hold the button, after a few seconds the shutdown message will be played and the Pi turns off.


I use the folowing script (

#!/usr/bin/env python

# -*- coding: utf-8 -*-
# import required modules

import time
import os
import RPi.GPIO as GPIO

# set GPIO pin
GPIOPin = 17
GPIO.setup(GPIOPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)


# main function

def main():
    value = 0

    while True:

        # increment value if button is pressed
        if not GPIO.input(GPIOPin):
            value += 0.1

        if value == 0.1:
	    os.system("aplay /home/bleep.wav")

        # restart or shutdown selected
        if value > 0:

            # shutdown selected if value is larger than 3 or equal
            if value >= 3:
                #print value
                print "Shutdown"
                os.system("mpc stop")
                os.system("aplay /home/shutdown.wav")
                os.system("systemctl poweroff")
                return 0

            # restart selected if value is less than 3

            elif GPIO.input(GPIOPin):
                #print value
                print "Next"
                #os.system("aplay /home/bleep.wav")
                os.system("mpc next")
                os.system("mpc play")
                value = 0             
        # wait 500ms


# call main function


except KeyboardInterrupt:  
    GPIO.cleanup()       # clean up GPIO on CTRL+C exit  
GPIO.cleanup()           # clean up GPIO on normal exit 


Harry (90.8 KB)

Great thanks that looks like a better approach I’ll give it a go. :smiley: