Code for automatic reconnect

Firmware/software/electronics/mechanics
Post Reply
huizerd
Beginner
Posts: 9
Joined: Mon Aug 02, 2021 7:49 am

Code for automatic reconnect

Post by huizerd »

Hi all,

I'm working with the basic asynchronous logging example (https://github.com/bitcraze/crazyflie-l ... asiclog.py) and trying to 1) keep trying to make an initial connection until it finds a Crazyflie, and 2) keep trying to reconnect if the logger has lost connection to a previously connected Crazyflie. I got 1) to work by just adding a while loop in the __init__ of the logger and closing and opening the link until it connects, but I'm not sure how to achieve 2). Is there any chance somebody of you has worked on this before? Thanks!

Jesse
jonasdn
Expert
Posts: 132
Joined: Mon Mar 01, 2021 3:13 pm

Re: Code for automatic reconnect

Post by jonasdn »

Hi Jesse!

You could try to work with the callbacks that are there! Everytime you disconnect from a Crazyflie the function ...

Code: Select all

def _disconnected(self, link_uri):
        """Callback when the Crazyflie is disconnected (called in all cases)"""
        print('Disconnected from %s' % link_uri)
        self.is_connected = False
... will be called so you can take action to reconnect.
huizerd
Beginner
Posts: 9
Joined: Mon Aug 02, 2021 7:49 am

Re: Code for automatic reconnect

Post by huizerd »

Hi Jonas,

Thanks! Using the callbacks gave me recursive reconnection attempts, but creating a separate reconnect function that is called in an outside loop worked for me.
Post Reply