33
44from awsiot import mqtt5_client_builder
55from awscrt import mqtt5
6- import threading , time
6+ import threading
7+ import time
78# This sample uses the Message Broker for AWS IoT to send and receive messages
89# through an MQTT connection. On startup, the device connects to the server,
910# subscribes to a topic, and begins publishing messages to that topic.
2526required .add_argument ("--endpoint" , required = True , metavar = "" , dest = "input_endpoint" ,
2627 help = "IoT endpoint hostname" )
2728required .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" )
2930required .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" )
3132
3233# 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 ]} " ,
3435 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" ,
3637 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" ,
3839 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" ,
4041 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 ,
4243 help = "Path to root CA file" )
4344
4445# args contains all the parsed commandline arguments used by the sample
@@ -119,11 +120,11 @@ def on_lifecycle_disconnection(lifecycle_disconnect_data: mqtt5.LifecycleDisconn
119120 on_lifecycle_connection_success = on_lifecycle_connection_success ,
120121 on_lifecycle_connection_failure = on_lifecycle_connection_failure ,
121122 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 )
124125
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
127128 # state it will attempt to reconnect automatically.
128129 print ("==== Starting client ====" )
129130 client .start ()
@@ -132,8 +133,7 @@ def on_lifecycle_disconnection(lifecycle_disconnect_data: mqtt5.LifecycleDisconn
132133 if not connection_success_event .wait (TIMEOUT ):
133134 raise TimeoutError ("Connection timeout" )
134135
135-
136- # Subscribe
136+ # Subscribe
137137 print ("==== Subscribing to topic '{}' ====" .format (message_topic ))
138138 subscribe_future = client .subscribe (subscribe_packet = mqtt5 .SubscribePacket (
139139 subscriptions = [mqtt5 .Subscription (
@@ -143,7 +143,6 @@ def on_lifecycle_disconnection(lifecycle_disconnect_data: mqtt5.LifecycleDisconn
143143 suback = subscribe_future .result (TIMEOUT )
144144 print ("Suback received with reason code:{}\n " .format (suback .reason_codes ))
145145
146-
147146 # Publish
148147 if message_count == 0 :
149148 print ("==== Sending messages until program killed ====\n " )
@@ -174,7 +173,6 @@ def on_lifecycle_disconnection(lifecycle_disconnect_data: mqtt5.LifecycleDisconn
174173 unsuback = unsubscribe_future .result (TIMEOUT )
175174 print ("Unsubscribed with {}\n " .format (unsuback .reason_codes ))
176175
177-
178176 # Stop the client. Instructs the client to disconnect and remain in a disconnected state.
179177 print ("==== Stopping Client ====" )
180178 client .stop ()
0 commit comments