From aeaad174b8da7aa5dabc31a126fa2e287c98f18e Mon Sep 17 00:00:00 2001 From: Peter Armstrong Date: Sun, 20 Dec 2020 12:46:19 +0000 Subject: [PATCH 1/3] adds username and password parameters to mqtt-all --- examples/mqtt-all.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/examples/mqtt-all.py b/examples/mqtt-all.py index 0eff47a..2e9df34 100755 --- a/examples/mqtt-all.py +++ b/examples/mqtt-all.py @@ -1,7 +1,7 @@ """ 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 @@ -165,6 +165,19 @@ def main(): type=int, help="the read interval in seconds", ) + parser.add_argument( + "--username", + default=None, + type=str, + help="mqtt username", + ) + parser.add_argument( + "--password", + default=None, + type=str, + help="mqtt password", + ) + args = parser.parse_args() # Raspberry Pi ID @@ -178,6 +191,8 @@ def main(): client_id: {device_id} port: {args.port} topic: {args.topic} + username: {args.username} + password: {args.password} Press Ctrl+C to exit! @@ -185,6 +200,7 @@ def main(): ) mqtt_client = mqtt.Client(client_id=device_id) + mqtt_client.username_pw_set(args.username, args.password) mqtt_client.on_connect = on_connect mqtt_client.on_publish = on_publish mqtt_client.connect(args.broker, port=args.port) From d2f4688195b6aa5b1beb4745623672ed854ef63c Mon Sep 17 00:00:00 2001 From: Peter Armstrong Date: Mon, 4 Jan 2021 12:16:13 +0000 Subject: [PATCH 2/3] adds username and password check --- examples/mqtt-all.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/mqtt-all.py b/examples/mqtt-all.py index 2e9df34..07eeccc 100755 --- a/examples/mqtt-all.py +++ b/examples/mqtt-all.py @@ -200,7 +200,8 @@ def main(): ) mqtt_client = mqtt.Client(client_id=device_id) - mqtt_client.username_pw_set(args.username, args.password) + if username and password: + mqtt_client.username_pw_set(args.username, args.password) mqtt_client.on_connect = on_connect mqtt_client.on_publish = on_publish mqtt_client.connect(args.broker, port=args.port) From 8e62b4f76481ff38831beef1b86d2f1cca77a4f2 Mon Sep 17 00:00:00 2001 From: Peter Armstrong Date: Tue, 13 Apr 2021 13:02:36 +0100 Subject: [PATCH 3/3] fix checking for username and password --- examples/mqtt-all.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/mqtt-all.py b/examples/mqtt-all.py index 07eeccc..5f25e39 100755 --- a/examples/mqtt-all.py +++ b/examples/mqtt-all.py @@ -200,7 +200,7 @@ def main(): ) mqtt_client = mqtt.Client(client_id=device_id) - if username and password: + 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