0

我正在研究 aws iot 并能够通过 shell 脚本获取终端更新的影子状态。但我能够获得 root@raspberrypi:~# ./aws_subscribe.py 与结果代码 0 以及 aws iot home i 的连接出现不同步错误附在下面。 我按照以下链接

4

1 回答 1

0

这是我所能得到的。这是我在树莓派上运行,所以我需要证书。我不确定您是否需要 lambda 上的证书。阴影状态在自定义回调中返回。这是他们决定这样做的方式。我仍然无法弄清楚如何在回调之外获取影子状态,因此您实际上可以用它做一些有用的事情,而不是打印。

   from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTShadowClient
   import logging
   import json 


def get(payload, responseStatus, token):
    dict = json.loads(payload)
    print(str(dict["state"]["desired"]["State"])) #make sure this matches your shadow names



# Read in command-line parameters
useWebsocket = False
host = "XXXXXYourINFO HEREXXXXXX.iot.us-east-1.amazonaws.com"
rootCAPath = "XXXXXYourINFO HEREXXXXXX"
certificatePath = "XXXXXYourINFO HEREXXXXXX"
privateKeyPath = "XXXXXYourINFO HEREXXXXXX"


# Configure logging
logger = logging.getLogger("AWSIoTPythonSDK.core")
logger.setLevel(logging.DEBUG)
streamHandler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
streamHandler.setFormatter(formatter)
logger.addHandler(streamHandler)

# Init AWSIoTMQTTShadowClient

myAWSIoTMQTTShadowClient = AWSIoTMQTTShadowClient("basicShadowUpdater")
myAWSIoTMQTTShadowClient.configureEndpoint(host, 8883)
myAWSIoTMQTTShadowClient.configureCredentials(rootCAPath, privateKeyPath, certificatePath)

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

# Connect to AWS IoT
myAWSIoTMQTTShadowClient.connect()

# Create a deviceShadow with persistent subscription
Bot = myAWSIoTMQTTShadowClient.createShadowHandlerWithName("GarageRBP", True)

#get the shadow here and the state is included in the custom callback
Bot.shadowGet(get, 5)
于 2017-01-12T13:46:21.180 回答