对于墙上供电的物联网设备,读取数据和发送命令的最佳方式是什么
For wall powered IoT device what are the best ways to read data and send commands
我有一个连接互联网的物联网设备(比方说连接互联网的高级天气传感器,它可以发送传感器数据并执行一些高级操作,例如旋转、打开、打开 valves/and 其他一些操作)。
编辑:
该设备采用墙上供电(电源插座),并具有 移动互联网连接 标准有线以太网连接(只需插入 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 所建议的,最灵活的解决方案似乎是将设备变成 "server",以便它可以接受传入连接。但是,出于安全、防火墙和复杂性的考虑,我们不能走这条路。设备还需要安装简单:第三方维护人员应该能够简单地将设备插入墙上和以太网,并且它应该可以工作。 (无需配置端口转发、防火墙等)。
仅供参考,我们还在该设备中使用了 PIC 微控制器。
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,使用不同的协议:
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.
我有一个连接互联网的物联网设备(比方说连接互联网的高级天气传感器,它可以发送传感器数据并执行一些高级操作,例如旋转、打开、打开 valves/and 其他一些操作)。
编辑:
该设备采用墙上供电(电源插座),并具有 移动互联网连接 标准有线以太网连接(只需插入 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 所建议的,最灵活的解决方案似乎是将设备变成 "server",以便它可以接受传入连接。但是,出于安全、防火墙和复杂性的考虑,我们不能走这条路。设备还需要安装简单:第三方维护人员应该能够简单地将设备插入墙上和以太网,并且它应该可以工作。 (无需配置端口转发、防火墙等)。
仅供参考,我们还在该设备中使用了 PIC 微控制器。
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,使用不同的协议:
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.