Skip to content

Commit ae72dab

Browse files
authored
Merge pull request #92 from peteretep/master
adds username and password parameters to mqtt-all
2 parents 12ec16a + 8e62b4f commit ae72dab

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

examples/mqtt-all.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
Run mqtt broker on localhost: sudo apt-get install mosquitto mosquitto-clients
33
4-
Example run: python3 mqtt-all.py --broker 192.168.1.164 --topic enviro
4+
Example run: python3 mqtt-all.py --broker 192.168.1.164 --topic enviro --username xxx --password xxxx
55
"""
66
#!/usr/bin/env python3
77

@@ -165,6 +165,19 @@ def main():
165165
type=int,
166166
help="the read interval in seconds",
167167
)
168+
parser.add_argument(
169+
"--username",
170+
default=None,
171+
type=str,
172+
help="mqtt username",
173+
)
174+
parser.add_argument(
175+
"--password",
176+
default=None,
177+
type=str,
178+
help="mqtt password",
179+
)
180+
168181
args = parser.parse_args()
169182

170183
# Raspberry Pi ID
@@ -178,13 +191,17 @@ def main():
178191
client_id: {device_id}
179192
port: {args.port}
180193
topic: {args.topic}
194+
username: {args.username}
195+
password: {args.password}
181196
182197
Press Ctrl+C to exit!
183198
184199
"""
185200
)
186201

187202
mqtt_client = mqtt.Client(client_id=device_id)
203+
if args.username and args.password:
204+
mqtt_client.username_pw_set(args.username, args.password)
188205
mqtt_client.on_connect = on_connect
189206
mqtt_client.on_publish = on_publish
190207
mqtt_client.connect(args.broker, port=args.port)

0 commit comments

Comments
 (0)