|
1 | 1 | # Basic example for using the BLE Connect UART |
2 | 2 | # 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. |
4 | 5 |
|
| 6 | +import time |
5 | 7 | from adafruit_ble import BLERadio |
6 | 8 | from adafruit_ble.advertising.standard import ProvideServicesAdvertisement |
7 | 9 | from adafruit_ble.services.nordic import UARTService |
8 | 10 |
|
| 11 | +SEND_RATE = 10 # how often in seconds to send text |
| 12 | + |
9 | 13 | ble = BLERadio() |
10 | 14 | uart_server = UARTService() |
11 | 15 | advertisement = ProvideServicesAdvertisement(uart_server) |
12 | 16 |
|
| 17 | +count = 0 |
13 | 18 | while True: |
14 | 19 | print("WAITING...") |
15 | 20 | # Advertise when not connected. |
|
22 | 27 | print("CONNECTED") |
23 | 28 |
|
24 | 29 | # Loop and read packets |
| 30 | + last_send = time.monotonic() |
25 | 31 | while ble.connected: |
| 32 | + # INCOMING (RX) check for incoming text |
26 | 33 | if uart_server.in_waiting: |
27 | 34 | raw_bytes = uart_server.read(uart_server.in_waiting) |
28 | 35 | 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() |
31 | 45 |
|
32 | 46 | # Disconnected |
33 | 47 | print("DISCONNECTED") |
0 commit comments