Skip to content

Commit cb64d9c

Browse files
authored
Merge pull request #9 from brentru/minimqtt-update
Update for MiniMQTT PR
2 parents 098618c + 3a8e0b3 commit cb64d9c

File tree

3 files changed

+46
-20
lines changed

3 files changed

+46
-20
lines changed

adafruit_aws_iot.py

+9
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,15 @@ def disconnect(self):
135135
self.on_unsubscribe = None
136136
self.client.deinit()
137137

138+
def reconnect(self):
139+
"""Reconnects to the AWS IoT MQTT Broker
140+
141+
"""
142+
try:
143+
self.client.reconnect()
144+
except MMQTTException as error:
145+
raise AWS_IOT_ERROR("Error re-connecting to AWS IoT:", error)
146+
138147
def connect(self, clean_session=True):
139148
"""Connects to Amazon AWS IoT MQTT Broker with Client ID.
140149
:param bool clean_session: Establishes a clean session with AWS broker.

examples/aws_iot_shadows.py

+18-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import time
12
import json
23
import board
34
import busio
@@ -6,7 +7,7 @@
67
from adafruit_esp32spi import adafruit_esp32spi
78
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
89
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
9-
from adafruit_minimqtt import MQTT
10+
import adafruit_minimqtt as MQTT
1011
from adafruit_aws_iot import MQTT_CLIENT
1112

1213
### WiFi ###
@@ -129,15 +130,12 @@ def message(client, topic, msg):
129130
wifi.connect()
130131
print("Connected!")
131132

133+
# Initialize MQTT interface with the esp interface
134+
MQTT.set_socket(socket, esp)
132135

133136
# Set up a new MiniMQTT Client
134-
client = MQTT(
135-
socket,
136-
broker=secrets["broker"],
137-
client_id=secrets["client_id"],
138-
network_manager=wifi,
139-
log=True,
140-
)
137+
client = MQTT.MQTT(broker=secrets["broker"],
138+
client_id=secrets["client_id"])
141139

142140
# Initialize AWS IoT MQTT API Client
143141
aws_iot = MQTT_CLIENT(client)
@@ -158,5 +156,15 @@ def message(client, topic, msg):
158156
# while True:
159157
# aws_iot.loop()
160158

161-
# Attempt to loop forever and handle network interface
162-
aws_iot.loop_forever()
159+
# Start a blocking message loop...
160+
# NOTE: NO code below this loop will execute
161+
# NOTE: Network reconnection is handled within this loop
162+
while True:
163+
try:
164+
aws_iot.loop()
165+
except (ValueError, RuntimeError) as e:
166+
print("Failed to get data, retrying\n", e)
167+
wifi.reset()
168+
aws_iot.reconnect()
169+
continue
170+
time.sleep(1)

examples/aws_iot_simpletest.py

+19-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import time
12
import json
23
import board
34
import busio
@@ -6,7 +7,7 @@
67
from adafruit_esp32spi import adafruit_esp32spi
78
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
89
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
9-
from adafruit_minimqtt import MQTT
10+
import adafruit_minimqtt as MQTT
1011
from adafruit_aws_iot import MQTT_CLIENT
1112

1213
### WiFi ###
@@ -126,14 +127,12 @@ def message(client, topic, msg):
126127
wifi.connect()
127128
print("Connected!")
128129

130+
# Initialize MQTT interface with the esp interface
131+
MQTT.set_socket(socket, esp)
132+
129133
# Set up a new MiniMQTT Client
130-
client = MQTT(
131-
socket,
132-
broker=secrets["broker"],
133-
client_id=secrets["client_id"],
134-
network_manager=wifi,
135-
log=True,
136-
)
134+
client = MQTT.MQTT(broker=secrets["broker"],
135+
client_id=secrets["client_id"])
137136

138137
# Initialize AWS IoT MQTT API Client
139138
aws_iot = MQTT_CLIENT(client)
@@ -154,5 +153,15 @@ def message(client, topic, msg):
154153
# while True:
155154
# aws_iot.loop()
156155

157-
# Attempt to loop forever and handle network interface
158-
aws_iot.loop_forever()
156+
# Start a blocking message loop...
157+
# NOTE: NO code below this loop will execute
158+
# NOTE: Network reconnection is handled within this loop
159+
while True:
160+
try:
161+
aws_iot.loop()
162+
except (ValueError, RuntimeError) as e:
163+
print("Failed to get data, retrying\n", e)
164+
wifi.reset()
165+
aws_iot.reconnect()
166+
continue
167+
time.sleep(1)

0 commit comments

Comments
 (0)