Run a command whenever Volumio Plays on Raspberry Pi

Hi ya’ll

is it possible to run a command instantly whenever volumio plays something or even pauses/stops playing something?

Like I am looking at running a command (for e.g. stop blinking led) that instantly would trigger whenever volumio plays and stops/pauses.

Thanks!

EDIT: More clarification:

I want to control two different gpio pins (these two pins will be set as OUTPUT) through volumio. Whenever I press play or is playing a song on volumio, I want a led (wired into the gpio) to turn on and whenever its not playing (either stopped or paused) - it turns off.

1 Like

Or use one of the GPIO plugins :slight_smile:

You could have a look at the amps witch plugin. It triggers gpio pins, but could easily be modified to execute commands.

What is it that you want to do?

Hi, thanks for the reply.

I want to control two different gpio pins (these two pins will be set as OUTPUT) through volumio. Whenever I press play or is playing a song on volumio, I want a led (wired into the gpio) to turn on and whenever its not playing (either stopped or paused) - it turns off.

You can use python to monitor the output (better use a websocket, as volumio already send a communication on play/stop/… )

This is untested, so please modify to your needs:

import os
import subprocess
import json

from time import sleep
import RPi.GPIO as GPIO

# Which GPIO
led =23

GPIO.setmode(GPIO.BCM)
GPIO.setup(led,GPIO.OUT)

while True:
	process = subprocess.Popen("volumio status".split(), stdout=subprocess.PIPE)
	output = process.communicate()[0]
	pythonObj = json.loads(output)
	status = pythonObj['status']

	if (status == "play") :
		# Execute code for play
		GPIO.output(led,GPIO.HIGH)
	if (status == "stop") :
		# Execute code for stop	
		GPIO.output(led,GPIO.LOW)
	sleep(2)
1 Like

Hi mate,

Thanks for the reply. Would this be instant? basically i want it to light up as soon as I hit “play”

Thanks for the reply,

I have tried to install it, but it didnt go past the “downloading .zip” section of the install. I am using volumio 3.

Update!

I have installed volumio 2 - just so I could access the older plugins.

I got my fingers dipped into the GPIO control plugin, however, it turns out I need to essentially turn a GPIO pin high and then low with a delay of 1 second in between. I am trying to simulate a button press…

Example:
I click play on song
GPIO Control turns GPIO pin 10 high
delay 1 second
GPIO Control turns GPIO pin 10 low

I think you should make clear what you want, as the requirements change in regards to the opening post.

Check out the ampswitch plugin, I added a delay function to it a while back…
But it would seem what you seek is a toggle function, this is also included via a pulse for latching relays.

yes you are right

I just happened to read over a certain component’s data sheet and I have seemed to misread it originally. sorry.

am I able to apply it to two different pins?

gpio pin 12 and 13 for example, independent from each other?

import os
import subprocess
import json

from time import sleep
import RPi.GPIO as GPIO

# Which GPIO
leda =23
ledb =24

# Set active state to avoid loop
playing = 0
GPIO.setmode(GPIO.BCM)
GPIO.setup(leda,GPIO.OUT)
GPIO.setup(ledb,GPIO.OUT)

while True:
	process = subprocess.Popen("volumio status".split(), stdout=subprocess.PIPE)
	output = process.communicate()[0]
	pythonObj = json.loads(output)
	status = pythonObj['status']

	if ((status == "play") and (playing == 0)) :
		# Execute code for play
		GPIO.output(leda,GPIO.HIGH)
        sleep(1)
		GPIO.output(leda,GPIO.LOW)
        playing = 1
	if ((status == "stop") and (playing == 1)) :
		# Execute code for stop	
		GPIO.output(ledb,GPIO.HIGH)
        sleep(1)
		GPIO.output(ledb,GPIO.LOW)
        playing = 0
	sleep(0.3) # To avoid CPU overload

You could switch to reading the socket events to avoid direct polling…

I don’t believe so. Why don’t you just use one gpio or some other hardware solution?
Else you could also just hard code it into the plugin…

Already mention that in my first reply on this topic.

I’m looking at upgrading the plugin to v3 but am having issues with node finding the onoff p library for some reason :confused::persevere::disappointed:

thanks for creating that plugin for v2!

1 Like

import os
import subprocess
import json

from time import sleep
import RPi.GPIO as GPIO

# Which GPIO
leda =24
ledb =23

# Set active state to avoid loop
playing = 0
GPIO.setmode(GPIO.BCM)
GPIO.setup(leda,GPIO.OUT)
GPIO.setup(ledb,GPIO.OUT)


while True:
        process = subprocess.Popen("volumio status".split(), stdout=subprocess.PIPE)
        output = process.communicate()[0]
        pythonObj = json.loads(output)
        status = pythonObj['status']

        if ((status == "play") and (playing == 0)) :
                # Execute code for play
                GPIO.output(leda,GPIO.HIGH)
                sleep(1)
                GPIO.output(leda,GPIO.LOW)
                playing = 1
        if ((status == "stop") or (status == "pause")  and (playing == 1)) :
                # Execute code for stop
                GPIO.output(ledb,GPIO.HIGH)
                sleep(0.5)
                GPIO.output(ledb,GPIO.LOW)
                playing = 0

        sleep(0.3) # To avoid CPU overload

This worked brilliantly, thanks!

I slightly modified it to include the “pause” status however, I was wondering if there is a way to add like a type of delay in which the script sees the “pause” status in volumio then waits five seconds just to see if the status changes and if it doesnt - it blinks the led but if it does, nothing happens.

something like:

if ((status == "stop") or (status == "pause" for 5 seconds)  and (playing == 1)) :
....
1 Like