Hallo liebe Forenmitglieder.
ich bin auf dieses Forum aufmerksam geworden als ich nach einer Lösung für mein mpd-Problem gesucht habe.
Vielleicht kann mir hier jemand weiterhelfen.
Erstmal kurz zu mir:
Ich habe mit Programmierung oder IT-Technik beruflich nichts zu tun, alles was ich mache habe ich mir selber beigebracht.
Jetzt zu meinem Problem:
Ich habe mir unter zu Hilfe nahme des usualpanic-Codes ein kleines INet-Radio zusammen gebastelt.
Ich verwende dazu eine RPI B+ mit Adafruit 16x2 RGB-Display.
An dem Ursprungs-Code hat mir nicht gefallen, das man das Radio nicht "ausschalten" kann.
Daher habe ich nach einigem googeln mein Programm um eine mpd-Stop-Funktion erweitert und lasse nach dem Stop das Display ausschalten.
Jetzt habe ich das Problem, das mir der Radiostream ab und zu abbricht und danach der mpd nicht mehr startet, erst wenn ich den Sender umschalte funktioniert alles wieder.
Kann mir dazu jemand einen Tip geben?
Ich habe auch kein Problem damit den mpd in regelmässigen Abständen neu starten zu lassen, solange es keinen direkten Einfluss auf die Wiedergabe hat.
Für alle Interessenten habe ich den Code mal mit angehängt.
Danke schonmal für Eure Tips und Hilfe.
#!/usr/bin/python
# radio.py, version 2.1 (RGB LCD Pi Plate version)
# February 17, 2013
# Written by Sheldon Hartling for Usual Panic
# BSD license, all text above must be included in any redistribution
#
# Code angepasst von Dirk Raikowski
# based on code from Kyle Prier (http://wwww.youtube.com/meistervision)
# and AdaFruit Industries (https://www.adafruit.com)
# Kyle Prier - https://www.dropbox.com/s/w2y8xx7t6gkq8yz/radio.py
# AdaFruit - https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git, Adafruit_CharLCDPlate
#
#dependancies
from Adafruit_I2C import Adafruit_I2C
from Adafruit_MCP230xx import Adafruit_MCP230XX
from Adafruit_CharLCDPlate import Adafruit_CharLCDPlate
from datetime import datetime
from subprocess import *
from time import sleep, strftime
from Queue import Queue
from threading import Thread
import smbus
# initialize the LCD plate
# use busnum = 0 for raspi version 1 (256MB)
# and busnum = 1 for raspi version 2 (512MB)
LCD = Adafruit_CharLCDPlate(busnum = 1)
# Define a queue to communicate with worker thread
LCD_QUEUE = Queue()
# Globals
PLAYLIST_MSG = []
STATION = 1
NUM_STATIONS = 0
# Buttons
NONE = 0x00
SELECT = 0x01
RIGHT = 0x02
DOWN = 0x04
UP = 0x08
LEFT = 0x10
UP_AND_DOWN = 0x0C
LEFT_AND_RIGHT = 0x12
# ----------------------------
# WORKER THREAD
# ----------------------------
# Define a function to run in the worker thread
def update_lcd(q):
while True:
msg = q.get()
# if we're falling behind, skip some LCD updates
while not q.empty():
q.task_done()
msg = q.get()
LCD.setCursor(0,0)
LCD.message(msg)
q.task_done()
return
# ----------------------------
# MAIN LOOP
# ----------------------------
def main():
global STATION, NUM_STATIONS, PLAYLIST_MSG
# Stop music player
output = run_cmd("mpc stop" )
LCD.backlight(LCD.RED) # Schaltet die Hintergrundbeleuchtung auf Rot um das ausgeschaltete Radio zu zeigen
# Setup AdaFruit LCD Plate
LCD.begin(16,2)
LCD.clear()
# Create the worker thread and make it a daemon
worker = Thread(target=update_lcd, args=(LCD_QUEUE,))
worker.setDaemon(True)
worker.start()
# Display startup banner
LCD_QUEUE.put('Willkommen beim \nRaspPi-Radio', True)
# Load our station playlist
load_playlist()
sleep(5)
LCD.clear()
# ----------------------------
# START THE MUSIC!
# ----------------------------
# Start music player
LCD_QUEUE.put(PLAYLIST_MSG[STATION - 1], True)
run_cmd("mpc volume +100") # Stellt die Lautstaerke auf max
mpc_play(STATION)
countdown_to_play = 0
LCD.backlight(LCD.BLUE)
# Main loop
while True:
press = read_buttons()
# LEFT button pressed
if(press == LEFT):
STATION -= 1
if(STATION < 1):
STATION = NUM_STATIONS
LCD.clear()
LCD_QUEUE.put(PLAYLIST_MSG[STATION - 1], True)
# start play in 300msec unless another key pressed
countdown_to_play = 3
# RIGHT button pressed
if(press == RIGHT):
STATION += 1
if(STATION > NUM_STATIONS):
STATION = 1
LCD.clear()
LCD_QUEUE.put(PLAYLIST_MSG[STATION - 1], True)
# start play in 300msec unless another key pressed
countdown_to_play = 3
# UP button pressed
if(press == UP):
mpc_play(STATION) # Startet das Radio
LCD.clear()
LCD.backlight(LCD.BLUE)
LCD_QUEUE.put(PLAYLIST_MSG[STATION - 1], True)
# DOWN button pressed
if(press == DOWN):
output = run_cmd("mpc stop")
LCD.clear()
LCD_QUEUE.put('Bis \nBald', True)
LCD.backlight(LCD.RED)
sleep(5)
LCD.clear()
LCD.backlight(LCD.OFF)
# SELECT button pressed
if(press == SELECT):
menu_pressed()
# If we haven't had a key press in 300 msec
# go ahead and issue the MPC command
if(countdown_to_play > 0):
countdown_to_play -= 1
if(countdown_to_play == 0):
# Play requested station
mpc_play(STATION)
delay_milliseconds(99)
update_lcd.join()
# ----------------------------
# READ SWITCHES
# ----------------------------
def read_buttons():
buttons = LCD.buttons()
# Debounce push buttons
if(buttons != 0):
while(LCD.buttons() != 0):
delay_milliseconds(1)
return buttons
def delay_milliseconds(milliseconds):
seconds = milliseconds / float(1000) # divide milliseconds by 1000 for seconds
sleep(seconds)
# ----------------------------
# LOAD PLAYLIST OF STATIONS
# ----------------------------
def load_playlist():
global STATION, NUM_STATIONS, PLAYLIST_MSG
# Run shell script to add all stations
# to the MPC/MPD music player playlist
output = run_cmd("mpc clear")
output = run_cmd("/home/pi/radio_playlist.sh")
# Load PLAYLIST_MSG list
PLAYLIST_MSG = []
with open ("/home/pi/radio_playlist.sh", "r") as playlist:
# Skip leading hash-bang line
for line in playlist:
if line[0:1] != '#!':
break
# Remaining comment lines are loaded
for line in playlist:
if line[0] == "#" :
PLAYLIST_MSG.append(line.replace(r'\n','\n')[1:-1] + " ")
playlist.close()
NUM_STATIONS = len(PLAYLIST_MSG)
# ----------------------------
# RADIO SETUP MENU
# ----------------------------
def menu_pressed():
global STATION
MENU_LIST = [
'1. Zeitanzeige \n & IP Addresse ',
'2. Audioausgang \n ueber HDMI ',
'3. Audioausgang \n ueber Klinke ',
'4. Neustart \n ',
'5. Neuladen \n Senderliste ',
'6. Radio \n Ausschalten ',
'7. Ende \n ']
item = 0
LCD.clear()
LCD.backlight(LCD.RED)
LCD_QUEUE.put(MENU_LIST[item], True)
keep_looping = True
while (keep_looping):
# Wait for a key press
press = read_buttons()
# UP button
if(press == UP):
item -= 1
if(item < 0):
item = len(MENU_LIST) - 1
LCD_QUEUE.put(MENU_LIST[item], True)
# DOWN button
elif(press == DOWN):
item += 1
if(item >= len(MENU_LIST)):
item = 0
LCD_QUEUE.put(MENU_LIST[item], True)
# SELECT button = exit
elif(press == SELECT):
keep_looping = False
# Take action
if( item == 0):
# 1. display time and IP address
display_ipaddr()
elif(item == 1):
# 2. audio output to HDMI
output = run_cmd("amixer -q cset numid=3 2")
elif(item == 2):
# 3. audio output to headphone jack
output = run_cmd("amixer -q cset numid=3 1")
elif(item == 3):
# 4. Neustart
LCD_QUEUE.put("Radio \nNeustarten", True)
LCD.clear()
sleep(1)
LCD_QUEUE.put("Bis gleich\n ", True)
sleep(1)
LCD_QUEUE.put("Radio wird \nneu gestartet", True)
sleep(1)
LCD_QUEUE.join()
output = run_cmd("mpc clear")
output = run_cmd("sudo reboot")
LCD.clear()
LCD.backlight(LCD.OFF)
exit(0)
elif(item == 4):
# 5. reload our station playlist
LCD_QUEUE.put("Neuladen der\nStationsliste...", True)
load_playlist()
sleep(2)
STATION = 1
LCD_QUEUE.put(PLAYLIST_MSG[STATION - 1], True)
LCD.clear()
LCD.backlight(LCD.BLUE)
mpc_play(STATION)
elif(item == 5):
# 6. shutdown the system
LCD_QUEUE.put('Radio wird\nausgeschaltet! ... ', True)
LCD_QUEUE.join()
output = run_cmd("mpc clear")
output = run_cmd("sudo shutdown now")
LCD.clear()
LCD.backlight(LCD.OFF)
exit(0)
else:
delay_milliseconds(99)
# Restore display
LCD.backlight(LCD.BLUE)
LCD_QUEUE.put(PLAYLIST_MSG[STATION - 1], True)
# ----------------------------
# DISPLAY TIME AND IP ADDRESS
# ----------------------------
def display_ipaddr():
show_wlan0 = "ip addr show wlan0 | cut -d/ -f1 | awk '/inet/ {printf \"w%15.15s\", $2}'"
show_eth0 = "ip addr show eth0 | cut -d/ -f1 | awk '/inet/ {printf \"e%15.15s\", $2}'"
ipaddr = run_cmd(show_eth0)
if ipaddr == "":
ipaddr = run_cmd(show_wlan0)
LCD.backlight(LCD.WHITE)
i = 29
muting = False
keep_looping = True
while (keep_looping):
# Every 1/2 second, update the time display
i += 1
#if(i % 10 == 0):
if(i % 5 == 0):
LCD_QUEUE.put(datetime.now().strftime('%b %d %H:%M:%S\n')+ ipaddr, True)
# Every 3 seconds, update ethernet or wi-fi IP address
if(i == 60):
ipaddr = run_cmd(show_eth0)
i = 0
elif(i == 30):
ipaddr = run_cmd(show_wlan0)
# Every 100 milliseconds, read the switches
press = read_buttons()
# Take action on switch press
# UP button pressed
if(press == UP):
output = run_cmd("mpc volume +2")
# DOWN button pressed
if(press == DOWN):
output = run_cmd("mpc volume -2")
# SELECT button = exit
if(press == SELECT):
keep_looping = False
# LEFT or RIGHT toggles mute
elif(press == LEFT or press == RIGHT ):
if muting:
#amixer command not working, can't use next line
#output = run_cmd("amixer -q cset numid=2 1")
mpc_play(STATION)
# work around a problem. Play always starts at full volume
delay_milliseconds(400)
output = run_cmd("mpc volume +2")
output = run_cmd("mpc volume -2")
else:
#amixer command not working, can't use next line
#output = run_cmd("amixer -q cset numid=2 0")
output = run_cmd("mpc stop" )
muting = not muting
delay_milliseconds(99)
# ----------------------------
def run_cmd(cmd):
p = Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT)
output = p.communicate()[0]
return output
#def run_cmd_nowait(cmd):
# pid = Popen(cmd, shell=True, stdout=NONE, stderr=STDOUT).pid
def mpc_play(STATION):
pid = Popen(["/usr/bin/mpc", "play", '%d' % ( STATION )]).pid
if __name__ == '__main__':
main()
Alles anzeigen