Here the circuit diagram:
Switch S1 can be set in three positions:
1 - Amplifier on/off controlled by raspberry
2 - Amplifier off
3 - Amplifier on
The raspberry pi GPIO controls the relay. Capacitor added because I don't want the raspberry to reset because of a brown out. The B+ and Pi 2 already reset at 4.65V if I'm correct.
Here the python script I use:
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
from mpd import MPDClient
#Auto switch off time in seconds
AsoTime = (15*60)
#Initialise amplifier control pin
CtlPin = 38
GPIO.setmode(GPIO.BOARD)
GPIO.setup(CtlPin, GPIO.OUT)
#Connect to MPD
try:
client = MPDClient()
client.connect("localhost", 6600)
except:
print('Could not connect to MPD client')
try:
Timer = 0 #ASO timer
Output = 0 #Output level of amplifier control pin
while True:
#client.status()['state'] can be 'play', 'stop' or 'pause'
if client.status()['state'] == 'play':
Output = 1
Timer = 0
#'stop' or 'pause'
else:
#Set timer
if Timer == 0 and Output == 1:
Timer = AsoTime
#Count down
if Timer > 0:
Timer -= 1
if Timer == 0:
Output = 0
time.sleep(1)
print(Timer)
GPIO.output(CtlPin, Output)
except KeyboardInterrupt:
print('Program stopped')
print('GPIO cleaned up')
#Free IO
GPIO.cleanup()
except:
print('Unknown error or exception occurred!')
Here the service file used by systemd:
[Unit]
Description=Amplifier power control
After=network.target
[Service]
ExecStart=/usr/bin/python /usr/local/test/gpio_test.py
TimeoutSec=0
Restart=always
RestartSec=1
StartLimitInterval=30
StartLimitBurst=20
[Install]
WantedBy=multi-user.target
If still need to search for a nice case for this. Any good ideas? Feedback appreciated.