1+ import time
12import board
23import busio
34from digitalio import DigitalInOut
67from adafruit_esp32spi import adafruit_esp32spi_wifimanager
78import adafruit_esp32spi .adafruit_esp32spi_socket as socket
89
9- from adafruit_minimqtt import MQTT
10+ import adafruit_minimqtt as MQTT
1011from adafruit_gc_iot_core import Cloud_Core , MQTT_API
1112
1213### WiFi ###
@@ -92,6 +93,9 @@ def message(client, topic, msg):
9293wifi .connect ()
9394print ("Connected!" )
9495
96+ # Initialize MQTT interface with the esp interface
97+ MQTT .set_socket (socket , esp )
98+
9599# Initialize Google Cloud IoT Core interface
96100google_iot = Cloud_Core (esp , secrets )
97101
@@ -101,14 +105,10 @@ def message(client, topic, msg):
101105# print("Your JWT is: ", jwt)
102106
103107# Set up a new MiniMQTT Client
104- client = MQTT (
105- socket ,
106- broker = google_iot .broker ,
107- username = google_iot .username ,
108- password = secrets ["jwt" ],
109- client_id = google_iot .cid ,
110- network_manager = wifi ,
111- )
108+ client = MQTT .MQTT (broker = google_iot .broker ,
109+ username = google_iot .username ,
110+ password = secrets ["jwt" ],
111+ client_id = google_iot .cid )
112112
113113# Initialize Google MQTT API Client
114114google_mqtt = MQTT_API (client )
@@ -129,5 +129,15 @@ def message(client, topic, msg):
129129# while True:
130130# google_mqtt.loop()
131131
132- # Attempt to loop forever and handle network disconnection
133- google_mqtt .loop_blocking ()
132+ # Start a blocking message loop...
133+ # NOTE: NO code below this loop will execute
134+ # NOTE: Network reconnection is handled within this loop
135+ while True :
136+ try :
137+ google_mqtt .loop ()
138+ except (ValueError , RuntimeError ) as e :
139+ print ("Failed to get data, retrying\n " , e )
140+ wifi .reset ()
141+ google_mqtt .reconnect ()
142+ continue
143+ time .sleep (1 )
0 commit comments