Add username and password authentication as optional for mqtt broker and refine readme
parent
42480df5c1
commit
3072331f8a
14
README
14
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
|
# Build
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
10
main.py
10
main.py
|
@ -44,7 +44,17 @@ def main():
|
||||||
port = int(os.getenv("MQTT_PORT", 1883))
|
port = int(os.getenv("MQTT_PORT", 1883))
|
||||||
topic = os.getenv("MQTT_TOPIC")
|
topic = os.getenv("MQTT_TOPIC")
|
||||||
|
|
||||||
|
username = os.getenv("MQTT_USERNAME")
|
||||||
|
password = os.getenv("MQTT_PASSWORD")
|
||||||
|
|
||||||
mqttc = mqtt.Client(mqtt_enums.CallbackAPIVersion.VERSION2)
|
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.connect(host, port)
|
||||||
mqttc.loop_start()
|
mqttc.loop_start()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue