diff --git a/examples/mqtt-all.py b/examples/mqtt-all.py index 0eff47a..b725a6c 100755 --- a/examples/mqtt-all.py +++ b/examples/mqtt-all.py @@ -1,13 +1,14 @@ """ Run mqtt broker on localhost: sudo apt-get install mosquitto mosquitto-clients -Example run: python3 mqtt-all.py --broker 192.168.1.164 --topic enviro +Example run: python3 mqtt-all.py --broker 192.168.1.164 --topic enviro --username xxx --password xxxx """ #!/usr/bin/env python3 import argparse import ST7735 import time +import ssl from bme280 import BME280 from pms5003 import PMS5003, ReadTimeoutError, SerialTimeoutError from enviroplus import gas @@ -38,6 +39,9 @@ DEFAULT_MQTT_BROKER_PORT = 1883 DEFAULT_MQTT_TOPIC = "enviroplus" DEFAULT_READ_INTERVAL = 5 +DEFAULT_TLS_MODE = False +DEFAULT_USERNAME = None +DEFAULT_PASSWORD = None # mqtt callbacks def on_connect(client, userdata, flags, rc): @@ -165,6 +169,24 @@ def main(): type=int, help="the read interval in seconds", ) + parser.add_argument( + "--tls", + default=DEFAULT_TLS_MODE, + action='store_true', + help="enable TLS" + ) + parser.add_argument( + "--username", + default=DEFAULT_USERNAME, + type=str, + help="mqtt username" + ) + parser.add_argument( + "--password", + default=DEFAULT_PASSWORD, + type=str, + help="mqtt password" + ) args = parser.parse_args() # Raspberry Pi ID @@ -178,6 +200,9 @@ def main(): client_id: {device_id} port: {args.port} topic: {args.topic} + tls: {args.tls} + username: {args.username} + password: {args.password} Press Ctrl+C to exit! @@ -185,8 +210,17 @@ def main(): ) mqtt_client = mqtt.Client(client_id=device_id) + if args.username and args.password: + mqtt_client.username_pw_set(args.username, args.password) mqtt_client.on_connect = on_connect mqtt_client.on_publish = on_publish + + if args.tls is True: + mqtt_client.tls_set(tls_version=ssl.PROTOCOL_TLSv1_2) + + if args.username is not None: + mqtt_client.username_pw_set(args.username, password=args.password) + mqtt_client.connect(args.broker, port=args.port) bus = SMBus(1)