From 5eba1d5654e47cf8d17eff5e4d109d5d6e9ffc0e Mon Sep 17 00:00:00 2001 From: caternuson Date: Thu, 14 May 2020 15:17:55 -0700 Subject: [PATCH 1/2] update UART example --- examples/bluefruitconnect_uart.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/examples/bluefruitconnect_uart.py b/examples/bluefruitconnect_uart.py index f5fbcb1..574ba9e 100644 --- a/examples/bluefruitconnect_uart.py +++ b/examples/bluefruitconnect_uart.py @@ -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. @@ -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") From ce4589de9b5403186a42a7c3d84cbd3c2373c63e Mon Sep 17 00:00:00 2001 From: caternuson Date: Thu, 14 May 2020 15:23:51 -0700 Subject: [PATCH 2/2] black --- examples/bluefruitconnect_uart.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/bluefruitconnect_uart.py b/examples/bluefruitconnect_uart.py index 574ba9e..5091592 100644 --- a/examples/bluefruitconnect_uart.py +++ b/examples/bluefruitconnect_uart.py @@ -8,7 +8,7 @@ from adafruit_ble.advertising.standard import ProvideServicesAdvertisement from adafruit_ble.services.nordic import UARTService -SEND_RATE = 10 # how often in seconds to send text +SEND_RATE = 10 # how often in seconds to send text ble = BLERadio() uart_server = UARTService() @@ -33,7 +33,7 @@ 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("raw bytes =", raw_bytes) print("RX:", text) # OUTGOING (TX) periodically send text if time.monotonic() - last_send > SEND_RATE: