【问题标题】:Connecting senesor to aws iot将传感器连接到 aws iot
【发布时间】:2020-03-03 13:51:22
【问题描述】:

我们曾尝试将传感器连接到 aws iot。传感器已连接到 aws iot,但我们无法在影子上查看消息。我们的影子没有得到更新。请提出一些方法,以便我们可以更新我们的影子。

【问题讨论】:

  • 欢迎使用 stackoverflow。请添加有关您尝试解决问题的内容或方式的更多信息。更容易理解实际问题。
  • 嗨!我正在将传感器数据连接到 aws iot 云。在连接期间,影子必须更新,以便我们可以看到使用 MOTT 协议发送到云的消息。问题是影子没有得到更新。

标签: amazon-web-services


【解决方案1】:

下面是我为与 AWS IoT 阴影交互而创建的一个类,它具有读取、更新和删除阴影的方法。这里的主要内容是我使用boto3 client 连接到 AWS IoT,这允许我推送和拉取 JSON 以存储在云中(事物的影子)。

import boto3
import logging
import json

# Initialize logger for CloudWatch logs
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)


class IoTOps:
    """
    Params: iotClient boto3 client used to update thing's shadow and also send 
    messages to the thing
        topic STRING - the MQTT Topic to send a message to
        QoS - INT - Essentially the channel of a specific topic to communicate
        over
    """
    def __init__(self, iotConfig):
        self.iotClient = boto3.client('iot-data', region_name='us-east-1')
        self.topic = iotConfig['topic']
        self.QoS = iotConfig['QoS']
        self.thingName = iotConfig['thingName']

    def publish_mqtt_message(self, payload):
        """                                                                        
        Description: Send MQTT message to MQTT Topic                         

        Return: None                                                          
        """                                                                       
        # Publish a MQTT message to a topic for our thing to ingest               
        logger.info('publishing MQTT message to topic: {}'.format(self.topic))         
        self.iotClient.publish(                                                           
            topic=self.topic,                     
            qos=self.QoS,                                                               
            payload=json.dumps(payload)                                   
        )  
        return

    def update_shadow(self, payload):
        """
        Summary: Updates a things shadow for a specified thing
        Return The state information in a JSON format
        """
        updatedPayload = {'state': {'desired': payload}}
        response = self.iotClient.update_thing_shadow(
            thingName=self.thingName,
            payload=json.dumps(updatedPayload)
        )
        return response

    def get_shadow(self):
        """
        Summary: gets thing shadow for a specified thing
        Return: The state information in JSON format
        """
        response = self.iotClient.get_thing_shadow(
            thingName=self.thingName
        )
        response = json.loads(response['payload'].read().decode('utf-8'))['state']['desired']
        return response

    def delete_shadow(self):
        """
        Summary: deletes thing shadow for a specified thing
        Return: The state information in a JSON format
        """
        response = self.iotClient.delete_thing_shadow(
            thingName=self.thingName
        )
        return response

【讨论】:

  • 以上代码是python和aws iot的通信。我想要传感器 esp8266 和 aws iot
  • 嗨@shashanksureshkumar,要回答这个问题,我认为我们需要更多地了解您的环境。看起来 esp8266 经常与 arduino 一起使用。我认为这个存储库可能对你有用:github.com/heskew/aws-sdk-arduino 以及这个博客:iotdesignpro.com/projects/…
  • 您想了解哪些环境信息?
  • 我想知道您使用什么语言与 AWS IoT 进行通信?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-25
  • 1970-01-01
  • 2023-01-31
  • 2022-07-29
  • 1970-01-01
  • 2021-01-24
  • 2018-03-02
相关资源
最近更新 更多