### Run basicPubSub.py example Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Command-line invocations for the basicPubSub.py example. Supports certificate-based mutual authentication, MQTT over WebSocket, and options for client id, topic, message, port, and run mode. ```python # Certificate based mutual authentication python basicPubSub.py -e -r -c -k # MQTT over WebSocket python basicPubSub.py -e -r -w # Customize client id and topic python basicPubSub.py -e -r -c -k -id -t # Customize the message python basicPubSub.py -e -r -c -k -id -t -M # Customize the port number python basicPubSub.py -e -r -c -k -p # change the run mode to subscribe or publish only (see python basicPubSub.py -h for the available options) python basicPubSub.py -e -r -c -k -m ``` -------------------------------- ### Run basicPubSub_CognitoSTS.py example Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Command-line invocations for the basicPubSub_CognitoSTS.py example. Requires an Amazon Cognito identity pool ID and unauthenticated identities enabled. ```python python basicPubSub_CognitoSTS.py -e -r -C # Customize client id and topic python basicPubsub_CognitoSTS.py -e -r -C -id -t ``` -------------------------------- ### Perform Jobs Operations with AWSIoTMQTTThingJobsClient Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Connects the jobs client and performs job operations: create subscriptions for notify-next and update accepted topics, start next pending job, and update job status. ```python ... myJobsClient.connect() # Create a subsciption for $notify-next topic myJobsClient.createJobSubscription(notifyNextCallback, jobExecutionTopicType.JOB_NOTIFY_NEXT_TOPIC) # Create a subscription for update-job-execution accepted response topic myJobsClient.createJobSubscription(updateSuccessfulCallback, jobExecutionTopicType.JOB_UPDATE_TOPIC, jobExecutionTopicReplyType.JOB_ACCEPTED_REPLY_TYPE, '+') # Send a message to start the next pending job (if any) myJobsClient.sendJobsStartNext(statusDetailsDict) # Send a message to update a successfully completed job myJobsClient.sendJobsUpdate(jobId, jobExecutionStatus.JOB_EXECUTION_SUCCEEDED, statusDetailsDict) ... ``` -------------------------------- ### Initialize and configure AWSIoTMQTTClient Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Initialize and configure the AWSIoTMQTTClient for plain MQTT communication with AWS IoT. The example shows certificate-based connection with endpoint port 8883 and commented alternatives for Websocket (port 443) and TLS ALPN. Configure credentials with root CA, private key, and certificate paths; for Websocket only the root CA is needed. The configuration calls set offline publish queueing, draining frequency, connect/disconnect timeout, and MQTT operation timeout. ```python # Import SDK packages from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient # For certificate based connection myMQTTClient = AWSIoTMQTTClient("myClientID") # For Websocket connection # myMQTTClient = AWSIoTMQTTClient("myClientID", useWebsocket=True) # Configurations # For TLS mutual authentication myMQTTClient.configureEndpoint("YOUR.ENDPOINT", 8883) # For Websocket # myMQTTClient.configureEndpoint("YOUR.ENDPOINT", 443) # For TLS mutual authentication with TLS ALPN extension # myMQTTClient.configureEndpoint("YOUR.ENDPOINT", 443) myMQTTClient.configureCredentials("YOUR/ROOT/CA/PATH", "PRIVATE/KEY/PATH", "CERTIFICATE/PATH") # For Websocket, we only need to configure the root CA # myMQTTClient.configureCredentials("YOUR/ROOT/CA/PATH") myMQTTClient.configureOfflinePublishQueueing(-1) # Infinite offline Publish queueing myMQTTClient.configureDrainingFrequency(2) # Draining: 2 Hz myMQTTClient.configureConnectDisconnectTimeout(10) # 10 sec myMQTTClient.configureMQTTOperationTimeout(5) # 5 sec ... ``` -------------------------------- ### Configure custom SSL ciphers with configureCredentials Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Configures custom SSL ciphers for the MQTT client before starting the connection. The Ciphers parameter accepts a string specifying the cipher suite. ```python myAWSIoTMQTTClient.configureCredentials(rootCAPath, privateKeyPath, certificatePath, Ciphers="AES128-SHA256") ``` -------------------------------- ### Access Greengrass discovery information at group/core/connectivity levels Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Shows how to use the discovery info provider to retrieve all cores, all CAs, all groups, and specific connectivity info for a given group, core, and connectivity ID. The final call chains multiple methods to get the exact connectivity info needed for connecting. ```python discoveryInfo = discoveryInfoProvider.discover("myGGADThingName") # I know nothing about the group/core I want to connect to. I want to iterate through all cores and find out. coreList = discoveryInfo.getAllCores() groupIdCAList = discoveryInfo.getAllCas() # list([(groupId, ca), ...]) # I know nothing about the group/core I want to connect to. I want to iterate through all groups and find out. groupList = discoveryInfo.getAllGroups() # I know exactly which group, which core and which connectivity info I need to connect. connectivityInfo = discoveryInfo.toObjectAtGroupLevel()["YOUR_GROUP_ID"] .getCoreConnectivityInfo("YOUR_CORE_THING_ARN") .getConnectivityInfo("YOUR_CONNECTIVITY_ID") # Connecting logic follows... ... ``` -------------------------------- ### Get MQTT Connection from Shadow Client Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Retrieves the underlying MQTT connection from the shadow client to perform plain MQTT operations alongside shadow operations. ```python myMQTTClient = myShadowClient.getMQTTConnection() myMQTTClient.publish("plainMQTTTopic", "Payload", 1) ``` -------------------------------- ### Perform Shadow Operations with AWSIoTMQTTShadowClient Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Connects the shadow client and performs shadow operations: get, update, delete, and delta callbacks. Uses persistent subscription for the shadow handler. ```python ... myShadowClient.connect() # Create a device shadow instance using persistent subscription myDeviceShadow = myShadowClient.createShadowHandlerWithName("Bot", True) # Shadow operations myDeviceShadow.shadowGet(customCallback, 5) myDeviceShadow.shadowUpdate(myJSONPayload, customCallback, 5) myDeviceShadow.shadowDelete(customCallback, 5) myDeviceShadow.shadowRegisterDeltaCallback(customCallback) myDeviceShadow.shadowUnregisterDeltaCallback() ... ``` -------------------------------- ### Persistent shadow subscription topics Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Topics subscribed to on the first Get request when using a persistent shadow handler. The SDK client remains subscribed to these accepted/rejected topics. ```sh $aws/things/PersistentSubShadow/shadow/get/accepted $aws/things/PersistentSubShadow/shadow/get/rejected ``` -------------------------------- ### Get MQTT Connection from Jobs Client Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Retrieves the underlying MQTT connection from the jobs client to perform plain MQTT operations alongside jobs operations. ```python myMQTTClient = myJobsClient.getMQTTConnection() myMQTTClient.publish("plainMQTTTopic", "Payload", 1) ``` -------------------------------- ### Initialize DiscoveryInfoProvider Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Initializes and configures a DiscoveryInfoProvider for AWS IoT Greengrass device discovery. Requires endpoint, credentials, and timeout. ```python from AWSIoTPythonSDK.core.greengrass.discovery.providers import DiscoveryInfoProvider discoveryInfoProvider = DiscoveryInfoProvider() discoveryInfoProvider.configureEndpoint("YOUR.IOT.ENDPOINT") discoveryInfoProvider.configureCredentials("YOUR/ROOT/CA/PATH", "CERTIFICATE/PATH", "PRIVATE/KEY/PATH") discoveryInfoProvider.configureTimeout(10) # 10 sec ``` -------------------------------- ### Run basicDiscovery sample Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Runs the basicDiscovery sample to perform Greengrass discovery. Requires a deployed Greengrass group, the GGAD certificate/private key, and a routing topic. Replace , , , , , and with your values. ```python python basicDiscovery.py -e -r -c -k -n -t ``` -------------------------------- ### Run basicPubSubAsync.py with MQTT over WebSocket Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Connects using MQTT over WebSocket by passing the -w flag. Requires the endpoint and root CA file path. ```bash python basicPubSubAsync.py -e -r -w ``` -------------------------------- ### Run ThingShadowEcho sample with custom client ID and thing name Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Runs the ThingShadowEcho sample with a custom client ID and thing name using the -id and -n flags. ```python python ThingShadowEcho.py -e -r -c -k -id -n ``` -------------------------------- ### Initialize AWSIoTMQTTThingJobsClient Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Initializes and configures an AWSIoTMQTTThingJobsClient for AWS IoT jobs operations. Supports certificate-based and Websocket connections, with commented alternatives for TLS ALPN. Requires endpoint, credentials, and timeouts. ```python from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTThingJobsClient # For certificate based connection myJobsClient = AWSIoTMQTTThingJobsClient("myClientID", "myThingName") # For Websocket connection # myJobsClient = AWSIoTMQTTThingJobsClient("myClientID", "myThingName", useWebsocket=True) # Configurations # For TLS mutual authentication myJobsClient.configureEndpoint("YOUR.ENDPOINT", 8883) # For Websocket # myJobsClient.configureEndpoint("YOUR.ENDPOINT", 443) myJobsClient.configureCredentials("YOUR/ROOT/CA/PATH", "PRIVATE/KEY/PATH", "CERTIFICATE/PATH") # For Websocket, we only need to configure the root CA # myJobsClient.configureCredentials("YOUR/ROOT/CA/PATH") myJobsClient.configureConnectDisconnectTimeout(10) # 10 sec myJobsClient.configureMQTTOperationTimeout(5) # 5 sec ... ``` -------------------------------- ### Run ThingShadowEcho sample with MQTT over WebSocket Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Runs the ThingShadowEcho sample using MQTT over WebSocket. The -w flag enables WebSocket connectivity. ```python python ThingShadowEcho.py -e -r -w ``` -------------------------------- ### Run basicPubSubAsync.py with custom client id and topic Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Customizes the client id and topic for the MQTT connection. Uses the -id and -t flags to override defaults. ```bash python basicPubSubAsync.py -e -r -c -k -id -t ``` -------------------------------- ### Initialize AWSIoTMQTTShadowClient Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Initializes and configures an AWSIoTMQTTShadowClient for device shadow operations. Supports certificate-based and Websocket connections, with commented alternatives for TLS ALPN. Requires endpoint, credentials, and timeouts. ```python from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTShadowClient # For certificate based connection myShadowClient = AWSIoTMQTTShadowClient("myClientID") # For Websocket connection # myMQTTClient = AWSIoTMQTTClient("myClientID", useWebsocket=True) # Configurations # For TLS mutual authentication myShadowClient.configureEndpoint("YOUR.ENDPOINT", 8883) # For Websocket # myShadowClient.configureEndpoint("YOUR.ENDPOINT", 443) # For TLS mutual authentication with TLS ALPN extension # myShadowClient.configureEndpoint("YOUR.ENDPOINT", 443) myShadowClient.configureCredentials("YOUR/ROOT/CA/PATH", "PRIVATE/KEY/PATH", "CERTIFICATE/PATH") # For Websocket, we only need to configure the root CA # myShadowClient.configureCredentials("YOUR/ROOT/CA/PATH") myShadowClient.configureConnectDisconnectTimeout(10) # 10 sec myShadowClient.configureMQTTOperationTimeout(5) # 5 sec ... ``` -------------------------------- ### Run basicPubSubAsync.py with certificate-based mutual authentication Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Connects to AWS IoT using a root CA, client certificate, and private key. Replace the placeholders with your endpoint and file paths before running. ```bash python basicPubSubAsync.py -e -r -c -k ``` -------------------------------- ### Run ThingShadowEcho sample with custom port number Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Runs the ThingShadowEcho sample with a custom port number using the -p flag. ```python python ThingShadowEcho.py -e -r -c -k -p ``` -------------------------------- ### Run ThingShadowEcho sample with certificate-based mutual authentication Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Runs the ThingShadowEcho sample using certificate-based mutual authentication. Replace , , , and with your AWS IoT endpoint and file paths. ```python python ThingShadowEcho.py -e -r -c -k ``` -------------------------------- ### Run basicPubSubAsync.py with a custom port number Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Overrides the default MQTT port with the -p flag. Use this when the broker listens on a non-default port. ```bash python basicPubSubAsync.py -e -r -c -k -p ``` -------------------------------- ### Run jobsSample with MQTT over WebSocket Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Runs the jobsSample using MQTT over WebSocket. The -w flag enables WebSocket connectivity. ```python python jobsSample.py -e -r -w -n ``` -------------------------------- ### Run jobsSample with custom client ID and thing name Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Runs the jobsSample with a custom client ID and thing name using the -id and -n flags. ```python python jobsSample.py -e -r -c -k -id -n ``` -------------------------------- ### Run jobsSample with certificate-based mutual authentication Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Runs the jobsSample using certificate-based mutual authentication. Replace , , , , and with your values. ```python python jobsSample.py -e -r -c -k -n ``` -------------------------------- ### Configure Queue with DROP_NEWEST Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Configures the offline publish queue with a size of 5 and DROP_NEWEST behavior. When the queue is full, the newest requests are discarded as new ones arrive. ```python myClient.configureOfflinePublishQueueing(5, AWSIoTPythonSDK.MQTTLib.DROP_NEWEST); ``` -------------------------------- ### Basic MQTT operations with AWSIoTMQTTClient Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Perform basic MQTT operations with the AWSIoTMQTTClient: connect, publish, subscribe, unsubscribe, and disconnect. The publish call uses QoS 0 and the subscribe call uses QoS 1 with a custom callback. ```python ... myMQTTClient.connect() myMQTTClient.publish("myTopic", "myPayload", 0) myMQTTClient.subscribe("myTopic", 1, customCallback) myMQTTClient.unsubscribe("myTopic") myMQTTClient.disconnect() ... ``` -------------------------------- ### Configure Queue with DROP_OLDEST Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Configures the offline publish queue with a size of 5 and DROP_OLDEST behavior. When the queue is full, the oldest requests are discarded as new ones arrive. ```python myClient.configureOfflinePublishQueueing(5, AWSIoTPythonSDK.MQTTLib.DROP_OLDEST); ``` -------------------------------- ### Create Non-Persistent Subscription Shadow Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Creates a device shadow with non-persistent subscription. The client subscribes to accepted/rejected topics for each shadow operation and unsubscribes after the response is returned. ```python nonPersistentSubShadow = myShadowClient.createShadowHandlerWithName("NonPersistentSubShadow", False) ``` -------------------------------- ### Configure shared credentials file Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Configure the shared credentials file in the default section. The SDK checks this file if no environment variables are set. On Unix the file is at ~/.aws/credentials and on Windows at %UserProfile%\.aws\credentials. ```sh [default] aws_access_key_id=foo aws_secret_access_key=bar aws_session_token=baz ``` -------------------------------- ### Create persistent shadow handler with createShadowHandlerWithName Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Creates a shadow handler with a persistent subscription. The subscription to accepted/rejected topics is sent on the first shadow operation, and no unsubscribe requests are sent afterward. ```python persistentSubShadow = myShadowClient.createShadowHandlerWithName("PersistentSubShadow", True) ``` -------------------------------- ### Default reconnect backoff timing values Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Default configuration applied on initialization if configureAutoReconnectBackoffTime is not called. The base quiet time is 1 second, maximum is 32 seconds, and stable connection time is 20 seconds. ```python baseReconnectQuietTimeSecond = 1 maxReconnectQuietTimeSecond = 32 stableConnectionTimeSecond = 20 ``` -------------------------------- ### Queue State When Full (DROP_OLDEST) Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Shows the internal queue state when the queue is just full with DROP_OLDEST configuration. This is the state before the 6th and 7th requests are made. ```sh HEAD ['pub_req1', 'pub_req2', 'pub_req3', 'pub_req4', 'pub_req5'] ``` -------------------------------- ### Configure IAM credentials via method parameters Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Use this method to pass custom IAM credentials directly to the AWS IoT MQTT client or shadow client. The SDK checks this configuration first before falling back to environment variables or the shared credentials file. Avoid hard-coding credentials in scripts; consider Amazon Cognito Identity or another credential provider. ```python # AWS IoT MQTT Client AWSIoTPythonSDK.MQTTLib.AWSIoTMQTTClient.configureIAMCredentials(obtainedAccessKeyID, obtainedSecretAccessKey, obtainedSessionToken) # AWS IoT MQTT Shadow Client AWSIoTPythonSDK.MQTTLib.AWSIoTMQTTShadowClient.configureIAMCredentials(obtainedAccessKeyID, obtainedSecretAccessKey, obtainedSessionToken) ``` -------------------------------- ### Configure Offline Publish Queueing Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Configures the offline publish queue size and drop behavior for plain MQTT operations. This is used when the client is temporarily offline and requests are queued until the queue limit is reached. ```python AWSIoTPythonSDK.MQTTLib.AWSIoTMQTTClient.configureOfflinePublishQueueing(queueSize, dropBehavior) ``` -------------------------------- ### Configure auto-reconnect backoff time for AWS IoT MQTT clients Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Shows the API calls to configure progressive reconnect backoff for both the AWSIoTMQTTClient and AWSIoTMQTTShadowClient. The backoff time doubles on each disconnect/reconnect attempt until reaching the maximum, and resets after the connection is stable for the specified stable connection time. ```python # AWS IoT MQTT Client AWSIoTPythonSDK.MQTTLib.AWSIoTMQTTClient.configureAutoReconnectBackoffTime(baseReconnectQuietTimeSecond, maxReconnectQuietTimeSecond, stableConnectionTimeSecond) # AWS IoT MQTT Shadow Client AWSIoTPythonSDK.MQTTLib.AWSIoTMQTTShadowClient.configureAutoReconnectBackoffTime(baseReconnectQuietTimeSecond, maxReconnectQuietTimeSecond, stableConnectionTimeSecond) ``` -------------------------------- ### Export IAM credentials as environment variables Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Set IAM credentials as environment variables for Unix systems. The SDK checks these variables if no custom configuration was provided via method calls. AWS_SESSION_TOKEN is required only when using temporary credentials. ```sh export AWS_ACCESS_KEY_ID= export AWS_SECRET_ACCESS_KEY= export AWS_SESSION_TOKEN= ``` -------------------------------- ### Default Offline Queue Configuration Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Default configuration for offline request queueing and draining when no explicit configuration is provided. The queue size is 20, drop behavior is DROP_NEWEST, and draining frequency is 2Hz. ```python offlinePublishQueueSize = 20 dropBehavior = DROP_NEWEST drainingFrequency = 2Hz ``` -------------------------------- ### Queue State After Overflow (DROP_OLDEST) Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Shows the internal queue state after the 6th and 7th offline publish requests are made with DROP_OLDEST. The oldest requests pub_req1 and pub_req2 are discarded. ```sh HEAD ['pub_req3', 'pub_req4', 'pub_req5', 'pub_req6', 'pub_req7'] ``` -------------------------------- ### Run jobsSample with custom port number Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Runs the jobsSample with a custom port number using the -p flag. ```python python jobsSample.py -e -r -c -k -n -p ``` -------------------------------- ### Configure shared credentials with AWS CLI Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Use the AWS CLI to configure the shared credentials file. This command is an alternative to manually editing the credentials file. ```sh aws configure ``` -------------------------------- ### Drop Behavior Constants Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Defines the drop behavior constants for the offline publish queue. DROP_OLDEST discards the oldest queued request when the queue is full, while DROP_NEWEST discards the newest. ```python # Drop the oldest request in the queue AWSIoTPythonSDK.MQTTLib.DROP_OLDEST = 0 # Drop the newest request in the queue AWSIoTPythonSDK.MQTTLib.DROP_NEWEST = 1 ``` -------------------------------- ### Configure Draining Frequency Source: https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst Configures the draining frequency in Hz for the offline request queue. When the client reconnects, queued requests are resent at this rate. ```python AWSIoTPythonSDK.MQTTLib.AWSIoTMQTTClient.configureDrainingFrequency(frequencyInHz) ```