Thank you for your donation!


Cloudsmith graciously provides open-source package management and distribution for our project.


Problem: Python scripting - run script when moode plays/stops
#1
Hi,
please could you provide guidance how to call a custom python script when moode plays music as well another one that would be called when music stops? - I want to control a relay by the RPI GPIO that would be switching the amplifier on and off.

Thanks,
Jakub
Reply
#2
Here's how I do it. Two files:

autoamp.sh
autoamp.service

Put the first in your home directory. Put the second in /etc/systemd/system/

Then run

sudo systemctl enable autoamp.service
sudo systemctl daemon-reload
sudo systemctl start autoamp.service

The 1st file, which I borrowed from someone (thanks!), polls the /proc/asound/card0/pcm0p/sub0/status virtual file (this works for my set-up, you might need to alter which file it polls depending on yours) for the text "RUNNING". This only appears when sound is playing. If it finds it, then it changes the state of your specified GPIO pin (in my case 23). If it doesn't find it, then no change. If it did find it, but then it doesn't anymore, then the state is changed as well. There's a counter in there for a delay on this change.

Hope this helps.


Attached Files
.zip   autoamp.zip (Size: 1.04 KB / Downloads: 17)
Reply
#3
(02-26-2019, 10:28 AM)tristanc Wrote: Here's how I do it. Two files:

autoamp.sh
autoamp.service

Put the first in your home directory. Put the second in /etc/systemd/system/

Then run

sudo systemctl enable autoamp.service
sudo systemctl daemon-reload
sudo systemctl start autoamp.service

The 1st file, which I borrowed from someone (thanks!), polls the /proc/asound/card0/pcm0p/sub0/status virtual file (this works for my set-up, you might need to alter which file it polls depending on yours) for the text "RUNNING". This only appears when sound is playing. If it finds it, then it changes the state of your specified GPIO pin (in my case 23). If it doesn't find it, then no change. If it did find it, but then it doesn't anymore, then the state is changed as well. There's a counter in there for a delay on this change.

Hope this helps.

Thanks! Great, this works and I especially like that it is independent way how to control the relay (on moOde or anything else). 

You may use the cat to help you show what the sound card output is 

cat /proc/asound/card0/pcm0p/sub0/

or other file, depending on the sound card that you are using.


If others follow this procedure, you will most likely further need to make the script executable (first line below) or even run it always as root - second line (GPIO needs to be called as root, or at least it is the simplest way that I found to do that):

sudo chmod +x autoamp.sh
sudo chmod +s autoamp.sh

Have fun!
Reply


Forum Jump: