From d742b4badb8bf1534b8af312ddf1b38f1b874ab9 Mon Sep 17 00:00:00 2001 From: Jeremy Sullivan Date: Fri, 17 May 2024 14:24:13 -0700 Subject: [PATCH 1/6] loop_forever method doesn't exist in MQTT client --- adafruit_aws_iot.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/adafruit_aws_iot.py b/adafruit_aws_iot.py index 50ea47a..2ba3f99 100644 --- a/adafruit_aws_iot.py +++ b/adafruit_aws_iot.py @@ -237,13 +237,6 @@ 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() - @staticmethod def validate_topic(topic: str) -> None: """Validates if user-provided pub/sub topics adhere to AWS Service Limits. From b8a9ba574b8930205edb658ef8e5285eaf41e0ab Mon Sep 17 00:00:00 2001 From: Jeremy Sullivan Date: Fri, 17 May 2024 14:29:40 -0700 Subject: [PATCH 2/6] adopts same method interface as client --- adafruit_aws_iot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_aws_iot.py b/adafruit_aws_iot.py index 2ba3f99..ce60098 100644 --- a/adafruit_aws_iot.py +++ b/adafruit_aws_iot.py @@ -222,7 +222,7 @@ 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. @@ -235,7 +235,7 @@ def loop(self) -> None: """ if self.connected_to_aws: - self.client.loop() + self.client.loop(timeout) @staticmethod def validate_topic(topic: str) -> None: From 0fe53e57b3122f10fae260cedb60a0940ba13f15 Mon Sep 17 00:00:00 2001 From: Jeremy Sullivan Date: Fri, 17 May 2024 14:33:50 -0700 Subject: [PATCH 3/6] adds ssl to example code connecting to AWS MQTT without TLS may not be possible --- examples/aws_iot_shadows.py | 1 + examples/aws_iot_simpletest.py | 1 + 2 files changed, 2 insertions(+) diff --git a/examples/aws_iot_shadows.py b/examples/aws_iot_shadows.py index 0d9f9c0..dff91cc 100644 --- a/examples/aws_iot_shadows.py +++ b/examples/aws_iot_shadows.py @@ -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, ) diff --git a/examples/aws_iot_simpletest.py b/examples/aws_iot_simpletest.py index 66a224b..5852ac9 100644 --- a/examples/aws_iot_simpletest.py +++ b/examples/aws_iot_simpletest.py @@ -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, ) From 4ea82736474d4a7987bd31d0c9dd3bbb6b5dae32 Mon Sep 17 00:00:00 2001 From: Jeremy Sullivan Date: Fri, 17 May 2024 14:41:27 -0700 Subject: [PATCH 4/6] added parameter documentation --- adafruit_aws_iot.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/adafruit_aws_iot.py b/adafruit_aws_iot.py index ce60098..1ffdd48 100644 --- a/adafruit_aws_iot.py +++ b/adafruit_aws_iot.py @@ -227,6 +227,8 @@ def loop(self, timeout: float = 0) -> None: 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 From b7a55b66a6273dbe8ea0c4b4ee870fa36b970763 Mon Sep 17 00:00:00 2001 From: Jeremy Sullivan Date: Sat, 18 May 2024 08:16:25 -0700 Subject: [PATCH 5/6] adding loop timeout gt zero if not set, loop() default timeout is zero and MQTT default loop() timeout is one, which will raise an MMQTTException. --- examples/aws_iot_native_networking.py | 2 +- examples/aws_iot_shadows.py | 4 ++-- examples/aws_iot_simpletest.py | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/aws_iot_native_networking.py b/examples/aws_iot_native_networking.py index 4f02dd7..fc1d839 100644 --- a/examples/aws_iot_native_networking.py +++ b/examples/aws_iot_native_networking.py @@ -118,6 +118,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) diff --git a/examples/aws_iot_shadows.py b/examples/aws_iot_shadows.py index dff91cc..7c33445 100644 --- a/examples/aws_iot_shadows.py +++ b/examples/aws_iot_shadows.py @@ -163,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() diff --git a/examples/aws_iot_simpletest.py b/examples/aws_iot_simpletest.py index 5852ac9..e920152 100644 --- a/examples/aws_iot_simpletest.py +++ b/examples/aws_iot_simpletest.py @@ -160,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() From e9565b2f921eaae81c78bea6c35090276898a704 Mon Sep 17 00:00:00 2001 From: Jeremy Sullivan Date: Sat, 18 May 2024 19:45:37 -0700 Subject: [PATCH 6/6] aligning all examples with same mqtt client settings When 'is_ssl' is true in MQTT (as is required by AWS IoT), the MQTT port is automatically set to 8883. --- examples/aws_iot_native_networking.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/examples/aws_iot_native_networking.py b/examples/aws_iot_native_networking.py index fc1d839..4f81593 100644 --- a/examples/aws_iot_native_networking.py +++ b/examples/aws_iot_native_networking.py @@ -17,7 +17,6 @@ # "device_cert_path" - Path to the Device Certificate from AWS IoT (".cert.pem") # "device_key_path" - Path to the RSA Private Key from AWS IoT (".private.key") # "broker" - The endpoint for the AWS IoT broker (".iot..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 @@ -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, )