Skip to content

Update UART example #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 14, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions examples/bluefruitconnect_uart.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
# Basic example for using the BLE Connect UART
# To use, start this program, and start the Adafruit Bluefruit LE Connect app.
# Connect, and then select UART
# Connect, and then select UART. Any text received FROM the connected device
# will be displayed. Periodically, text is sent TO the connected device.

import time
from adafruit_ble import BLERadio
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.nordic import UARTService

SEND_RATE = 10 # how often in seconds to send text

ble = BLERadio()
uart_server = UARTService()
advertisement = ProvideServicesAdvertisement(uart_server)

count = 0
while True:
print("WAITING...")
# Advertise when not connected.
Expand All @@ -22,12 +27,21 @@
print("CONNECTED")

# Loop and read packets
last_send = time.monotonic()
while ble.connected:
# INCOMING (RX) check for incoming text
if uart_server.in_waiting:
raw_bytes = uart_server.read(uart_server.in_waiting)
text = raw_bytes.decode().strip()
print("raw bytes =", raw_bytes)
print("text =", text)
# print("raw bytes =", raw_bytes)
print("RX:", text)
# OUTGOING (TX) periodically send text
if time.monotonic() - last_send > SEND_RATE:
text = "COUNT = {}\r\n".format(count)
print("TX:", text.strip())
uart_server.write(text.encode())
count += 1
last_send = time.monotonic()

# Disconnected
print("DISCONNECTED")