Skip to content

Commit 485f605

Browse files
authored
Merge pull request #882 from adafruit/TheKitty-patch-106
Create client.py for BLE Client Server tutorial
2 parents 966d2af + 12910d9 commit 485f605

2 files changed

Lines changed: 90 additions & 9 deletions

File tree

BLE_Client_Server/client.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
from time import sleep
2+
from adafruit_ble.uart_client import UARTClient
3+
from adafruit_ble.scanner import Scanner
4+
from adafruit_bluefruit_connect.packet import Packet
5+
from adafruit_bluefruit_connect.button_packet import ButtonPacket
6+
from adafruit_bluefruit_connect.color_packet import ColorPacket
7+
from neopixel import NeoPixel
8+
from board import NEOPIXEL, SWITCH
9+
from adafruit_debouncer import Debouncer
10+
from digitalio import DigitalInOut, Direction, Pull
11+
import adafruit_fancyled.adafruit_fancyled as fancy
12+
13+
pin = DigitalInOut(SWITCH) # Set up built-in pushbutton switch
14+
pin.direction = Direction.INPUT
15+
pin.pull = Pull.UP
16+
switch = Debouncer(pin)
17+
18+
pixels = NeoPixel(NEOPIXEL, 1) # Set up built-in NeoPixel
19+
20+
AQUA = 0x00FFFF # (0, 255, 255)
21+
GREEN = 0x00FF00 # (0, 255, 0)
22+
ORANGE = 0xFF8000 # (255, 128, 0)
23+
RED = 0xFF0000 # (255, 0, 0)
24+
BLUE = 0x0000FF # (0, 0, 255)
25+
26+
gradients = {'Off': [(0.0, RED), (0.75, ORANGE)],
27+
'On': [(0.0, GREEN), (1.0, AQUA)]}
28+
palette = fancy.expand_gradient(gradients['Off'], 30)
29+
30+
gamma_levels = (0.25, 0.3, 0.15)
31+
color_index = 1
32+
fade_direction = 1
33+
34+
TARGET = 'a0:b4:c2:d0:e7:f2' # CHANGE TO YOUR BLE ADDRESS
35+
36+
button_packet = ButtonPacket("1", True) # Transmits pressed button 1
37+
38+
scanner = Scanner() # BLE Scanner
39+
uart_client = UARTClient() # BLE Client
40+
41+
while True:
42+
uart_addresses = []
43+
pixels[0] = BLUE # Blue LED indicates disconnected status
44+
pixels.show()
45+
46+
# Keep trying to find target UART peripheral
47+
while not uart_addresses:
48+
uart_addresses = uart_client.scan(scanner)
49+
for address in uart_addresses:
50+
if TARGET in str(address):
51+
uart_client.connect(address, 5) # Connect to target
52+
53+
while uart_client.connected: # Connected
54+
switch.update()
55+
if switch.fell: # Check for button press
56+
try:
57+
uart_client.write(button_packet.to_bytes()) # Transmit press
58+
except OSError:
59+
pass
60+
# Check for LED status receipt
61+
if uart_client.in_waiting:
62+
packet = Packet.from_stream(uart_client)
63+
if isinstance(packet, ColorPacket):
64+
if fancy.CRGB(*packet.color).pack() == GREEN: # Color match
65+
# Green indicates on state
66+
palette = fancy.expand_gradient(gradients['On'], 30)
67+
else:
68+
# Otherwise red indicates off
69+
palette = fancy.expand_gradient(gradients['Off'], 30)
70+
71+
# NeoPixel color fading routing
72+
color = fancy.palette_lookup(palette, color_index / 29)
73+
color = fancy.gamma_adjust(color, brightness=gamma_levels)
74+
c = color.pack()
75+
pixels[0] = c
76+
pixels.show()
77+
if color_index == 0 or color_index == 28:
78+
fade_direction *= -1 # Change direction
79+
color_index += fade_direction
80+
81+
sleep(0.02)

BLE_Client_Server/server.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1+
from time import sleep
12
from adafruit_ble.uart_server import UARTServer
23
from adafruit_bluefruit_connect.packet import Packet
34
from adafruit_bluefruit_connect.button_packet import ButtonPacket
45
from adafruit_bluefruit_connect.color_packet import ColorPacket
56
from board import A0, D13
67
from analogio import AnalogIn
78
from digitalio import DigitalInOut, Direction
8-
from time import sleep
9-
9+
1010
led = AnalogIn(A0) # Initialize blue LED light detector
11-
11+
1212
solenoid = DigitalInOut(D13) # Initialize solenoid
1313
solenoid.direction = Direction.OUTPUT
1414
solenoid.value = False
15-
15+
1616
uart_server = UARTServer()
17-
17+
1818
while True:
1919
uart_server.start_advertising() # Advertise when not connected.
20-
20+
2121
while not uart_server.connected: # Wait for connection
2222
pass
23-
23+
2424
while uart_server.connected: # Connected
2525
if uart_server.in_waiting: # Check BLE commands
2626
packet = Packet.from_stream(uart_server)
@@ -29,7 +29,7 @@
2929
solenoid.value = True # Activate solenoid for 1 second
3030
sleep(1)
3131
solenoid.value = False
32-
32+
3333
led_intensity = led.value # Check blue LED detector intensity
3434
led_on = led_intensity > 1000
3535
# Color: red = off, green = on
@@ -38,5 +38,5 @@
3838
uart_server.write(color_packet.to_bytes()) # Transmit state color
3939
except OSError:
4040
pass
41-
41+
4242
sleep(.2)

0 commit comments

Comments
 (0)