Skip to content

Commit ae3e87d

Browse files
committed
Adding MQTT Username / Password & TLS Config
Signed-off-by: James Sutton <1068763+jpwsutton@users.noreply.github.com>
1 parent 12ec16a commit ae3e87d

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

examples/mqtt-all.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import argparse
99
import ST7735
1010
import time
11+
import ssl
1112
from bme280 import BME280
1213
from pms5003 import PMS5003, ReadTimeoutError, SerialTimeoutError
1314
from enviroplus import gas
@@ -38,6 +39,9 @@
3839
DEFAULT_MQTT_BROKER_PORT = 1883
3940
DEFAULT_MQTT_TOPIC = "enviroplus"
4041
DEFAULT_READ_INTERVAL = 5
42+
DEFAULT_TLS_MODE = False
43+
DEFAULT_USERNAME = None
44+
DEFAULT_PASSWORD = None
4145

4246
# mqtt callbacks
4347
def on_connect(client, userdata, flags, rc):
@@ -165,6 +169,24 @@ def main():
165169
type=int,
166170
help="the read interval in seconds",
167171
)
172+
parser.add_argument(
173+
"--tls",
174+
default=DEFAULT_TLS_MODE,
175+
type=bool,
176+
help="enable TLS"
177+
)
178+
parser.add_argument(
179+
"--username",
180+
default=DEFAULT_USERNAME,
181+
type=str,
182+
help="mqtt username"
183+
)
184+
parser.add_argument(
185+
"--password",
186+
default=DEFAULT_PASSWORD,
187+
type=str,
188+
help="mqtt password"
189+
)
168190
args = parser.parse_args()
169191

170192
# Raspberry Pi ID
@@ -178,6 +200,9 @@ def main():
178200
client_id: {device_id}
179201
port: {args.port}
180202
topic: {args.topic}
203+
tls: {args.tls}
204+
username: {args.username}
205+
password: {args.password}
181206
182207
Press Ctrl+C to exit!
183208
@@ -187,6 +212,13 @@ def main():
187212
mqtt_client = mqtt.Client(client_id=device_id)
188213
mqtt_client.on_connect = on_connect
189214
mqtt_client.on_publish = on_publish
215+
216+
if args.tls is True:
217+
mqtt_client.tls_set(tls_version=ssl.PROTOCOL_TLSv1_2)
218+
219+
if args.username is not None:
220+
mqtt_client.username_pw_set(args.username, password=args.password)
221+
190222
mqtt_client.connect(args.broker, port=args.port)
191223

192224
bus = SMBus(1)

0 commit comments

Comments
 (0)