Reconnect after exception (nRF52840)

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
elhari1
 
Posts: 6
Joined: Thu Apr 28, 2022 11:19 am

Reconnect after exception (nRF52840)

Post by elhari1 »

Hello everyone.
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:
from asyncio.TimeoutError bleio.exceptions.BluetoothError: Failed to connect: timeout
and
bleak.exc.BleakError: Device with address ... was not found
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.

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)
and on my Jetson Nano:

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)
The part running on the Jetson gets started as a Thread without any while condition or anything similar.

I appreciate any help! Thank you!

User avatar
DJDevon3
 
Posts: 210
Joined: Wed Mar 06, 2019 11:02 am

Re: Reconnect after exception (nRF52840)

Post by DJDevon3 »

There’s a reset button on the feather board. You can wire up an external rest switch too. Reset always reloads the program.

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”