Skip to content

Update for MiniMQTT PR #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions adafruit_aws_iot.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@ def disconnect(self):
self.on_unsubscribe = None
self.client.deinit()

def reconnect(self):
"""Reconnects to the AWS IoT MQTT Broker

"""
try:
self.client.reconnect()
except MMQTTException as error:
raise AWS_IOT_ERROR("Error re-connecting to AWS IoT:", error)

def connect(self, clean_session=True):
"""Connects to Amazon AWS IoT MQTT Broker with Client ID.
:param bool clean_session: Establishes a clean session with AWS broker.
Expand Down
28 changes: 18 additions & 10 deletions examples/aws_iot_shadows.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import time
import json
import board
import busio
Expand All @@ -6,7 +7,7 @@
from adafruit_esp32spi import adafruit_esp32spi
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_aws_iot import MQTT_CLIENT

### WiFi ###
Expand Down Expand Up @@ -129,15 +130,12 @@ def message(client, topic, msg):
wifi.connect()
print("Connected!")

# Initialize MQTT interface with the esp interface
MQTT.set_socket(socket, esp)

# Set up a new MiniMQTT Client
client = MQTT(
socket,
broker=secrets["broker"],
client_id=secrets["client_id"],
network_manager=wifi,
log=True,
)
client = MQTT.MQTT(broker=secrets["broker"],
client_id=secrets["client_id"])

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

# Attempt to loop forever and handle network interface
aws_iot.loop_forever()
# Start a blocking message loop...
# NOTE: NO code below this loop will execute
# NOTE: Network reconnection is handled within this loop
while True:
try:
aws_iot.loop()
except (ValueError, RuntimeError) as e:
print("Failed to get data, retrying\n", e)
wifi.reset()
aws_iot.reconnect()
continue
time.sleep(1)
29 changes: 19 additions & 10 deletions examples/aws_iot_simpletest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import time
import json
import board
import busio
Expand All @@ -6,7 +7,7 @@
from adafruit_esp32spi import adafruit_esp32spi
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_aws_iot import MQTT_CLIENT

### WiFi ###
Expand Down Expand Up @@ -126,14 +127,12 @@ def message(client, topic, msg):
wifi.connect()
print("Connected!")

# Initialize MQTT interface with the esp interface
MQTT.set_socket(socket, esp)

# Set up a new MiniMQTT Client
client = MQTT(
socket,
broker=secrets["broker"],
client_id=secrets["client_id"],
network_manager=wifi,
log=True,
)
client = MQTT.MQTT(broker=secrets["broker"],
client_id=secrets["client_id"])

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

# Attempt to loop forever and handle network interface
aws_iot.loop_forever()
# Start a blocking message loop...
# NOTE: NO code below this loop will execute
# NOTE: Network reconnection is handled within this loop
while True:
try:
aws_iot.loop()
except (ValueError, RuntimeError) as e:
print("Failed to get data, retrying\n", e)
wifi.reset()
aws_iot.reconnect()
continue
time.sleep(1)