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,19 +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+ action = 'store_true' ,
176+ help = "enable TLS"
177+ )
168178 parser .add_argument (
169179 "--username" ,
170- default = None ,
180+ default = DEFAULT_USERNAME ,
171181 type = str ,
172- help = "mqtt username" ,
182+ help = "mqtt username"
173183 )
174184 parser .add_argument (
175185 "--password" ,
176- default = None ,
186+ default = DEFAULT_PASSWORD ,
177187 type = str ,
178- help = "mqtt password" ,
188+ help = "mqtt password"
179189 )
180-
181190 args = parser .parse_args ()
182191
183192 # Raspberry Pi ID
@@ -191,6 +200,7 @@ def main():
191200 client_id: { device_id }
192201 port: { args .port }
193202 topic: { args .topic }
203+ tls: { args .tls }
194204 username: { args .username }
195205 password: { args .password }
196206
@@ -204,6 +214,13 @@ def main():
204214 mqtt_client .username_pw_set (args .username , args .password )
205215 mqtt_client .on_connect = on_connect
206216 mqtt_client .on_publish = on_publish
217+
218+ if args .tls is True :
219+ mqtt_client .tls_set (tls_version = ssl .PROTOCOL_TLSv1_2 )
220+
221+ if args .username is not None :
222+ mqtt_client .username_pw_set (args .username , password = args .password )
223+
207224 mqtt_client .connect (args .broker , port = args .port )
208225
209226 bus = SMBus (1 )
0 commit comments