Files
piplayer/code/volumio-control.py
T
2024-08-20 12:51:46 +00:00

83 lines
1.9 KiB
Python

#####
# # 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.
#
# sudo nano /etc/systemd/system/startup-commands.service
# # [Unit]
# # After = network-online.target
# # Wants = network-online.target
# #
# # [Service]
# # Type = oneshot
# # RemainAfterExit = yes
# # ExecStart = 'python3 /home/volumio/volumio-control.py >> /home/volumio/volumio-control.log 2>&1 &'
# #
# # [Install]
# # WantedBy = multi-user.target
#
# systemctl enable startup-commands.service
#####
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 == 11:
print('next')
sio.emit('next')
elif channel == 13:
# print('state', status)
if status == 'play':
print('pause')
sio.emit('pause')
else:
print('play')
sio.emit('play')
elif channel == 15:
print('prev')
sio.emit('prev')
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)
@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()