Thank you for your donation!


How to add GPIO Buttons
#1
Dears,

how would I be able to add GPIO buttons for next, previous track, random and pause? Sorry for this very newbie question but any support would be really appreciated. Many thanks in advance.

Best regards,
Calimero22
Reply
#2
This can be done, but will depend on how comfortable you are in working with electronics & a bit of programming.

On this website https://raspberrypihq.com/use-a-push-but...y-pi-gpio/ they explain how to capture events from GPIO using a python script. When a button is pushed the script just outputs "Button was pushed", but this can easily be modified so that it calls a url instead.

Looking at the Moode web api commands (like below) you can trigger these by using a couple of buttons

http://moode/command?cmd=play
http://moode/command?cmd=next
http://moode/command?cmd=stop
http://moode/command?cmd=vol.sh up 1
http://moode/command?cmd=vol.sh dn 1
http://moode/command?cmd=vol.sh mute

So, it can be done, but you'll need to get some buttons, wire them up and do some python coding.

Good Luck!
Reply
#3
Try the GPIO button handler. Its in the Local Services section of System Config.

   
Reply
#4
Nice, i hadn't seen that yet.
Makes things even easier!

Just to check: for the CMD I'd use for example "curl http://moode/command?cmd=play" is that right?
Reply
#5
The commands should be local for example:

Code:
mpc play
mpc pause
mpc next
mpc prev
/var/www/vol.sh up 1
/var/www/vol.sh dn 1
/var/www/vol.sh mute
sudo poweroff

This post has more information.
http://moodeaudio.org/forum/showthread.p...53#pid9153

-Tim
Reply
#6
Hello,

I've tried to use the GPIO handler, the vol.sh commands work but none of the mpc commands appear to be working. To check that I done it correctly, "mpc pause" without the "" for example should be entered in the cmd box? Entering "/var/www/vol.sh mute" in the cmd box mutes the player so I'm happy that there's no problem with the detecting the button press or the connection.

Paul
Reply
#7
The command strings should not have any quotes around them.

Code:
/var/www/vol.sh mute
/var/www/vol.sh up 1
/var/www/vol.sh dn 1
mpc play
mpc pause
.
.

I ran a quick test on a DAC that has a rotary encoder attached to GPIO pins 23, 24. 

- Set Rotary encoder to OFF in Audio config 
- Set GPIO button handler to OFF in System config
- In GPIO Config turn Button 1 ON and set it to pin = 23 and command = mpc stop
- From SSH terminal run the gpio button handler and note that it prints out the config for Button 1.

Code:
pi@rp3:~ $ sudo /var/www/command/gpio-buttons.py
2019-05-19 09:02:01 sw_1: pin=23, enabled=1, bounce_time=1000, cmd=mpc stop

- Start a radio station playing
- Turn the rotary encoder knob which will cause GPIO pin 23 to change state
- Observe that playback is now stopped and the message below from mpc appears in SSH terminal
- This confirms that the mpc command was executed when pin 23 changed state

Code:
volume: 18%   repeat: off   random: off   single: off   consume: off
 
-Tim
Reply
#8
Thanks Tim,

I have found the problem and really annoyed with myself for not noticing it.

Predictive text on my tablet was inserting a space after the mpc commands, deleted the trailing space solved it.

Thanks for your help.
Reply
#9
That explains it. I'll trim leading and trailing spaces from the command b4 its written to the cfg_gpio table.
Reply


Forum Jump: