summaryrefslogtreecommitdiff
path: root/mqtt_client/main.py
diff options
context:
space:
mode:
Diffstat (limited to 'mqtt_client/main.py')
-rw-r--r--mqtt_client/main.py15
1 files changed, 13 insertions, 2 deletions
diff --git a/mqtt_client/main.py b/mqtt_client/main.py
index 1a3ca8a..78cdca6 100644
--- a/mqtt_client/main.py
+++ b/mqtt_client/main.py
@@ -1,5 +1,7 @@
import paho.mqtt.client as mqtt
+from paho.mqtt.enums import MQTTProtocolVersion
import json
+from time import sleep
def on_connect(client, userdata, flags, reason_code, properties):
print(f"Conectado: {reason_code}")
@@ -14,10 +16,19 @@ def on_message(client, userdata, msg):
print(f"Unit: {payload["unit"]}")
print(f"Timestamp: {payload["timestamp"]}")
-mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
+mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, protocol=MQTTProtocolVersion.MQTTv5)
mqttc.on_connect = on_connect
mqttc.on_message = on_message
+mqttc.username_pw_set('tester', 'rosebud')
-mqttc.connect("127.0.0.1", 1883, 60)
+connected = False
+
+while(not connected):
+ try:
+ mqttc.connect("127.0.0.1", 1883, 60, '', 0, True)
+ connected = True
+ except ConnectionRefusedError:
+ print("Failed to connect. Retrying...")
+ sleep(5)
mqttc.loop_forever()