Get updated status from Volumio WebUI

I’m trying to implement a way of changing an RGB LED according to which state the player is in.

I have the code below working on physical buttons but as soon as I involve the WebUI(to play/pause) it doesn’t update the LED.

Is what I want to do even possible?

(WARNING: I’ve pulled code from multiple places and I know it could be better written and commented and just generally cleaned up!)

[code]#!/usr/bin/env python

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 subprocess
from socketIO_client import SocketIO, LoggingNamespace
from rotary_class1 import RotaryEncoder
from RPLCD_i2c import CharLCD
from time import sleep

lcd = CharLCD(address=0x27, port=1, cols=20, rows=4, dotsize=8)

#define pins for rotary encoder
PIN_A = 8
PIN_B = 11

GPIO.setmode(GPIO.BCM)
socketIO = SocketIO(‘localhost’, 3000)
status = ‘stop’

#set RGB LED GPIO numbers
redPin = 19
greenPin = 13
bluePin = 12

def blink(pin):
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.HIGH)

def turnOff(pin):
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.LOW)

def redOn():
blink(redPin)

def redOff():
turnOff(redPin)

def greenOn():
blink(greenPin)

def greenOff():
turnOff(greenPin)

def blueOn():
blink(bluePin)

def blueOff():
turnOff(bluePin)

def button_pressed(channel):
if channel == 5:
print ‘next’
socketIO.emit(‘next’)
elif channel == 6:
print ‘previous’
socketIO.emit(‘prev’)
elif channel == 27:
print ‘play/pause’
print(‘state’, status)
if status == ‘play’:
socketIO.emit(‘pause’)
blueOff()
redOff()
greenOn()
elif status == ‘pause’:
socketIO.emit(‘play’)
greenOff()
redOff()
blueOn()
elif status == ‘stop’:
socketIO.emit(‘play’)
greenOff()
blueOff()
redOn()
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

def on_push_state(*args):
# print(‘state’, args)
global status
status = args[0][‘status’].encode(‘ascii’, ‘ignore’)
print status

def switch_event(event):
if event == RotaryEncoder.CLOCKWISE:
# getstate from websocket and determine volume
socketIO.emit(‘volume’, ‘+’)
print ‘vol+’
time.sleep(.1)
elif event == RotaryEncoder.ANTICLOCKWISE:
# getstate from websocket and determine volume
socketIO.emit(‘volume’, ‘-’)
print ‘vol-’
time.sleep(.1)
return

for x in [5,6,27]:
setup_channel(x)

rswitch = RotaryEncoder(PIN_A,PIN_B,switch_event)
socketIO.on(‘pushState’, on_push_state)

get initial state

socketIO.emit(‘getState’, ‘’, on_push_state)

try:
socketIO.wait()
except KeyboardInterrupt:
pass

GPIO.cleanup()
[/code]