diff --git a/MAVProxy/modules/mavproxy_mqtt.py b/MAVProxy/modules/mavproxy_mqtt.py index 8213b27f05..54780a7ab8 100644 --- a/MAVProxy/modules/mavproxy_mqtt.py +++ b/MAVProxy/modules/mavproxy_mqtt.py @@ -1,17 +1,22 @@ from paho.mqtt import MQTTException from MAVProxy.modules.lib import mp_settings from MAVProxy.modules.lib import mp_module +import paho.mqtt import paho.mqtt.client as mqtt import json import numbers -import sys + +# TODO MQTTv3 vs MQTTv5 class MqttModule(mp_module.MPModule): def __init__(self, mpstate): super(MqttModule, self).__init__(mpstate, "mqtt", "mqtt publisher") - self.connected = False - self.client = mqtt.Client() + if paho.mqtt.__version__[0] > '1': + self.client = mqtt.Client(callback_api_version=mqtt.CallbackAPIVersion.VERSION1) # TODO update (VERSION1 deprecated) + else: + client = mqtt.Client() + self.client.on_connect = self.on_connect self.mqtt_settings = mp_settings.MPSettings( [('ip', str, '127.0.0.1'), ('port', int, '1883'), @@ -25,36 +30,50 @@ def __init__(self, mpstate): def mavlink_packet(self, m): """handle an incoming mavlink packet""" - if not self.connected: + if not self.client.is_connected(): return try: data = self.convert_to_dict(m) self.client.publish(f'{self.mqtt_settings.prefix}/{m.get_type()}', json.dumps(data)) except MQTTException as e: - self.connected = False print(f'mqtt: Exception occurred: {e}') def connect(self): """connect to mqtt broker""" - try: - if self.mqtt_settings.username != '': - if self.mqtt_settings.password != '': - self.client.username_pw_set(self.mqtt_settings.username, password=self.mqtt_settings.password) - else: - self.client.username_pw_set(self.mqtt_settings.username, password=None) - self.client.reinitialise(client_id=self.mqtt_settings.name) - print(f'connecting to {self.mqtt_settings.ip}:{self.mqtt_settings.port}') - self.client.connect(self.mqtt_settings.ip, int(self.mqtt_settings.port), 30) - except MQTTException as e: - print(f'mqtt: could not establish connection: {e}') - returns - self.connected = True - print('mqtt: connected...') + if self.mqtt_settings.username != '': + self.client.username_pw_set(username=self.mqtt_settings.username) + if self.mqtt_settings.password != '': + self.client.username_pw_set(username=self.mqtt_settings.username, password=self.mqtt_settings.password) + if self.client.is_connected(): + self.client.disconnect() + print(f'connecting to {self.mqtt_settings.ip}:{self.mqtt_settings.port}') + self.client.loop_start() + self.client.connect(self.mqtt_settings.ip, int(self.mqtt_settings.port), 30) + + def on_connect(self, client, userdata, flags, reason_code, properties=None): + """callback to set the connection status""" + if reason_code != 0: + print('mqtt: connection error - ', end='') + if reason_code == 1: + print('incorrect protocol version') + elif reason_code == 2: + print('invalid client identifier') + elif reason_code == 3: + print('server unavailable') + elif reason_code == 4: + print('bad username or password') + elif reason_code == 5: + print('not authorised') + else: + print('unknown error') + else: + print('mqtt: connected') + # stop the client loop because no incoming traffic needs to be handled + self.client.loop_stop() def disconnect(self): """disconnect""" self.client.disconnect() - self.connected = False print('mqtt: disconnected') def mqtt_command(self, args):