-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathled_websocket.py
More file actions
78 lines (68 loc) · 2.61 KB
/
Copy pathled_websocket.py
File metadata and controls
78 lines (68 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import time
import board
import neopixel
import websocket
import json
# URL du serveur websocket sur la Raspberry Pi
server_url = "ws://172.20.10.2:8000/ws/swipes"
# Choix d'un pin ouvert connecté à l'entrée de données de la bande NeoPixel, par exemple board.D18
pixel_pin = board.D18
# Nombre de NeoPixels
num_pixels = 115
intensity = 55
switch = False
# Configuration des NeoPixels
pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.2, auto_write=False)
# Fonction appelée lorsque la connexion websocket est ouverte
def on_open(ws):
print("Connexion établie")
# Fonction appelée lors de la réception de données via websocket
def on_message(ws, message):
global intensity
global switch
try:
# Analyser les données JSON reçues
data = json.loads(message)
print("Données reçues:", data)
if data['hand'] is None:
if switch == True:
switch = False
if intensity > 55:
intensity = intensity - 50
pixels.fill((intensity, intensity, intensity))
pixels.show()
else:
if intensity < 255 and switch == False:
intensity = intensity + 50
if intensity == 255:
switch = True
if switch == True and intensity > 105:
intensity = intensity - 50
pixels.fill((intensity, intensity, intensity))
pixels.show()
if data['action'] == 'click':
pixels.fill((0, 255, 0))
pixels.show()
time.sleep(0.5)
pixels.fill((intensity, intensity, intensity))
pixels.show()
if data['action'] == 'up' or data['action'] == 'down' or data['action'] == 'left' or data['action'] == 'right' or data['action'] == 'up-left' or data['action'] == 'up-right' or data['action'] == 'down-left' or data['action'] == 'down-right':
pixels.fill((0, 0, 255))
pixels.show()
time.sleep(0.5)
pixels.fill((intensity, intensity, intensity))
pixels.show()
except Exception as e:
print("Erreur lors du traitement des données:", e)
# Fonction pour se connecter au serveur websocket
def connect_to_websocket():
print("Tentative de connexion au serveur websocket...")
ws = websocket.WebSocketApp(server_url, on_open=on_open, on_message=on_message)
ws.run_forever()
# Boucle pour essayer de se reconnecter toutes les 5 secondes
while True:
try:
connect_to_websocket()
except Exception as e:
print("Erreur lors de la connexion au serveur websocket:", e)
time.sleep(5)