Change the relay pin to the GPIO pin for your relay. | |
Save the contents of relaytoggle.py script to /home/pi/relaytoggle.py | |
Save the contents of relaytoggle.service script to /lib/systemd/system/relaytoggle.service | |
Run the following commands | |
sudo systemctl enable relaytoggle | |
sudo service relaytoggle start |
#!/usr/bin/env python3 | |
import RPi.GPIO as GPIO | |
import os | |
import time | |
defaultDelay = 5 #seconds | |
relayPin = 2 # GPIO pin in BCM | |
currentState = False # the current state | |
GPIO.setwarnings(False) | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(relayPin, GPIO.OUT) | |
def isRunning(): | |
return "RUNNING" in os.popen('cat /proc/asound/card*/pcm*/sub*/status | grep RUNNING').read().split() | |
while True: | |
if isRunning() and not currentState: | |
GPIO.output(relayPin, 1) | |
currentState = True | |
print("Relay Activated") | |
time.sleep(defaultDelay) | |
elif not isRunning() and currentState: | |
# delay again on playback stops to account for track changes | |
time.sleep(defaultDelay) | |
if isRunning(): | |
# if it's running don't deactivate the realay | |
print("Playback resumed, cancelling deactivation") | |
continue | |
else: | |
# deactivate the relay | |
GPIO.output(relayPin, 0) | |
currentState = False | |
print("Relay Deactivated") | |
time.sleep(defaultDelay) | |
else: | |
print("No State Change...") | |
time.sleep(defaultDelay) |
[Unit] | |
Description=MoOde Relay Toggle Daemon | |
[Service] | |
Restart=on-failure | |
WorkingDirectory=/home/pi | |
ExecStart=python relaytoggle.py | |
ExecReload=/bin/kill -HUP $MAINPID | |
KillSignal=SIGINT | |
[Install] | |
WantedBy=multi-user.target |
Hi
Thanks for uploading this. It works like a dream.
I tweaked the Python script a little bit, to use a simple state machine. This way I can set the the pinout is set to one (turbine the amplifier on) after e very short time (in this case - 1 second), And the Amplifier stays on for a longer duration (60 seconds in this case) after playback stops. This means that the amplifier wont turn off, even if I pause the music for a short while (for instance if my wife asks me a question)
I'm new to Github, so I'm not sure if I should create a fork or how else the share my scripts. Suggestions are welcome
Hi
Thanks for uploading this. It works like a dream.
I tweaked the Python script a little bit, to use a simple state machine. This way I can set the the pinout is set to one (turbine the amplifier on) after e very short time (in this case - 1 second), And the Amplifier stays on for a longer duration (60 seconds in this case) after playback stops. This means that the amplifier wont turn off, even if I pause the music for a short while (for instance if my wife asks me a question)
I'm new to Github, so I'm not sure if I should create a fork or how else the share my scripts. Suggestions are welcome
Hey!
I want to see your version if possible - seems like it's doing the thing that I'm searching right now.
You can fork this Gist, and edit it, in this way you will make a new revision. If you want, you can modify the Instructions to give a credit to the original creator :)
I think it may be because your relay is wired for normally closed, or is a normally closed relay, so it opens in the high state.
If it's wired for normally closed and you have the option, wire it for normally open.
If that's not an option, invert the GPIO.output(replayPin, 0) with GPIO.output(replayPin, 1) and vice versa.