Skip to content

Instantly share code, notes, and snippets.

@ivesdebruycker
Created November 22, 2016 21:51
Show Gist options
  • Save ivesdebruycker/4b08bdd5415609ce95e597c1d28e9b9e to your computer and use it in GitHub Desktop.
Save ivesdebruycker/4b08bdd5415609ce95e597c1d28e9b9e to your computer and use it in GitHub Desktop.
Control Volumio2 with GPIO buttons
# https://volumio.org/forum/gpio-pins-control-volume-t2219.html
# https://pypi.python.org/pypi/socketIO-client
# https://volumio.github.io/docs/API/WebSocket_APIs.html
import RPi.GPIO as GPIO
import time
import subprocess
from socketIO_client import SocketIO, LoggingNamespace
GPIO.setmode(GPIO.BOARD)
socketIO = SocketIO('localhost', 3000)
status = 'pause'
def button_pressed(channel):
if channel == 13:
print 'next'
socketIO.emit('next')
elif channel == 15:
print 'random'
socketIO.emit('replaceAndPlay', {"uri":"live_playlists_random_50", "title":"50 random tracks", "service":"live_playlists"})
elif channel == 11:
print 'play/pause'
print('state', status)
if status == 'play':
socketIO.emit('pause')
else:
socketIO.emit('play')
else:
print "unknown button", channel
def setup_channel(channel):
try:
print "register %d" %channel
GPIO.setup(channel, GPIO.IN, GPIO.PUD_UP)
GPIO.add_event_detect(channel, GPIO.FALLING, callback = button_pressed, bouncetime = 400)
print 'success'
except (ValueError, RuntimeError) as e:
print 'ERROR:', e
for x in [11, 13, 15]:
setup_channel(x)
def on_push_state(*args):
# print('state', args)
global status
status = args[0]['status'].encode('ascii', 'ignore')
print status
socketIO.on('pushState', on_push_state)
# get initial state
socketIO.emit('getState', '', on_push_state)
try:
socketIO.wait()
except KeyboardInterrupt:
pass
GPIO.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment