diff --git a/README b/README index fa7f379..c1280fb 100644 --- a/README +++ b/README @@ -1,3 +1,17 @@ +This is the docker container and script to publish the energy consumption of the Huette to the MQTT broker. + +The data is a json array with index 0 means today, index 1 one day in the past, index 2 two days in the past and so on. The data is in watt hours. + +e.g. [1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000] + +# Environment variables + +MQTT_HOST: The host of the MQTT broker +MQTT_PORT: (Optional) The port of the MQTT broker +MQTT_TOPIC: The topic to publish to +MQTT_USERNAME: (Optional) The user for the MQTT broker +MQTT_PASSWORD: (Optional) The password for the MQTT broker + # Build ``` diff --git a/main.py b/main.py index a61c1e7..e8e2fcb 100644 --- a/main.py +++ b/main.py @@ -44,7 +44,17 @@ def main(): port = int(os.getenv("MQTT_PORT", 1883)) topic = os.getenv("MQTT_TOPIC") + username = os.getenv("MQTT_USERNAME") + password = os.getenv("MQTT_PASSWORD") + mqttc = mqtt.Client(mqtt_enums.CallbackAPIVersion.VERSION2) + + if username: + if not password: + raise ValueError("MQTT_USERNAME is set but MQTT_PASSWORD is not set") + + mqttc.username_pw_set(username, password) + mqttc.connect(host, port) mqttc.loop_start()