-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrekel_LED.py
74 lines (62 loc) · 2.39 KB
/
brekel_LED.py
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
import socket
import select
import board
import neopixel
import socket
def get_hostname_value():
hostname = socket.gethostname()
prefix = "raspicam"
if hostname.startswith(prefix):
number_part = hostname[len(prefix):]
if not number_part:
return 0
try:
number = int(number_part)
if 0 <= number <= 9:
return number
else:
return -1
except ValueError:
return -1
else:
return -1
def listen_for_broadcasts(port=9875):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.bind(('', port))
sock.setblocking(0)
device_id = get_hostname_value();
print(f"device_id: {device_id}")
print(f"Listening for broadcast packets on port {port}...")
pixels = neopixel.NeoPixel(board.D18, 12, pixel_order=neopixel.GRBW)
pixels.brightness = 0.02
pixels.fill((0, 0, 0, 255))
try:
while True:
readable, _, _ = select.select([sock], [], [], 1.0)
if readable:
message, addr = sock.recvfrom(1024)
message = message.decode()
if message.startswith("BREKEL_START\t") and message.endswith("\tBREKEL_END"):
parts = message.split('\t')
if len(parts) == 8 and parts[0] == "BREKEL_START" and parts[7] == "BREKEL_END":
try:
red = int(parts[1])
green = int(parts[2])
blue = int(parts[3])
white = int(parts[4])
brightness = float(parts[5])
device = int(parts[6])
if device == device_id:
print(f"Received RGBW: ({red}, {green}, {blue}, {white}), Brightness: {brightness}")
pixels.brightness = brightness
pixels.fill((red, green, blue, white))
except ValueError as e:
print(f"Error parsing values: {e}")
except KeyboardInterrupt:
print("Program stopped by user.")
finally:
sock.close()
if __name__ == "__main__":
listen_for_broadcasts()