-
Notifications
You must be signed in to change notification settings - Fork 781
/
Copy pathserver.py
49 lines (38 loc) · 1.65 KB
/
server.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
from time import sleep
from adafruit_ble import BLERadio
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.nordic import UARTService
from adafruit_bluefruit_connect.packet import Packet
from adafruit_bluefruit_connect.button_packet import ButtonPacket
from adafruit_bluefruit_connect.color_packet import ColorPacket
from board import A0, D13
from analogio import AnalogIn
from digitalio import DigitalInOut, Direction
led = AnalogIn(A0) # Initialize blue LED light detector
solenoid = DigitalInOut(D13) # Initialize solenoid
solenoid.direction = Direction.OUTPUT
solenoid.value = False
ble = BLERadio()
uart_service = UARTService()
advertisement = ProvideServicesAdvertisement(uart_service)
while True:
ble.start_advertising(advertisement) # Advertise when not connected.
while not ble.connected: # Wait for connection
pass
while ble.connected: # Connected
if uart_service.in_waiting: # Check BLE commands
packet = Packet.from_stream(uart_service)
if isinstance(packet, ButtonPacket):
if packet.button == '1' and packet.pressed:
solenoid.value = True # Activate solenoid for 1 second
sleep(1)
solenoid.value = False
led_intensity = led.value # Check blue LED detector intensity
led_on = led_intensity > 1000
# Color: red = off, green = on
color_packet = ColorPacket((255 * int(not led_on), 255 * led_on, 0))
try:
uart_service.write(color_packet.to_bytes()) # Transmit state color
except OSError:
pass
sleep(.2)