Skip to content

Commit 4ca25f1

Browse files
authored
Merge pull request #21 from caternuson/example_update
Update UART example
2 parents 32dbf82 + ce4589d commit 4ca25f1

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

examples/bluefruitconnect_uart.py

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
# Basic example for using the BLE Connect UART
22
# To use, start this program, and start the Adafruit Bluefruit LE Connect app.
3-
# Connect, and then select UART
3+
# Connect, and then select UART. Any text received FROM the connected device
4+
# will be displayed. Periodically, text is sent TO the connected device.
45

6+
import time
57
from adafruit_ble import BLERadio
68
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
79
from adafruit_ble.services.nordic import UARTService
810

11+
SEND_RATE = 10 # how often in seconds to send text
12+
913
ble = BLERadio()
1014
uart_server = UARTService()
1115
advertisement = ProvideServicesAdvertisement(uart_server)
1216

17+
count = 0
1318
while True:
1419
print("WAITING...")
1520
# Advertise when not connected.
@@ -22,12 +27,21 @@
2227
print("CONNECTED")
2328

2429
# Loop and read packets
30+
last_send = time.monotonic()
2531
while ble.connected:
32+
# INCOMING (RX) check for incoming text
2633
if uart_server.in_waiting:
2734
raw_bytes = uart_server.read(uart_server.in_waiting)
2835
text = raw_bytes.decode().strip()
29-
print("raw bytes =", raw_bytes)
30-
print("text =", text)
36+
# print("raw bytes =", raw_bytes)
37+
print("RX:", text)
38+
# OUTGOING (TX) periodically send text
39+
if time.monotonic() - last_send > SEND_RATE:
40+
text = "COUNT = {}\r\n".format(count)
41+
print("TX:", text.strip())
42+
uart_server.write(text.encode())
43+
count += 1
44+
last_send = time.monotonic()
3145

3246
# Disconnected
3347
print("DISCONNECTED")

0 commit comments

Comments
 (0)