2

I'm trying to connect to DroneKit-SITL and keeping an MQTT-Broker running with loop_forever(). Whenever I receive a message over MQTT, I want to send commands to the drone via the dronekit python API. At the same time, I want to send drone data to other clients over MQTT every 5 seconds.

How do I let my dronekit-sitl-class and the mqtt-broker-class interact with each other? Should each of them run in a thread or a process?

2 Answers2

1

I fixed the problem by running the loop in a separate thread like this:

clientloop_thread = Thread(target=self.connect)
clientloop_thread.start()

def connect(self):
    self._client.loop_forever()
  • I suggest you to set daemon=True. Because if you kill the process, the thread will keep on going and you will have problem with Error 98: Address already in use – Martin Mar 11 '19 at 15:02
1

better solution is use loop_start() and loop_stop() instead of loop_forever()

self._client.loop_start() # its start loop in new thread
  • I wouldn't say it's better, but it's an alternative. it depends on the case if you want blocking or non-blocking behavior. You can also use manually looping with `client.loop(.1) #blocks for 100ms` – tturbo Feb 24 '23 at 14:58