Skip to content

Instantly share code, notes, and snippets.

@michaelfdeberry
Last active April 14, 2024 14:38
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save michaelfdeberry/d3e7c9b4c4e1305be863d81a7e09f3de to your computer and use it in GitHub Desktop.
Save michaelfdeberry/d3e7c9b4c4e1305be863d81a7e09f3de to your computer and use it in GitHub Desktop.
Python 3 relay toggle for Moode Audio
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
@wigw
Copy link

wigw commented Jan 27, 2023

Hello. I tried your script, I did everything step by step, I got nothing. Maybe I did not copy the text of the script correctly (indents, tab) or there are no necessary app. Please help.

@michaelfdeberry
Copy link
Author

michaelfdeberry commented Jan 28, 2023

Hello. I tried your script, I did everything step by step, I got nothing. Maybe I did not copy the text of the script correctly (indents, tab) or there are no necessary app. Please help.

I have not used Moode in a while, it could be that the script is no longer compatible.

However, an easy way to tell if the issue is formatting is you can try to run the script. You can run the command ‘python relaytoggle.py’ from the directory where the file is saved. Fix any errors it reports.

The other thing I can think of off hand is to make sure you are using the BCM PIN number, and not the board PIN number.

@wigw
Copy link

wigw commented Jan 28, 2023

Hi. Thank you so much. Everything worked out. The problem is in the formatting of the document. See the screenshot.
relaytoggle py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment