简体   繁体   English

AWS IoT shadowGet 超时

[英]AWS IoT shadowGet Timeout

I try to get AWS IoT script working on a Raspberry Pi using the AWS IoT Shadow functionality.我尝试使用 AWS IoT Shadow 功能让 AWS IoT 脚本在 Raspberry Pi 上运行。

When I try to get the current state with shadowGet(...) the returning callback (function customShadowCallback_Get) comes back with responseStatus=timeout.当我尝试使用 shadowGet(...) 获取当前 state 时,返回的回调(函数 customShadowCallback_Get)返回 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.我还尝试在 AWS IoT 控制台中创建一个名为“RPi3a-shadow”的影子,但它仍然不起作用。 What am I missing?我错过了什么?

Thanks so much!非常感谢!

You must have a timeout.你必须有一个超时。 Otherwise, it will disconnect before the API returns data否则会在API返回数据之前断开连接

print('Shadow get!')

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

Right idea, wrong execution

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM