Skip to content

Commit

Permalink
Merge pull request #27 from jersu11/main
Browse files Browse the repository at this point in the history
Update interface to match MQTT client
  • Loading branch information
justmobilize authored May 28, 2024
2 parents f6cd827 + e9565b2 commit 64f1804
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 17 deletions.
13 changes: 4 additions & 9 deletions adafruit_aws_iot.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,13 @@ def _on_unsubscribe_mqtt(
self.on_unsubscribe(self, user_data, topic, pid)

# MiniMQTT Network Control Flow
def loop(self) -> None:
def loop(self, timeout: float = 0) -> None:
"""Starts a synchronous message loop which maintains connection with AWS IoT.
Must be called within the keep_alive timeout specified to init.
This method does not handle network connection/disconnection.
:param float timeout: client return after this timeout, in seconds.
Example of "pumping" an AWS IoT message loop:
..code-block::python
Expand All @@ -235,14 +237,7 @@ def loop(self) -> None:
"""
if self.connected_to_aws:
self.client.loop()

def loop_forever(self) -> None:
"""Begins a blocking, asynchronous message loop.
This method handles network connection/disconnection.
"""
if self.connected_to_aws:
self.client.loop_forever()
self.client.loop(timeout)

@staticmethod
def validate_topic(topic: str) -> None:
Expand Down
6 changes: 2 additions & 4 deletions examples/aws_iot_native_networking.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
# "device_cert_path" - Path to the Device Certificate from AWS IoT ("<THING_NAME>.cert.pem")
# "device_key_path" - Path to the RSA Private Key from AWS IoT ("<THING_NAME>.private.key")
# "broker" - The endpoint for the AWS IoT broker ("<PREFIX>.iot.<REGION>.amazonaws.com")
# "port" - The port for the "broker" above (8883)
# "client_id" - The client id. Your device's Policy needs to allow this client ("basicPubSub")
#
# pylint: disable=no-name-in-module,wrong-import-order
Expand Down Expand Up @@ -93,9 +92,8 @@ def message(client, topic, msg):
# Set up a MiniMQTT Client
mqtt_client = MQTT.MQTT(
broker=secrets["broker"],
port=secrets["port"],
is_ssl=True, # ssl is required
client_id=secrets["client_id"],
is_ssl=True,
socket_pool=pool,
ssl_context=ssl_context,
)
Expand All @@ -118,6 +116,6 @@ def message(client, topic, msg):
# NOTE: NO code below this loop will execute
# NOTE: Network reconnection is NOT handled within this loop
while True:
aws_iot.loop()
aws_iot.loop(10)

time.sleep(1)
5 changes: 3 additions & 2 deletions examples/aws_iot_shadows.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ def message(client, topic, msg):
client = MQTT.MQTT(
broker=secrets["broker"],
client_id=secrets["client_id"],
is_ssl=True,
socket_pool=pool,
ssl_context=ssl_context,
)
Expand All @@ -162,14 +163,14 @@ def message(client, topic, msg):
# Pump the message loop forever, all events
# are handled in their callback handlers
# while True:
# aws_iot.loop()
# aws_iot.loop(10)

# 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()
aws_iot.loop(10)
except (ValueError, RuntimeError) as e:
print("Failed to get data, retrying\n", e)
wifi.reset()
Expand Down
5 changes: 3 additions & 2 deletions examples/aws_iot_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ def message(client, topic, msg):
client = MQTT.MQTT(
broker=secrets["broker"],
client_id=secrets["client_id"],
is_ssl=True,
socket_pool=pool,
ssl_context=ssl_context,
)
Expand All @@ -159,14 +160,14 @@ def message(client, topic, msg):
# Pump the message loop forever, all events
# are handled in their callback handlers
# while True:
# aws_iot.loop()
# aws_iot.loop(10)

# 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()
aws_iot.loop(10)
except (ValueError, RuntimeError) as e:
print("Failed to get data, retrying\n", e)
wifi.reset()
Expand Down

0 comments on commit 64f1804

Please sign in to comment.