summaryrefslogtreecommitdiff
path: root/mqtt_client
diff options
context:
space:
mode:
Diffstat (limited to 'mqtt_client')
-rw-r--r--mqtt_client/main.py11
1 files changed, 10 insertions, 1 deletions
diff --git a/mqtt_client/main.py b/mqtt_client/main.py
index 78cdca6..80b4da9 100644
--- a/mqtt_client/main.py
+++ b/mqtt_client/main.py
@@ -2,6 +2,7 @@ import paho.mqtt.client as mqtt
from paho.mqtt.enums import MQTTProtocolVersion
import json
from time import sleep
+import os
def on_connect(client, userdata, flags, reason_code, properties):
print(f"Conectado: {reason_code}")
@@ -16,10 +17,18 @@ def on_message(client, userdata, msg):
print(f"Unit: {payload["unit"]}")
print(f"Timestamp: {payload["timestamp"]}")
+try:
+ user_name = os.environ["MQTT_CLIENT_USER"]
+ user_pass = os.environ["MQTT_CLIENT_PASSWORD"]
+except KeyError:
+ print("credentials not supplied in environment variables. Going unauthenticated...")
+ user_name = None
+ user_pass = None
+
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.username_pw_set(user_name, user_pass)
connected = False