From aeaad174b8da7aa5dabc31a126fa2e287c98f18e Mon Sep 17 00:00:00 2001 From: Peter Armstrong Date: Sun, 20 Dec 2020 12:46:19 +0000 Subject: [PATCH 1/7] 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/7] 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 ae3e87dc933d1e94548f40e1903027fc7b2e7096 Mon Sep 17 00:00:00 2001 From: James Sutton <1068763+jpwsutton@users.noreply.github.com> Date: Fri, 19 Feb 2021 20:07:48 +0000 Subject: [PATCH 3/7] Adding MQTT Username / Password & TLS Config Signed-off-by: James Sutton <1068763+jpwsutton@users.noreply.github.com> --- examples/mqtt-all.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/examples/mqtt-all.py b/examples/mqtt-all.py index 0eff47a..d531120 100755 --- a/examples/mqtt-all.py +++ b/examples/mqtt-all.py @@ -8,6 +8,7 @@ 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, + type=bool, + 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! @@ -187,6 +212,13 @@ def main(): mqtt_client = mqtt.Client(client_id=device_id) 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) From 2dbabe56cd8cddb2a8c641a9b545eacc80e0c4f0 Mon Sep 17 00:00:00 2001 From: James Sutton <1068763+jpwsutton@users.noreply.github.com> Date: Fri, 19 Feb 2021 20:23:24 +0000 Subject: [PATCH 4/7] Tweaking the arguments for MQTT TLS Signed-off-by: James Sutton <1068763+jpwsutton@users.noreply.github.com> --- 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 d531120..9735ddc 100755 --- a/examples/mqtt-all.py +++ b/examples/mqtt-all.py @@ -172,7 +172,7 @@ def main(): parser.add_argument( "--tls", default=DEFAULT_TLS_MODE, - type=bool, + action='store_true', help="enable TLS" ) parser.add_argument( From 8e62b4f76481ff38831beef1b86d2f1cca77a4f2 Mon Sep 17 00:00:00 2001 From: Peter Armstrong Date: Tue, 13 Apr 2021 13:02:36 +0100 Subject: [PATCH 5/7] 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 From ba1042d0b2245f9ad5d0d12ac55c01f7b66fa223 Mon Sep 17 00:00:00 2001 From: James Sutton <1068763+jpwsutton@users.noreply.github.com> Date: Fri, 19 Feb 2021 20:07:48 +0000 Subject: [PATCH 6/7] Adding MQTT Username / Password & TLS Config Signed-off-by: James Sutton <1068763+jpwsutton@users.noreply.github.com> --- examples/mqtt-all.py | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/examples/mqtt-all.py b/examples/mqtt-all.py index 5f25e39..895a8a2 100755 --- a/examples/mqtt-all.py +++ b/examples/mqtt-all.py @@ -8,6 +8,7 @@ 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,19 +169,24 @@ def main(): type=int, help="the read interval in seconds", ) + parser.add_argument( + "--tls", + default=DEFAULT_TLS_MODE, + type=bool, + help="enable TLS" + ) parser.add_argument( "--username", - default=None, + default=DEFAULT_USERNAME, type=str, - help="mqtt username", + help="mqtt username" ) parser.add_argument( "--password", - default=None, + default=DEFAULT_PASSWORD, type=str, - help="mqtt password", + help="mqtt password" ) - args = parser.parse_args() # Raspberry Pi ID @@ -191,6 +200,7 @@ def main(): client_id: {device_id} port: {args.port} topic: {args.topic} + tls: {args.tls} username: {args.username} password: {args.password} @@ -204,6 +214,13 @@ def main(): 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) From d3c7e731ec6d57f16a3d4983f1cec9529bb37077 Mon Sep 17 00:00:00 2001 From: James Sutton <1068763+jpwsutton@users.noreply.github.com> Date: Fri, 19 Feb 2021 20:23:24 +0000 Subject: [PATCH 7/7] Tweaking the arguments for MQTT TLS Signed-off-by: James Sutton <1068763+jpwsutton@users.noreply.github.com> --- 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 895a8a2..b725a6c 100755 --- a/examples/mqtt-all.py +++ b/examples/mqtt-all.py @@ -172,7 +172,7 @@ def main(): parser.add_argument( "--tls", default=DEFAULT_TLS_MODE, - type=bool, + action='store_true', help="enable TLS" ) parser.add_argument(