GPIO amplifier control

Hi, I did my best to search this forum for answer with no success.

I’m trying to find a way how to turn on and off amplifier using raspberry’s GPIO. Hardware is no problem. But I would like to know if there is a way how to control GPIO based on Mopidy activity? Like linking status “is playing” to GPIO, or to have a set of scripts running when stream started/stopped.

I found there is complex API for Mopidy, but that is far beyond my skills.

Any ideas? Thank you.

I wouldn’t call the API complex, in fact I found it very easy to start with writing my own code :slight_smile: To run some code on player state changes just create a class deriving from mopidy.core.CoreListener and implement playback_state_changed method (docs: https://docs.mopidy.com/en/latest/api/core/#core-events).

Or You can take another aproach which I did and use pulseaudio events - this approach is better if You plan to use other audio sources on Your device (for example bluetooth playback or spotify connect). I wrote simple python script to handle this: https://github.com/mczerski/MyMusicBox/blob/master/usr/local/bin/speaker-on-off. If You use only alsa there is also a way to do same thing but You need to dig the internet for the solution.

2 Likes

Your pulse event solution is nice, not come across that before.

1 Like

Hi,

is there any foolproof guide how to implement it to PiMusicBox? You know… for a fool.

Thanks

Hm, yeah, I am making strongly use of mosquitto and my mosquitto-frontend (and openHab) and I do something similar.

It works like this, openhab turns on the speakers by a let’s say mosquitto-heartbeat-message.
Five minutes after last the heartbeat-message the speakers/amplifier are turned off.

So you have to send the heartbeat-message e.g. every minute to keep them turned on!

This is what my frontend does - it sends a message every 30 seconds when state is playing to keep the amplifier turned on.

Of course you don’t have to use openHab. A simple python script should do the job.

If you are interested in such a solution, let me know.

Nevertheless this is how my openHab config looks like:

rule "Server Speaker Mopidy Keep Alive"
when
    Item Mopidy_Speaker_Keep_Alive received update
then
    sendCommand(PCSpeakersServer,ON)
     logInfo("Online","Mopidy") 
end

rule "Server Speakers auto timer"
when
    Item PCSpeakersServer received command ON
then
    # set the timeout back to 5 mins
    serverSpeakersTimeout= 60 ; // 5 min --> 300 / 5secs = 60
    logInfo("SrvSpkrs","Started timeout ")
end

rule "Every 5 seconds"
when
    Time cron "0/5 0/1 * 1/1 * ? *"
then
    if (serverSpeakersTimeout > 0)
    {
        if (serverSpeakersTimeout==1)
        {
            sendCommand(PCSpeakersServer,OFF)
            logInfo("SrvSpkrs","Deactivated")	
        }	
        serverSpeakersTimeout = serverSpeakersTimeout - 1
    }
end