File tree Expand file tree Collapse file tree 1 file changed +18
-1
lines changed Expand file tree Collapse file tree 1 file changed +18
-1
lines changed Original file line number Diff line number Diff line change 11"""
22Run 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 )
You can’t perform that action at this time.
0 commit comments