Skip to content
Browse files

Exit when we're told to, and try to reconnect in the event of a failure

  • Loading branch information...
1 parent e686fa0 commit d464b0060908f6b763168b14a4df72e9932306f1 @kylegordon committed
Showing with 6 additions and 1 deletion.
  1. +6 −1 mqtt-gpio-trigger.py
View
7 mqtt-gpio-trigger.py
@@ -62,12 +62,17 @@ def cleanup(signum, frame):
mqttc.publish("/status/" + socket.getfqdn(), "Offline")
mqttc.disconnect()
logging.info("Exiting on signal %d", signum)
+ sys.exit(signum)
def connect():
"""
Connect to the broker, define the callbacks, and subscribe
"""
- mqttc.connect(MQTT_HOST, MQTT_PORT, 60, True)
+ result = mqttc.connect(MQTT_HOST, MQTT_PORT, 60, True)
+ if result != 0:
+ logging.info("Connection failed with error code %s. Retrying", result)
+ time.sleep(10)
+ connect()
#define the callbacks
mqttc.on_message = on_message

0 comments on commit d464b00

Please sign in to comment.
Something went wrong with that request. Please try again.