Merge pull request #36 from drcrimzon/patch-1

Add MQTT connection error handling
This commit is contained in:
Blake Blackshear 2019-05-15 07:10:53 -05:00 committed by GitHub
commit 8c924896c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -25,6 +25,15 @@ def main():
# connect to mqtt and setup last will # connect to mqtt and setup last will
def on_connect(client, userdata, flags, rc): def on_connect(client, userdata, flags, rc):
print("On connect called") 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))
# publish a message to signal that the service is running # publish a message to signal that the service is running
client.publish(MQTT_TOPIC_PREFIX+'/available', 'online', retain=True) client.publish(MQTT_TOPIC_PREFIX+'/available', 'online', retain=True)
client = mqtt.Client() client = mqtt.Client()
@ -87,4 +96,4 @@ def main():
camera.join() camera.join()
if __name__ == '__main__': if __name__ == '__main__':
main() main()