gpt4 book ai didi

arduino - 对于墙壁供电的物联网设备,读取数据和发送命令的最佳方式是什么

转载 作者:行者123 更新时间:2023-12-02 03:18:07 24 4
gpt4 key购买 nike

我有一个连接互联网的物联网设备(比如说先进的连接互联网的天气传感器,它可以发送传感器数据并执行一些高级操作,如旋转、打开、打开阀门/以及其他一些操作)。

编辑:

该设备为墙壁供电(电源 socket ),并具有移动互联网连接标准有线以太网连接(只需插入 Cat5 电缆)

我需要能够获得设备的状态(大约 500 字节的数据),并且希望能够 发送简单的命令,如 :

  • rotate-180-deg
  • turn-lights-on
  • turn-lights-off
  • open-valve-1
  • switch-sensor-X-on
  • switch-sensor-X-off

  • 目前,我的设备每(2 分钟)向我的中央服务器发送一个 HTTP 请求及其状态。这非常适合获取设备传感器的读数。但是,当我想向设备发送命令时,这种方法变得更加成问题。例如。如果我想发送命令 rotate-180-degrees ,我的中央服务器必须等到设备联系它并在 HTTP 请求的响应中 - 我可以放置一些命令,所以当设备收到响应时,它会实际执行命令。

    但是这种方法有缺陷:
  • 这不是实时的(我必须等待 2-3 分钟才能有机会发送命令)
  • 我不知道设备是否接收到命令(例如,在网络错误的情况下)
  • 我不知道设备是否已经确认或执行了命令(状态都没有)

  • 这个问题的解决方案是什么?

    更新:
    正如@mhopeng 建议的那样,最灵活的解决方案似乎是将设备变成“服务器”,以便它可以接受传入的连接。但是,出于安全性、防火墙和复杂性的考虑,我们不能走这条路。设备还需要易于安装:第三方维护人员应该能够简单地将设备插入墙壁和以太网,并且它应该可以工作。 (无需配置端口转发、防火墙等)。

    仅供引用,我们还在此设备中使用 PIC 微 Controller 。

    最佳答案

    This article详细介绍了将数据从服务器发送到设备的三个选项。

    根据您的描述,听起来您使用的是 短轮询 :

    The most basic way to solve this communication problem is called short polling—a method where the client periodically asks the server if there is new data available for it. This is the simplest solution to code, though it is not recommended if you need to notify a device in real time.



    然后文章继续描述选项 2, 长轮询 :

    The next option is long polling. In this case, the client performs the request and the server won’t respond until it has something to send. This enables real-time push notifications from the cloud to devices, though it requires the device to leave the connection open for as long as it needs to listen to the server. Using this technique consumes more energy and also risks the loss of the connection. Consider the case where a device remotely controls the door of a truck. If a long poll request has been made, and then the truck goes into a tunnel, the mobile connection will drop. The device will then need additional logic to kill the hung connection and open a new one.



    最后,它详细介绍了选项 3, 使用不同的协议(protocol) :

    A third option is to use newer protocols like CoAp or MQTT, for example, which were designed to provide low latency, small packet sizes and stable communication over weak networks. These newer protocols provide a two-way communication channel, which in turn supports push notifications. This makes them good choices for IoT projects requiring the ability to control connected devices in real time. The only downside could be the lack of firmware libraries and examples for embedded devices, which are significantly more abundant for HTTP-based connections. Choosing the right protocol will depend on your application and how often you will need to communicate with a device.

    关于arduino - 对于墙壁供电的物联网设备,读取数据和发送命令的最佳方式是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35347042/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com