Skip to content

Commit 622b4c7

Browse files
author
brentru
committed
move blocking logic into the client-side, tested
1 parent b4c0850 commit 622b4c7

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

examples/minimqtt_pub_sub_blocking.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# CircuitPython MiniMQTT Library
22
# Adafruit IO SSL/TLS Example for WiFi
3+
import time
34
import board
45
import busio
56
from digitalio import DigitalInOut
@@ -93,8 +94,19 @@ def message(client, topic, message):
9394
print('Connecting to MQTT broker...')
9495
mqtt_client.connect()
9596

96-
# Start a blocking message loop
97-
# If you only want to listen to incoming messages,
98-
# you'll want to loop_forever as it handles network reconnections
99-
# No code below this line will execute.
100-
mqtt_client.loop_forever()
97+
# Start a blocking message loop...
98+
# NOTE: NO code below this loop will execute
99+
# NOTE: Network reconnection is handled within this loop
100+
101+
counter = 0
102+
while True:
103+
print("Iteration #", counter)
104+
try:
105+
mqtt_client.loop()
106+
except (ValueError, RuntimeError) as e:
107+
print("Failed to get data, retrying\n", e)
108+
wifi.reset()
109+
mqtt_client.reconnect()
110+
continue
111+
counter += 1
112+
time.sleep(1)

0 commit comments

Comments
 (0)