Mise à jour avec la nouvelle API (V2) de Paho MQTT

This commit is contained in:
2025-08-24 16:37:25 +02:00
parent 75f8c6a637
commit 2309ef1deb
3 changed files with 18 additions and 8 deletions

View File

@@ -179,12 +179,14 @@ def load_config(config_path):
def connect_mqtt(mqtt_config):
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to MQTT Broker!")
def on_connect(client, userdata, connect_flags, reason_code, properties):
if reason_code == 0:
logging.info("Connected to MQTT Broker!")
else:
print("Failed to connect, return code {rc}")
client = mqtt_client.Client()
logging.error(f"Failed to connect to MQTT Broker, reason code: {reason_code}")
# Use the new callback API version 2
client = mqtt_client.Client(callback_api_version=mqtt_client.CallbackAPIVersion.VERSION2)
client.username_pw_set(mqtt_config["user"], mqtt_config["password"])
client.on_connect = on_connect
client.connect(mqtt_config['broker'], mqtt_config['port'])