76 lines
1.7 KiB
Python
76 lines
1.7 KiB
Python
##### SETUP
|
|
# # enable ssh at http://volumio.local/dev
|
|
#
|
|
# # fix sources https://forums.raspberrypi.com/viewtopic.php?t=320294#p1918001
|
|
# sudo apt-get update
|
|
#
|
|
# sudo apt-get install python3-dev python3-rpi.gpio
|
|
# sudo apt-get install python3-pip
|
|
# pip3 install python-socketio==4.6.1 # https://community.volumio.com/t/cannot-connect-to-volumio-using-pythons-socketio-client/67479/2
|
|
# # https://python-socketio.readthedocs.io/en/v4/client.html#creating-a-client-instance
|
|
#
|
|
# nano volumio-control.py # s.u.
|
|
#
|
|
# # wait 25 seconds for the API to come online
|
|
# sudo nano /etc/rc.local
|
|
# # su volumio -c 'sleep 25 && python3 /home/volumio/volumio-control.py >> /home/volumio/volumio-control.log 2>&1 &'
|
|
#
|
|
# sudo reboot
|
|
#
|
|
# ps aux | grep python
|
|
#####
|
|
|
|
|
|
import RPi.GPIO as GPIO
|
|
import socketio
|
|
|
|
GPIO.setmode(GPIO.BOARD)
|
|
|
|
sio = socketio.Client()
|
|
sio.connect('http://localhost:3000')
|
|
|
|
status = 'pause'
|
|
|
|
def button_pressed(channel):
|
|
if channel == 19:
|
|
print('prev')
|
|
sio.emit('prev')
|
|
elif channel == 21:
|
|
# print('state', status)
|
|
if status == 'play':
|
|
print('pause')
|
|
sio.emit('pause')
|
|
else:
|
|
print('play')
|
|
sio.emit('play')
|
|
elif channel == 23:
|
|
print('next')
|
|
sio.emit('next')
|
|
|
|
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 [19, 21, 23]:
|
|
setup_channel(x)
|
|
|
|
|
|
@sio.on('pushState')
|
|
def on_push_state(*args):
|
|
global status
|
|
status = args[0]['status']
|
|
print(status)
|
|
|
|
# get initial state
|
|
sio.emit('getState')
|
|
|
|
try:
|
|
while True : pass
|
|
except:
|
|
GPIO.cleanup()
|