The following warnings occurred:
Warning [2] count(): Parameter must be an array or an object that implements Countable - Line: 906 - File: showthread.php PHP 7.2.34 (Linux)
File Line Function
/showthread.php 906 errorHandler->error



Thank you for your donation!


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


Instruction Guide Use hardware momentary switches to control moOde
#1
Hardware momentary switches can be used to control the play, pause, next, previous functions of moode. They can also be used to power off and to launch a playlist.
On the hardware side, you set your hardware button to connect the ground pin of your RPi with a GPIO pin of your choice. Have a look here to see how to select the gpio pins.
Then you have to open a ssh session.

Create a new directory called "hwbuttons"
Code:
mkdir hwbuttons

Go to the new directory
Code:
cd hwbuttons

Open nano text editor
Code:
nano

Paste the following python script. This python script include the following buttons:

Toggle play pause: GPIO 12
Previous: GPIO 10
Next: GPIO 11
Volume up: GPIO 20
Volume down: GPIO 26
Power off: GPIO 9
Launch playlist "JAZZ_STREAM": GPIO 16
Launch playlist "MUSIC_STREAM": GPIO 13

It works for radio stations and music files. Make sure no spaces on playlists names.
If you are not interested in a particular button, mark it with # everywhere it appears.

Code:
#!/usr/bin/python
#script for moode audio GPIO buttons
#not sure this is needed:
#sudo apt-get update
#sudo apt-get install python-dev
#sudo apt-get install python-rpi.gpio
#on playlist names no spaces

#import
import RPi.GPIO as GPIO
import sys
import time
import os
import subprocess

#set GPIO mode
GPIO.setmode(GPIO.BCM)

#Define your GPIO pins
SW_PREV = 10
SW_NEXT = 11
SW_PLAY = 12
SW_VOLUP = 20
SW_VOLDOWN = 26
SW_POWEROFF = 9
SW_PL1 = 16
SW_PL2 = 13


#code to manage BUTTONS
GPIO.setup(SW_PREV,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_NEXT,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_PLAY,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_VOLUP,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_VOLDOWN,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_POWEROFF,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_PL1,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_PL2,GPIO.IN, pull_up_down=GPIO.PUD_UP)
while True:
  try:
     prev_switch = GPIO.input(SW_PREV)
     next_switch = GPIO.input(SW_NEXT)
     play_switch = GPIO.input(SW_PLAY)
     volup_switch = GPIO.input(SW_VOLUP)
     voldown_switch = GPIO.input(SW_VOLDOWN)
     poweroff_switch = GPIO.input(SW_POWEROFF)
     pl1_switch = GPIO.input(SW_PL1)
     pl2_switch = GPIO.input(SW_PL2)
     if prev_switch == False:
        subprocess.call(['mpc', 'prev' ])
        print "previous"
     elif next_switch == False:
        subprocess.call(['mpc', 'next' ])
        print "next"
     elif poweroff_switch == False:
        subprocess.call(['mpc', 'stop' ])
        subprocess.call(['sudo', 'poweroff' ])
        print "shutting down"
     elif volup_switch == False:
        subprocess.call(['/var/www/vol.sh','up','2' ])
        print "volumeup"
     elif voldown_switch == False:
        subprocess.call(['/var/www/vol.sh','dn','2' ])
        print "volumedown"
     elif pl1_switch == False:
        subprocess.call(['mpc','clear' ])
        subprocess.call(['mpc','load','JAZZ_STREAM' ])
        subprocess.call(['mpc','play' ])
     elif pl2_switch == False:
        subprocess.call(['mpc','clear' ])
        subprocess.call(['mpc','load','MUSIC_STREAM' ])
        subprocess.call(['mpc','play' ])
     elif play_switch == False:
        subprocess.call(['mpc', 'toggle' ])
        print "play"
     time.sleep(0.5)

  except KeyboardInterrupt:
     print "\nExit"
     GPIO.setwarnings(False)
     GPIO.cleanup()
     sys.exit(0)
#End of program

ctrl-x and save the file as "buttons.py"

To set the script to run every time you launch moode
Code:
sudo nano /etc/rc.local


Add the line below just before the exit 0
Code:
sudo python /home/pi/hwbuttons/buttons.py &

It works on my moode streamer. I did put this together from information I found on the volumio forum and from help I received from Kent (aka theoldpresbyope). It is possible that some of the lines in the script are not needed.

Remy
Reply


Messages In This Thread
Use hardware momentary switches to control moOde - by remy1961 - 05-20-2018, 03:05 PM

Forum Jump: