From 874e9085a74f75a973d86805b5015421d1271f4d Mon Sep 17 00:00:00 2001 From: Mike Wilkinson Date: Tue, 14 May 2019 08:34:14 -0400 Subject: [PATCH 1/2] Add MQTT connection error handling --- detect_objects.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/detect_objects.py b/detect_objects.py index 784a6c7d9..f6b604af9 100644 --- a/detect_objects.py +++ b/detect_objects.py @@ -25,6 +25,17 @@ def main(): # connect to mqtt and setup last will def on_connect(client, userdata, flags, rc): print("On connect called") + if rc != 0: + if rc == 3: + print ("MQTT Server unavailable") + elif rc == 4: + print ("MQTT Bad username or password") + elif rc == 5: + print ("MQTT Not authorized") + else: + print ("Unable to connect to MQTT: Connection refused. Error code: " + str(rc)) + elif rc == 0: + print ("Connection to MQTT established.") # publish a message to signal that the service is running client.publish(MQTT_TOPIC_PREFIX+'/available', 'online', retain=True) client = mqtt.Client() @@ -87,4 +98,4 @@ def main(): camera.join() if __name__ == '__main__': - main() \ No newline at end of file + main() From 2c2f0044b9544ba67018df09ba3de608a9abd716 Mon Sep 17 00:00:00 2001 From: Mike Wilkinson Date: Tue, 14 May 2019 11:09:57 -0400 Subject: [PATCH 2/2] Remove error redundant check --- detect_objects.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/detect_objects.py b/detect_objects.py index f6b604af9..2c7a4c420 100644 --- a/detect_objects.py +++ b/detect_objects.py @@ -34,8 +34,6 @@ def main(): print ("MQTT Not authorized") else: print ("Unable to connect to MQTT: Connection refused. Error code: " + str(rc)) - elif rc == 0: - print ("Connection to MQTT established.") # publish a message to signal that the service is running client.publish(MQTT_TOPIC_PREFIX+'/available', 'online', retain=True) client = mqtt.Client()