diff --git a/adafruit_aws_iot.py b/adafruit_aws_iot.py index 50ea47a..1ffdd48 100644 --- a/adafruit_aws_iot.py +++ b/adafruit_aws_iot.py @@ -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 @@ -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: diff --git a/examples/aws_iot_native_networking.py b/examples/aws_iot_native_networking.py index 4f02dd7..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, ) @@ -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) diff --git a/examples/aws_iot_shadows.py b/examples/aws_iot_shadows.py index 0d9f9c0..7c33445 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, ) @@ -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() diff --git a/examples/aws_iot_simpletest.py b/examples/aws_iot_simpletest.py index 66a224b..e920152 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, ) @@ -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()