Skip to content

Commit 64f1804

Browse files
authored
Merge pull request #27 from jersu11/main
Update interface to match MQTT client
2 parents f6cd827 + e9565b2 commit 64f1804

File tree

4 files changed

+12
-17
lines changed

4 files changed

+12
-17
lines changed

adafruit_aws_iot.py

+4-9
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,13 @@ def _on_unsubscribe_mqtt(
222222
self.on_unsubscribe(self, user_data, topic, pid)
223223

224224
# MiniMQTT Network Control Flow
225-
def loop(self) -> None:
225+
def loop(self, timeout: float = 0) -> None:
226226
"""Starts a synchronous message loop which maintains connection with AWS IoT.
227227
Must be called within the keep_alive timeout specified to init.
228228
This method does not handle network connection/disconnection.
229229
230+
:param float timeout: client return after this timeout, in seconds.
231+
230232
Example of "pumping" an AWS IoT message loop:
231233
..code-block::python
232234
@@ -235,14 +237,7 @@ def loop(self) -> None:
235237
236238
"""
237239
if self.connected_to_aws:
238-
self.client.loop()
239-
240-
def loop_forever(self) -> None:
241-
"""Begins a blocking, asynchronous message loop.
242-
This method handles network connection/disconnection.
243-
"""
244-
if self.connected_to_aws:
245-
self.client.loop_forever()
240+
self.client.loop(timeout)
246241

247242
@staticmethod
248243
def validate_topic(topic: str) -> None:

examples/aws_iot_native_networking.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
# "device_cert_path" - Path to the Device Certificate from AWS IoT ("<THING_NAME>.cert.pem")
1818
# "device_key_path" - Path to the RSA Private Key from AWS IoT ("<THING_NAME>.private.key")
1919
# "broker" - The endpoint for the AWS IoT broker ("<PREFIX>.iot.<REGION>.amazonaws.com")
20-
# "port" - The port for the "broker" above (8883)
2120
# "client_id" - The client id. Your device's Policy needs to allow this client ("basicPubSub")
2221
#
2322
# pylint: disable=no-name-in-module,wrong-import-order
@@ -93,9 +92,8 @@ def message(client, topic, msg):
9392
# Set up a MiniMQTT Client
9493
mqtt_client = MQTT.MQTT(
9594
broker=secrets["broker"],
96-
port=secrets["port"],
97-
is_ssl=True, # ssl is required
9895
client_id=secrets["client_id"],
96+
is_ssl=True,
9997
socket_pool=pool,
10098
ssl_context=ssl_context,
10199
)
@@ -118,6 +116,6 @@ def message(client, topic, msg):
118116
# NOTE: NO code below this loop will execute
119117
# NOTE: Network reconnection is NOT handled within this loop
120118
while True:
121-
aws_iot.loop()
119+
aws_iot.loop(10)
122120

123121
time.sleep(1)

examples/aws_iot_shadows.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ def message(client, topic, msg):
141141
client = MQTT.MQTT(
142142
broker=secrets["broker"],
143143
client_id=secrets["client_id"],
144+
is_ssl=True,
144145
socket_pool=pool,
145146
ssl_context=ssl_context,
146147
)
@@ -162,14 +163,14 @@ def message(client, topic, msg):
162163
# Pump the message loop forever, all events
163164
# are handled in their callback handlers
164165
# while True:
165-
# aws_iot.loop()
166+
# aws_iot.loop(10)
166167

167168
# Start a blocking message loop...
168169
# NOTE: NO code below this loop will execute
169170
# NOTE: Network reconnection is handled within this loop
170171
while True:
171172
try:
172-
aws_iot.loop()
173+
aws_iot.loop(10)
173174
except (ValueError, RuntimeError) as e:
174175
print("Failed to get data, retrying\n", e)
175176
wifi.reset()

examples/aws_iot_simpletest.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ def message(client, topic, msg):
138138
client = MQTT.MQTT(
139139
broker=secrets["broker"],
140140
client_id=secrets["client_id"],
141+
is_ssl=True,
141142
socket_pool=pool,
142143
ssl_context=ssl_context,
143144
)
@@ -159,14 +160,14 @@ def message(client, topic, msg):
159160
# Pump the message loop forever, all events
160161
# are handled in their callback handlers
161162
# while True:
162-
# aws_iot.loop()
163+
# aws_iot.loop(10)
163164

164165
# Start a blocking message loop...
165166
# NOTE: NO code below this loop will execute
166167
# NOTE: Network reconnection is handled within this loop
167168
while True:
168169
try:
169-
aws_iot.loop()
170+
aws_iot.loop(10)
170171
except (ValueError, RuntimeError) as e:
171172
print("Failed to get data, retrying\n", e)
172173
wifi.reset()

0 commit comments

Comments
 (0)