Hallo Zusammen,
auch ich versuche mit dem Raspberry Pi 2 und Python ein Internet Radio zu realisieren. Mit vier Tastern und einem Drehwinkelgeber steuere ich das Ganze.
Den Drehwinkelgeber benutze ich für die Lautstärke und die vier Taster für 'Play', ' Pause', 'Next' und 'Press'. Mit jeder Betätigung der Taster bzw. Drehwinkelgeber lasse ich mir in der Konsole den aktuellen Sender, die aktuelle Lautstärke, Datum und Uhrzeit ausgeben. Das funktioniert auch soweit sehr gut. Jetzt würde ich allerdings die Ausgabe auch zyklisch, ohne Einwirkung von Außen, aktualisieren aber das will nicht so recht gelingen und mir gehen nach vielen Stunden probieren und Suchen im Web die Ideen aus.
Mein Code sieht so aus:
#!/usr/bin/python3
# IMPORTS #############################################################
import RPi.GPIO as GPIO, sys, os
import time # for debouncing of switches and rotary encoder
from datetime import date
from mpd import MPDClient
# variables for GPIO
switchPlay = 3
switchStop = 5
switchNext = 29
switchPrev = 33
# variables for rotary encoder
pulseA = 16
pulseB = 18
# generall variables
volume = 85 # in percent
client = MPDClient()
debounceTimeSwitch = 0.3 # s
debounceTimeEncoder = 0.4 # s
lastPlayedRadioStation = 0
counter = 0
# generall setup
GPIO.setwarnings(True) # disable warnings
GPIO.setmode(GPIO.BOARD) # pin numbering pf 40-pin plug-in
# setup the following gpio-pin as inputs incl. pull-up resistor
GPIO.setup(switchPlay, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(switchStop, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(switchNext, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(switchPrev, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(pulseA, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(pulseB, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# FUNCTIONS ###########################################################
def fktCtrlRadioStation(channel):
global lastTime
global lastPlayedRadioStation
try:
now = time.time()
if now-lastTime > debounceTimeSwitch:
lastTime = time.time()
mpdClient(0) # connect to client
if channel == switchPlay:
print('pressed play')
client.play()
printInfoToDisplay()
elif channel == switchStop:
print('pressed stop')
client.stop()
elif channel == switchNext:
client.next()
lastPlayedRadioStation = int(client.status()['song'])
printInfoToDisplay()
elif channel == switchPrev:
client.previous()
lastPlayedRadioStation = int(client.status()['song'])
printInfoToDisplay()
else:
print("failure in fktCtrlRadioStation")
mpdClient(1) # disconnect from client
except:
print("Something went wrong within fktCtrlVolume")
def fktCtrlVolume(channel):
global lastTimeEncoder
global currentVolume
try:
now = time.time()
if now-lastTimeEncoder > debounceTimeSwitch:
lastTimeEncoder = time.time()
mpdClient(0) # connect to client
if GPIO.input(pulseB) == 0:
currentVolume = int(client.status()['volume'])
currentVolume = currentVolume + 5
if currentVolume > 100:
currentVolume = 100
client.setvol(currentVolume)
printInfoToDisplay()
elif GPIO.input(pulseB) == 1:
currentVolume = int(client.status()['volume'])
currentVolume = currentVolume - 5
if currentVolume < 0:
currentVolume = 0
client.setvol(currentVolume)
printInfoToDisplay()
else:
print("failure in myCBpulse")
mpdClient(1) # disconnect from client
except:
print("Something went wrong within fktCtrlVolume")
def printInfoToDisplay():
global lastPlayedRadioStation
global currentRadioStation
global currentVolume
global lengthOfPlaylist
counter = 0
try:
# read radio station from text file and save stations in variable
with open('senderliste.txt', 'r') as f:
for line in f:
counter += 1
if counter == lastPlayedRadioStation + 1:
currentRadioStation = line.rstrip()
# write current radio station (ID) to text file
with open('parameter.txt', 'w') as out:
lastPlayedRadioStation
out.write(str(lastPlayedRadioStation))
# print info to console
os.system('clear')
print(currentRadioStation)
print('Volume = ' + str(currentVolume) + '%')
# time
currentDate = date.today() # current Date YYYY-MM-DD
currentTime = time.localtime()
print(str(currentDate) + ' - ' + str(currentTime.tm_hour) + ':' + str(currentTime.tm_min) + 'Uhr')
except:
print("Something went wron within printInfoToDisplay")
def mpdClient(ID):
if ID == 0:
# connect to client
client.timeout = 10
client.idletimeout = None
client.connect("localhost", 6600)
elif ID == 1:
# disconnect from client
client.close()
client.disconnect()
else:
print('failure in function mpdClient')
# failure
# INTERUPTS ###########################################################
GPIO.add_event_detect(switchPlay, GPIO.FALLING, callback=fktCtrlRadioStation)
GPIO.add_event_detect(switchStop, GPIO.FALLING, callback=fktCtrlRadioStation)
GPIO.add_event_detect(switchNext, GPIO.FALLING, callback=fktCtrlRadioStation)
GPIO.add_event_detect(switchPrev, GPIO.FALLING, callback=fktCtrlRadioStation)
GPIO.add_event_detect(pulseA, GPIO.FALLING, callback=fktCtrlVolume)
# MAIN ################################################################
# load playlist from radio.m3u
mpdClient(0) # connect to client
client.clear() # clear current playlist
client.load("radio") # load playlist with stored links
# define length of playlist and save to variable (lengthOfPlaylist)
lengthOfPlaylist = int(client.status()['playlistlength'])
print('Playlistlenght = ' + str(lengthOfPlaylist))
# read last played radio station and save number to variable (lastPlayedRadioStation)
with open('parameter.txt', 'r') as f:
for line in f:
lastPlayedRadioStation = int(line.rstrip())
print('Last played radio station = ' + str(lastPlayedRadioStation))
# qualifiy length of playlist and last played radio station (if failed, set lastPlayedRadioStation to 0)
if lastPlayedRadioStation + 1 > lengthOfPlaylist:
print('Failure: Last played radio station and playlist length not plausible')
lastPlayedRadioStation = 0
print('Restet variable: ' + str(lastPlayedRadioStation))
# identify name of radio station and save to variable (currentRadioStation)
with open('senderliste.txt', 'r') as f:
for line in f:
counter += 1
if counter == lastPlayedRadioStation + 1:
currentRadioStation = line.rstrip()
print('Aktueller Sender ist: ' + currentRadioStation)
# read current volume and save to variable (currentVolume)
currentVolume = client.status()['volume']
print(currentVolume)
# play last radio station
client.clear() # clear current playlist
client.load("radio") # load playlist with stored links
client.setvol(volume) # set volume
client.play(lastPlayedRadioStation)
mpdClient(1) # disconnect from client
# Software debouncing
lastTime = time.time()
lastTimeEncoder = time.time()
try:
while True:
time.sleep(0.5) # time delay 5s
except KeyboardInterrupt:
print("Programm aborted by user.")
pass
# cleanup
finally:
GPIO.cleanup()
Alles anzeigen
Um jetzt meine zyklische Aktualisierung zu realisieren habe ich die while Schleife am Ende folgendermaßen angepasst. Aufrufen der Funktion printInfoToDisplay alle 5s.
Sobald ich das aber einfüge reagiert das Programm nicht mehr auf die Taster bzw. den Drehwinkelgeber und ich verstehe nicht warum.
Ich sag schon mal vielen Dank.
Gruß Raymond79