For a project I'm working on, I'm using a Adafruit Feather nRF52840 Express. I basically transmit data to the nRF52840 via UART and send them to a Jetson Nano with BLE. During the past weeks while doing some tests, the following error messages appeared quite frequently:
andfrom asyncio.TimeoutError bleio.exceptions.BluetoothError: Failed to connect: timeout
Is there a way to reset the program/nRF52840 and retry to establish the connection after such an exception? Until now I manually had to restart the program but this won't be possible if the applications runs without a monitor attached to the jetson.bleak.exc.BleakError: Device with address ... was not found
Here's the code running on my nRF52840:
Code: Select all
import board
import time
import busio
# configuration
SERVICE_NAME = "CIRCUITPY070d"
# setup UART
uart = busio.UART(board.TX, board.RX)
# setup bluetooth
from adafruit_bluefruit_connect.packet import Packet
from adafruit_bluefruit_connect.color_packet import ColorPacket
from adafruit_ble import BLERadio
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.nordic import UARTService
ble = BLERadio()
ble.name = "CIRCUITPY070d" # My UART-GNSS board
uart_service = UARTService()
advertisement = ProvideServicesAdvertisement(uart_service)
advertisement.short_name = SERVICE_NAME
was_connected = False
while True:
# Advertise BLE when not connected.
if not ble.connected:
was_connected = False
if not ble.advertising:
print(f'Start advertising as "{SERVICE_NAME}"')
ble.start_advertising(advertisement, interval=0.5, timeout=5)
# dismiss uart data when not connected
if uart.in_waiting: # otherwise: if nbytes := uart.in_waiting:
uart.reset_input_buffer()
else:
if not was_connected:
was_connected = True
print("Connected")
ble.stop_advertising()
# send uart data when connected
if nbytes := uart.in_waiting:
# data = uart.read(nbytes)
data = uart.readline()
if data:
print("Broadcasting", data)
uart_service.write(data)
time.sleep(0.1)
Code: Select all
from adafruit_ble import BLERadio
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.nordic import UARTService
import time
# Establish BLE connection
SERVICE_NAME = "CIRCUITPY070d"
ble = BLERadio()
uart_connection = None
while True:
if not uart_connection or not uart_connection.connected:
print(f'Trying to connect to "{SERVICE_NAME}"')
for adv in ble.start_scan(ProvideServicesAdvertisement):
if UARTService in adv.services and adv.short_name == SERVICE_NAME:
uart_connection = ble.connect(adv)
print("Connected!")
break
ble.stop_scan()
if uart_connection and uart_connection.connected:
uart_service = uart_connection[UARTService]
print("data dummy")
if uart_BANNED_waiting:
data = uart_service.readline().decode("utf-8").strip()
if data:
print(data)
time.sleep(0.1)
I appreciate any help! Thank you!