Hi everyone
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]