Skip to content

Commit b9eff77

Browse files
authored
Merge branch 'master' into pr-watch
2 parents 4ecf3ea + 966d2af commit b9eff77

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

BLE_Client_Server/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Adafruit Learning System tutorial - A CircuitPython BLE Client & Server
2+
3+
Use Bluetooth to turn a server on and off and actively sense the server's status light
4+
5+
Follow the Adafruit learn guide here: https://learn.adafruit.com/circuitpython-ble-client-server/
6+
7+
Adafruit invests time and resources providing this open source code,
8+
please support Adafruit and open-source hardware by purchasing
9+
products from [Adafruit](https://www.adafruit.com)!
10+
11+
MIT license, project and code by rdagger
12+
13+
All text above, and the splash screen below must be included in any redistribution
14+
15+
-----------------------
16+
If you are looking to make changes/additions, please use the GitHub Issues and Pull Request mechanisms.

BLE_Client_Server/server.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from adafruit_ble.uart_server import UARTServer
2+
from adafruit_bluefruit_connect.packet import Packet
3+
from adafruit_bluefruit_connect.button_packet import ButtonPacket
4+
from adafruit_bluefruit_connect.color_packet import ColorPacket
5+
from board import A0, D13
6+
from analogio import AnalogIn
7+
from digitalio import DigitalInOut, Direction
8+
from time import sleep
9+
10+
led = AnalogIn(A0) # Initialize blue LED light detector
11+
12+
solenoid = DigitalInOut(D13) # Initialize solenoid
13+
solenoid.direction = Direction.OUTPUT
14+
solenoid.value = False
15+
16+
uart_server = UARTServer()
17+
18+
while True:
19+
uart_server.start_advertising() # Advertise when not connected.
20+
21+
while not uart_server.connected: # Wait for connection
22+
pass
23+
24+
while uart_server.connected: # Connected
25+
if uart_server.in_waiting: # Check BLE commands
26+
packet = Packet.from_stream(uart_server)
27+
if isinstance(packet, ButtonPacket):
28+
if packet.button == '1' and packet.pressed:
29+
solenoid.value = True # Activate solenoid for 1 second
30+
sleep(1)
31+
solenoid.value = False
32+
33+
led_intensity = led.value # Check blue LED detector intensity
34+
led_on = led_intensity > 1000
35+
# Color: red = off, green = on
36+
color_packet = ColorPacket((255 * int(not led_on), 255 * led_on, 0))
37+
try:
38+
uart_server.write(color_packet.to_bytes()) # Transmit state color
39+
except OSError:
40+
pass
41+
42+
sleep(.2)

0 commit comments

Comments
 (0)