3
3
4
4
from awsiot import mqtt5_client_builder
5
5
from awscrt import mqtt5
6
- import threading , time
6
+ import threading
7
+ import time
7
8
# This sample uses the Message Broker for AWS IoT to send and receive messages
8
9
# through an MQTT connection. On startup, the device connects to the server,
9
10
# subscribes to a topic, and begins publishing messages to that topic.
25
26
required .add_argument ("--endpoint" , required = True , metavar = "" , dest = "input_endpoint" ,
26
27
help = "IoT endpoint hostname" )
27
28
required .add_argument ("--cert" , required = True , metavar = "" , dest = "input_cert" ,
28
- help = "Path to the certificate file to use during mTLS connection establishment" )
29
+ help = "Path to the certificate file to use during mTLS connection establishment" )
29
30
required .add_argument ("--key" , required = True , metavar = "" , dest = "input_key" ,
30
- help = "Path to the private key file to use during mTLS connection establishment" )
31
+ help = "Path to the private key file to use during mTLS connection establishment" )
31
32
32
33
# Optional Arguments
33
- optional .add_argument ("--client_id" , metavar = "" ,dest = "input_clientId" , default = f"mqtt5-sample-{ uuid .uuid4 ().hex [:8 ]} " ,
34
+ optional .add_argument ("--client_id" , metavar = "" , dest = "input_clientId" , default = f"mqtt5-sample-{ uuid .uuid4 ().hex [:8 ]} " ,
34
35
help = "Client ID" )
35
- optional .add_argument ("--topic" , metavar = "" ,default = "test/topic" , dest = "input_topic" ,
36
+ optional .add_argument ("--topic" , metavar = "" , default = "test/topic" , dest = "input_topic" ,
36
37
help = "Topic" )
37
- optional .add_argument ("--message" , metavar = "" ,default = "Hello from mqtt5 sample" , dest = "input_message" ,
38
+ optional .add_argument ("--message" , metavar = "" , default = "Hello from mqtt5 sample" , dest = "input_message" ,
38
39
help = "Message payload" )
39
- optional .add_argument ("--count" , type = int , metavar = "" ,default = 5 , dest = "input_count" ,
40
+ optional .add_argument ("--count" , type = int , metavar = "" , default = 5 , dest = "input_count" ,
40
41
help = "Messages to publish (0 = infinite)" )
41
- optional .add_argument ("--ca_file" , metavar = "" , dest = "input_ca" ,
42
+ optional .add_argument ("--ca_file" , metavar = "" , dest = "input_ca" , default = None ,
42
43
help = "Path to root CA file" )
43
44
44
45
# args contains all the parsed commandline arguments used by the sample
@@ -119,11 +120,11 @@ def on_lifecycle_disconnection(lifecycle_disconnect_data: mqtt5.LifecycleDisconn
119
120
on_lifecycle_connection_success = on_lifecycle_connection_success ,
120
121
on_lifecycle_connection_failure = on_lifecycle_connection_failure ,
121
122
on_lifecycle_disconnection = on_lifecycle_disconnection ,
122
- client_id = args .input_clientId )
123
-
123
+ client_id = args .input_clientId ,
124
+ ca_filepath = args . input_ca )
124
125
125
- # Start the client, instructing the client to desire a connected state. The client will try to
126
- # establish a connection with the provided settings. If the client is disconnected while in this
126
+ # Start the client, instructing the client to desire a connected state. The client will try to
127
+ # establish a connection with the provided settings. If the client is disconnected while in this
127
128
# state it will attempt to reconnect automatically.
128
129
print ("==== Starting client ====" )
129
130
client .start ()
@@ -132,8 +133,7 @@ def on_lifecycle_disconnection(lifecycle_disconnect_data: mqtt5.LifecycleDisconn
132
133
if not connection_success_event .wait (TIMEOUT ):
133
134
raise TimeoutError ("Connection timeout" )
134
135
135
-
136
- # Subscribe
136
+ # Subscribe
137
137
print ("==== Subscribing to topic '{}' ====" .format (message_topic ))
138
138
subscribe_future = client .subscribe (subscribe_packet = mqtt5 .SubscribePacket (
139
139
subscriptions = [mqtt5 .Subscription (
@@ -143,7 +143,6 @@ def on_lifecycle_disconnection(lifecycle_disconnect_data: mqtt5.LifecycleDisconn
143
143
suback = subscribe_future .result (TIMEOUT )
144
144
print ("Suback received with reason code:{}\n " .format (suback .reason_codes ))
145
145
146
-
147
146
# Publish
148
147
if message_count == 0 :
149
148
print ("==== Sending messages until program killed ====\n " )
@@ -174,7 +173,6 @@ def on_lifecycle_disconnection(lifecycle_disconnect_data: mqtt5.LifecycleDisconn
174
173
unsuback = unsubscribe_future .result (TIMEOUT )
175
174
print ("Unsubscribed with {}\n " .format (unsuback .reason_codes ))
176
175
177
-
178
176
# Stop the client. Instructs the client to disconnect and remain in a disconnected state.
179
177
print ("==== Stopping Client ====" )
180
178
client .stop ()
0 commit comments