Skip to content

Commit

Permalink
fix: Reconnect if MQTT server is not available at start up.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewjw committed Nov 7, 2022
1 parent fdbfb5d commit 128abcb
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions glowprom/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import functools
import time

import paho.mqtt.client as mqtt

Expand All @@ -25,12 +26,22 @@ def on_connect(topic, client, userdata, flags, rc):
client.subscribe(topic)


def connect(args, on_message):
def connect(args, on_message, retry=True):
client = mqtt.Client()
client.on_connect = functools.partial(on_connect, args.topic)
client.on_message = on_message

client.username_pw_set(args.user, password=args.passwd)
client.connect("glowmqtt.energyhive.com", 1883, 60)

while True:
try:
client.connect(args.mqtt, args.port, 60)
except Exception as e:
if not retry:
raise
print(e)
time.sleep(10)
else:
break

client.loop_forever()

0 comments on commit 128abcb

Please sign in to comment.