From 87bf385d1994c5c0ee14b32218107b6cfa7743f7 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 13 Mar 2020 11:01:52 -0400 Subject: [PATCH 1/4] add reconnect --- adafruit_gc_iot_core.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/adafruit_gc_iot_core.py b/adafruit_gc_iot_core.py index 8a13e2b..37f48ec 100644 --- a/adafruit_gc_iot_core.py +++ b/adafruit_gc_iot_core.py @@ -128,6 +128,14 @@ def disconnect(self): # De-initialize MiniMQTT Client self._client.deinit() + def reconnect(self): + """Reconnects to the Google MQTT Broker. + """ + try: + self._client.reconnect() + except: + raise MQTT_API_ERROR("Error reconnecting to Google MQTT.") + def connect(self): """Connects to the Google MQTT Broker. """ From 305cd9d7eea037aea1f84081e71113028eebb96f Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 13 Mar 2020 11:23:14 -0400 Subject: [PATCH 2/4] update for PR --- adafruit_gc_iot_core.py | 2 +- examples/gc_iot_core_simpletest.py | 17 ++++++++--------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/adafruit_gc_iot_core.py b/adafruit_gc_iot_core.py index 37f48ec..4ef62a9 100644 --- a/adafruit_gc_iot_core.py +++ b/adafruit_gc_iot_core.py @@ -339,7 +339,7 @@ def __init__(self, esp, secrets, log=False): self._reg_id = secrets["registry_id"] self._device_id = secrets["device_id"] self._private_key = secrets["private_key"] - self.broker = "mqtt.googleapis.com" + self.broker = "https://mqtt.googleapis.com" self.username = b"unused" self.cid = self.client_id diff --git a/examples/gc_iot_core_simpletest.py b/examples/gc_iot_core_simpletest.py index 932f198..ce2aa76 100644 --- a/examples/gc_iot_core_simpletest.py +++ b/examples/gc_iot_core_simpletest.py @@ -6,7 +6,7 @@ from adafruit_esp32spi import adafruit_esp32spi_wifimanager import adafruit_esp32spi.adafruit_esp32spi_socket as socket -from adafruit_minimqtt import MQTT +import adafruit_minimqtt as MQTT from adafruit_gc_iot_core import Cloud_Core, MQTT_API ### WiFi ### @@ -92,6 +92,9 @@ def message(client, topic, msg): wifi.connect() print("Connected!") +# Initialize MQTT interface with the esp interface +MQTT.set_socket(socket, esp) + # Initialize Google Cloud IoT Core interface google_iot = Cloud_Core(esp, secrets) @@ -101,14 +104,10 @@ def message(client, topic, msg): # print("Your JWT is: ", jwt) # Set up a new MiniMQTT Client -client = MQTT( - socket, - broker=google_iot.broker, - username=google_iot.username, - password=secrets["jwt"], - client_id=google_iot.cid, - network_manager=wifi, -) +client = MQTT.MQTT(broker=google_iot.broker, + username=google_iot.username, + password=secrets["jwt"], + client_id=google_iot.cid) # Initialize Google MQTT API Client google_mqtt = MQTT_API(client) From c813b1fdf91212ae23c9e4f91531a2e9a79caf08 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 13 Mar 2020 11:28:27 -0400 Subject: [PATCH 3/4] remove blocking --- examples/gc_iot_core_simpletest.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/examples/gc_iot_core_simpletest.py b/examples/gc_iot_core_simpletest.py index ce2aa76..d6f2352 100644 --- a/examples/gc_iot_core_simpletest.py +++ b/examples/gc_iot_core_simpletest.py @@ -128,5 +128,15 @@ def message(client, topic, msg): # while True: # google_mqtt.loop() -# Attempt to loop forever and handle network disconnection -google_mqtt.loop_blocking() +# Start a blocking message loop... +# NOTE: NO code below this loop will execute +# NOTE: Network reconnection is handled within this loop +while True: + try: + google_mqtt.loop() + except (ValueError, RuntimeError) as e: + print("Failed to get data, retrying\n", e) + wifi.reset() + google_mqtt.reconnect() + continue + time.sleep(1) \ No newline at end of file From 0032b098105a6eea71c8618367da9438b5ac1079 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 13 Mar 2020 11:28:53 -0400 Subject: [PATCH 4/4] remove block.. --- examples/gc_iot_core_simpletest.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/gc_iot_core_simpletest.py b/examples/gc_iot_core_simpletest.py index d6f2352..2e8833d 100644 --- a/examples/gc_iot_core_simpletest.py +++ b/examples/gc_iot_core_simpletest.py @@ -1,3 +1,4 @@ +import time import board import busio from digitalio import DigitalInOut @@ -139,4 +140,4 @@ def message(client, topic, msg): wifi.reset() google_mqtt.reconnect() continue - time.sleep(1) \ No newline at end of file + time.sleep(1)