Hallo,
ich habe das One Button Audiobook (siehe hier) nachgebaut.
Die hierin vorgeschlagenen Tests (Handbetrieb) funktionieren auch. Ich kann auch das python script starten und über den button die mp3 Datei starten und anhalten. Auch die Datenübernahme vom usb Stick funktioniert noch, aber nach dem erneuten start von mpd durch das script geht's in die Hose. Das Script stürzt mit Fehlermeldungen ab:
[ ok ] Stopping mpd (via systemctl): mpd.service.
[ ok ] Starting mpd (via systemctl): mpd.service.
error: Connection refused
error: Connection refused
error: Connection refused
[ ok ] Restarting mpd (via systemctl): mpd.service.
Traceback (most recent call last):
File "/home/pi/tobabp.py", line 130, in <module>
main()
File "/home/pi/tobabp.py", line 103, in main
print client.status()
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 211, in _execute
self._write_command(command, args)
File "/usr/lib/python2.7/dist-packages/mpd.py", line 224, in _write_command
self._write_line(" ".join(parts))
File "/usr/lib/python2.7/dist-packages/mpd.py", line 217, in _write_line
self._wfile.write("%s\n" % line)
File "/usr/lib/python2.7/dist-packages/mpd.py", line 53, in _dummy
raise ConnectionError("Not connected")
mpd.ConnectionError: Not connected
Alles anzeigen
Die neue mp3 datei wurde vom stick geladen und der stick un-mounted.
Bis dahin hat es also noch funktioniert.
Da ich allerdings von Python keinen blassen Schimmer habe, komme ich hier nicht wirklich weiter.
Kann mir jemand von den Python Gurus hier weiterhelfen?
Ich habe das script (mit meinen GPIO-Pin Anpassungen) angefügt:
#!/usr/bin/env python
# Copyright (C) 2012 Michael Clemens
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import RPi.GPIO as GPIO
import os
import pyudev
from mpd import (MPDClient, CommandError)
from socket import error as SocketError
from time import sleep
# Configure MPD connection settings
HOST = 'localhost'
PORT = '6600'
CON_ID = {'host':HOST, 'port':PORT}
# Configure IO ports
BUTTON = 17
LED = 2
GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTON, GPIO.IN)
GPIO.setup(LED, GPIO.OUT)
## Some functions
def mpdConnect(client, con_id):
"""
Simple wrapper to connect MPD.
"""
try:
client.connect(**con_id)
except SocketError:
return False
return True
def loadMusic(client, con_id, device):
os.system("mount "+device+" /music/usb")
os.system("/etc/init.d/mpd stop")
os.system("rm /music/mp3/*")
os.system("cp /music/usb/* /music/mp3/")
os.system("umount /music/usb")
os.system("rm /music/mpd/tag_cache")
os.system("/etc/init.d/mpd start")
os.system("mpc clear")
os.system("mpc ls | mpc add")
os.system("/etc/init.d/mpd restart")
def flashLED(speed, time):
for x in range(0, time):
GPIO.output(LED, GPIO.LOW)
sleep(speed)
GPIO.output(LED, GPIO.HIGH)
sleep(speed)
def updateLED(client):
# adjust LED to actual state
if client.status()["state"] == "play":
GPIO.output(LED, GPIO.LOW)
else:
GPIO.output(LED, GPIO.HIGH)
def checkForUSBDevice(name):
res = ""
context = pyudev.Context()
for device in context.list_devices(subsystem='block', DEVTYPE='partition'):
if device.get('ID_FS_LABEL') == name:
res = device.device_node
return res
def main():
## MPD object instance
client = MPDClient()
mpdConnect(client, CON_ID)
status = client.status()
print status
timebuttonisstillpressed = 0
flashLED(0.1, 5)
updateLED(client)
while True:
device = checkForUSBDevice("1GB") # 1GB is the name of my thumb drive
if device != "":
# USB thumb drive has been inserted, new music will be copied
flashLED(0.1, 5)
client.disconnect()
loadMusic(client, CON_ID, device)
mpdConnect(client, CON_ID)
print client.status()
flashLED(0.1, 5)
# wait until thumb drive is umplugged again
while checkForUSBDevice("1GB") == device:
sleep(1.0)
flashLED(0.1, 5)
if GPIO.input(BUTTON) == True:
if timebuttonisstillpressed == 0:
# button has been pressed, pause or unpause now
if client.status()["state"] == "stop":
client.play()
else:
client.pause()
updateLED(client)
elif timebuttonisstillpressed > 4:
# go back one track if button is pressed > 4 secs
client.previous()
flashLED(0.1, 5)
timebuttonisstillpressed = 0
timebuttonisstillpressed = timebuttonisstillpressed + 0.1
else:
timebuttonisstillpressed = 0
sleep(0.1)
# Script starts here
if __name__ == "__main__":
main()
Alles anzeigen
Danke schon mal.
Configuration:
Debian Jessie (aktuellste RASBIAN Version) auf RPi B
Nachtrag:
der Fehler liegt möglicherweise an einer ganz anderen Stelle, denn nachdem ich mal ein komplettes Hörbuch auf den USB Stick gepackt hatte, funktioniert zumindest der restart via script. Vorher hatte ich lediglich einen Testsong auf dem Stick.
Jetzt zeigt sich aber eine veränderte Symptomatik. Sobald das script mehrere Minuten ohne Tastendruck läuft, stürzt es beim nächsten Tastendruck ab. Dabei ist es gleichgültig ob die Wiedergabe gerade läuft oder ob der Tastendruck die Wiedergabe gerade starten sollte.
So sieht jetzt die Fehlermeldung aus:
Traceback (most recent call last):
File "/home/pi/tobabp.py", line 130, in <module>
main()
File "/home/pi/tobabp.py", line 112, in main
if client.status()["state"] == "stop":
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 329, in _fetch_object
objs = list(self._read_objects())
File "/usr/lib/python2.7/dist-packages/mpd.py", line 274, in _read_objects
for key, value in self._read_pairs():
File "/usr/lib/python2.7/dist-packages/mpd.py", line 253, in _read_pairs
pair = self._read_pair(separator)
File "/usr/lib/python2.7/dist-packages/mpd.py", line 244, in _read_pair
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
Alles anzeigen
Gunter