88import argparse
99import ST7735
1010import time
11+ import ssl
1112from bme280 import BME280
1213from pms5003 import PMS5003 , ReadTimeoutError , SerialTimeoutError
1314from enviroplus import gas
3839DEFAULT_MQTT_BROKER_PORT = 1883
3940DEFAULT_MQTT_TOPIC = "enviroplus"
4041DEFAULT_READ_INTERVAL = 5
42+ DEFAULT_TLS_MODE = False
43+ DEFAULT_USERNAME = None
44+ DEFAULT_PASSWORD = None
4145
4246# mqtt callbacks
4347def 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