'AWS IoT shadowGet Timeout

I try to get AWS IoT script working on a Raspberry Pi using the AWS IoT Shadow functionality.

When I try to get the current state with shadowGet(...) the returning callback (function customShadowCallback_Get) comes back with responseStatus=timeout.

Here is the code:

from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTShadowClient

# Custom Shadow callback
def customShadowCallback_Get(payload, responseStatus, token):
    print(responseStatus)
    payloadDict = json.loads(payload)
    print("++++++++GET++++++++++")
    print("property: " + str(payloadDict["state"]["property"]))
    print("version: " + str(payloadDict["version"]))
    print("+++++++++++++++++++++++\n\n")

# Init AWSIoTMQTTShadowClient
myAWSIoTMQTTShadowClient = AWSIoTMQTTShadowClient('RPi3a')
myAWSIoTMQTTShadowClient.configureEndpoint(host, port)
myAWSIoTMQTTShadowClient.configureCredentials(rootCAPath, privateKeyPath, certificatePath)

# AWSIoTMQTTShadowClient connection configuration
myAWSIoTMQTTShadowClient.configureAutoReconnectBackoffTime(1, 32, 20)
myAWSIoTMQTTShadowClient.configureConnectDisconnectTimeout(10)  # 10 sec
myAWSIoTMQTTShadowClient.configureMQTTOperationTimeout(5)  # 5 sec

# Connect and subscribe to AWS IoT
myAWSIoTMQTTShadowClient.connect()

# Create a deviceShadow with persistent subscription
deviceShadowHandler = myAWSIoTMQTTShadowClient.createShadowHandlerWithName('RPi3a-shadow', True)

# get current shadow status.
deviceShadowHandler.shadowGet(customShadowCallback_Get, 5)
print('Shadow get!')

while True:
    time.sleep(1)
    
myAWSIoTMQTTShadowClient.disconnect()

The relevant policy is:

  {
      "Effect": "Allow",
      "Action": [
        "iot:UpdateThingShadow",
        "iot:GetThingShadow",
        "iot:DeleteThingShadow",
        "iot:Subscribe"
      ],
      "Resource": "arn:aws:iot:us-east-1:<mycode>:topicfilter/$aws/things/RPi3a-shadow/*"
    }

Does anybody have an idea why I get this timeout? I also tried creating a named shadow "RPi3a-shadow" in the AWS IoT console, but still it does not work. What am I missing?

Thanks so much!



Solution 1:[1]

You must have a timeout. Otherwise, it will disconnect before the API returns data

print('Shadow get!')

time.sleep(2)
    
myAWSIoTMQTTShadowClient.disconnect()```

Right idea, wrong execution

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1