Permalink
Browse files

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

  • Loading branch information...
kylegordon committed Dec 6, 2012
1 parent 13e959b commit 643d351786c852be6f1018c4c8c685d46cf07a69
Showing with 7 additions and 1 deletion.
  1. +7 −1 mqtt-pwm-lights.py
View
@@ -51,12 +51,18 @@ 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 643d351

Please sign in to comment.