Skip to content

Commit 7e1ab12

Browse files
authored
Merge pull request #9 from brentru/update-minimqtt
Update for minimqtt PR
2 parents 8702e0e + 0032b09 commit 7e1ab12

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

adafruit_gc_iot_core.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,14 @@ def disconnect(self):
128128
# De-initialize MiniMQTT Client
129129
self._client.deinit()
130130

131+
def reconnect(self):
132+
"""Reconnects to the Google MQTT Broker.
133+
"""
134+
try:
135+
self._client.reconnect()
136+
except:
137+
raise MQTT_API_ERROR("Error reconnecting to Google MQTT.")
138+
131139
def connect(self):
132140
"""Connects to the Google MQTT Broker.
133141
"""
@@ -331,7 +339,7 @@ def __init__(self, esp, secrets, log=False):
331339
self._reg_id = secrets["registry_id"]
332340
self._device_id = secrets["device_id"]
333341
self._private_key = secrets["private_key"]
334-
self.broker = "mqtt.googleapis.com"
342+
self.broker = "https://mqtt.googleapis.com"
335343
self.username = b"unused"
336344
self.cid = self.client_id
337345

examples/gc_iot_core_simpletest.py

+21-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import time
12
import board
23
import busio
34
from digitalio import DigitalInOut
@@ -6,7 +7,7 @@
67
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
78
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
89

9-
from adafruit_minimqtt import MQTT
10+
import adafruit_minimqtt as MQTT
1011
from adafruit_gc_iot_core import Cloud_Core, MQTT_API
1112

1213
### WiFi ###
@@ -92,6 +93,9 @@ def message(client, topic, msg):
9293
wifi.connect()
9394
print("Connected!")
9495

96+
# Initialize MQTT interface with the esp interface
97+
MQTT.set_socket(socket, esp)
98+
9599
# Initialize Google Cloud IoT Core interface
96100
google_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
114114
google_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

Comments
 (0)