With the development and popularization of the Internet of Things technology, more and more smart devices have network connection and data transmission capabilities.
Most of the devices in IoT scenarios are resource-constrained, such as CPU, RAM, Flash, and network bandwidth. Especially battery-powered devices are very sensitive to the power consumption and bandwidth of transmission protocols. Directly using TCP and HTTP protocols to realize data exchange between devices and platforms cannot meet the requirements of devices for low power consumption.
In order to allow these devices to access the network smoothly, the CoAP protocol came into being. As a fully managed MQTT messaging service dedicated to providing data connection capabilities for various IoT scenarios, EMQX Cloud also supports CoAP protocol device access, allowing a defined CoAP message format to publish, subscribe, and receive to EMQX Cloud messages, etc.
This article will introduce how to use EMQX Cloud to implement CoAP protocol device access.
Introduction to CoAP Protocol
Due to the complexity and variety of IoT scenarios, various factors such as device-side hardware conditions, network stability, traffic restrictions, device power consumption, and the number of device connections cause the message delivery of IoT devices to be very different from traditional Internet scenarios. Various IoT communication protocols.
As an HTTP-like protocol in the IoT world, CoAP protocol gateway is used on resource-constrained IoT devices, and its detailed specification is defined in RFC 7252.
Protocol Features
CoAP refers to many HTTP design ideas, and also improves many design details and adds many practical functions according to the specific conditions of constrained resource-limited devices. Such as:
- message-based model
- The transport layer is based on the UDP protocol and supports restricted devices
- Uses a request/response model like HTTP requests, HTTP is text format, CoAP is binary format, and is more compact than HTTP
- Support two-way communication
- Lightweight and low power consumption
- Support reliable transmission, data retransmission, and block transmission to ensure that data arrives reliably
- Support IP multicast
- Support observation mode
- Support asynchronous communication
market conditions
Compared with MQTT, CoAP is lighter and has lower overhead, and is more suitable in some specific devices and network environments. EMQX Cloud and some public cloud IoT platforms provide CoAP access capabilities.
CoAP protocol access to EMQX Cloud
Create a deployment
Create a new deployment. On the EMQX Cloud deployment page, obtain the public network connection address: 120.77.xx.
Activate CoAP Access Gateway
The CoAP access gateway is currently in the internal testing stage, and you can submit a work order to enable the access capability. After activation, the CoAP access gateway address is your deployment connection address, namely 120.77.xx, and the port is udp 5683.
Connect to deployment, publish-subscribe messages
libcoap is a very easy-to-use CoAP client library, here we use it as a CoAP client to test the functionality of the EMQX Cloud CoAP access gateway.
For installation and deployment, refer to the following example.
git clone http://github.com/obgm/libcoap
cd libcoap
./autogen.sh
./configure --enable-documentation=no --enable-tests=no
make
1. Publish an example
We use libcoap to publish a message to the EMQX Cloud deployment:
- Topic name is: "topic1"
- Client ID is: "client1"
- Username: "emqx"
- The password is: "public"
- Payload is: "hello,EMQX Cloud"
# CoAP 终端发送消息 "hello EMQX Cloud",topic 为 topic1
./examples/coap-client -m put -e "hello,EMQX Cloud" "coap://120.77.x.x:5683/mqtt/topic1?c=client1&u=emqx&p=public"
Next, we use MQTT X to subscribe to the corresponding topic topic1, and we can see that the message has been successfully published.
2. Subscription example
We subscribe to a topic using libcoap:
- Topic name is: "topic1"
- Client ID is: "client1"
- Username: "emqx"
- The password is: "public"
- Payload is: "hello,EMQX Cloud"
Next, we use MQTT X to send "hello,EMQX Cloud" to the topic1
topic.
# CoAP 终端订阅 topic1 主题,-s 20表示订阅维持20秒
./examples/coap-client -m get -s 20 "coap://120.77.x.x:5683/mqtt/topic1?c=client1&u=emqx&p=public"
During this period, if a message is generated on the topic topic1, libcoap will receive the message.
summary
So far, we have completed the entire process of using the CoAP protocol gateway to access EMQX Cloud.
At present, IoT protocols are developing in a diversified manner. Different industries and scenarios apply different protocols. In the same scenario, there can be multiple protocols to choose from. No protocol can dominate the market. There are various protocols. certain complementary effects. Therefore, to realize the interconnection of IoT devices and data, the key point is not the unification of protocols, but the interconnection between different protocols and the unification of upper-layer business application layer protocols. The CoAP protocol gateway provides new possibilities for solving the data connection problem of IoT devices. EMQX Cloud supports multi-protocol access, and connects hundreds of millions of IoT devices to EMQX Cloud reliably through the open standard IoT protocols MQTT, MQTT over WebSocket, CoAP/LwM2M, allowing IoT data to play a greater role. value.
Copyright statement: This article is original by EMQ, please indicate the source when reprinting.
Original link: https://www.emqx.com/zh/blog/connecting-coap-devices-to-emqx-cloud
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。