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 "button.py", line 36, in <module> client.next() File "/usr/lib/python2.7/dist-packages/mpd.py", line 167, in <lambda> return lambda *args: wrapper(command, args) File "/usr/lib/python2.7/dist-packages/mpd.py", line 213, in _execute return retval() File "/usr/lib/python2.7/dist-packages/mpd.py", line 312, in _fetch_nothing line = self._read_line() File "/usr/lib/python2.7/dist-packages/mpd.py", 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 iknowthe.net/blog/raspberry- … utton.html) thanks to the author.

[code]import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(4,GPIO.IN)
buttonPinNext = 4
GPIO.setup(buttonPinNext,GPIO.IN)
import mpd
import time

global TEST_MPD_HOST, TEST_MPD_PORT, TEST_MPD_PASSWORD
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 == False)): # Play the next song in this play list.
print “Next track in play list”
client.next()
client.ping()
time.sleep(1)[/code]

Hi,

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.

Pi.jpg

I use the folowing script (button.py):

#!/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.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIOPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

GPIO

# 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")
                time.sleep(1)
                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
        time.sleep(0.1)

    return


# call main function

try:     
  main()

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

Regards

Harry
Button.zip (90.8 KB)

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