Add MQTT auth support
This commit adds config options and support for connecting to an mqtt broker with auth. Change-Id: I1fad16d2cd3941dd414e7469c1ecb6a2ba96e1f9
This commit is contained in:
parent
964b3fdb33
commit
8a4a69afef
|
@ -51,6 +51,10 @@ can set:
|
||||||
if your broker uses a non-default port.
|
if your broker uses a non-default port.
|
||||||
* **keepalive** - Used to set the keepalive time for connections to the MQTT
|
* **keepalive** - Used to set the keepalive time for connections to the MQTT
|
||||||
broker. By default this is set to 60 seconds.
|
broker. By default this is set to 60 seconds.
|
||||||
|
* **username** - Used to set the auth username to connect to the MQTT broker
|
||||||
|
with.
|
||||||
|
* **password** - Used to set the auth password to connect to the MQTT broker
|
||||||
|
with. A username must be set for this option to be used.
|
||||||
|
|
||||||
Other Settings
|
Other Settings
|
||||||
--------------
|
--------------
|
||||||
|
|
|
@ -116,10 +116,26 @@ def _main(args, config):
|
||||||
else:
|
else:
|
||||||
keepalive = 60
|
keepalive = 60
|
||||||
|
|
||||||
|
# Configure auth
|
||||||
|
auth = None
|
||||||
|
if config.has_option('mqtt', 'username'):
|
||||||
|
mqtt_username = config.get('mqtt', 'username')
|
||||||
|
else:
|
||||||
|
mqtt_username = None
|
||||||
|
if config.has_option('mqtt', 'password'):
|
||||||
|
mqtt_password = config.get('mqtt', 'password')
|
||||||
|
else:
|
||||||
|
mqtt_password = None
|
||||||
|
if mqtt_username:
|
||||||
|
auth = {'username': mqtt_username}
|
||||||
|
if mqtt_password:
|
||||||
|
auth['password'] = mqtt_password
|
||||||
|
|
||||||
mqttqueue = PushMQTT(
|
mqttqueue = PushMQTT(
|
||||||
config.get('mqtt', 'hostname'),
|
config.get('mqtt', 'hostname'),
|
||||||
port=mqtt_port,
|
port=mqtt_port,
|
||||||
keepalive=keepalive)
|
keepalive=keepalive,
|
||||||
|
auth=auth)
|
||||||
|
|
||||||
base_topic = config.get('mqtt', 'topic')
|
base_topic = config.get('mqtt', 'topic')
|
||||||
while True:
|
while True:
|
||||||
|
|
Loading…
Reference in New Issue