Labs Guide
CoAP, short for Constrained Application Protocol, is a communication protocol based on UDP designed to meet the M2M (Machine-to-Machine) needs in constrained environments. Currently, in the field of IoT protocols, the Matter Protocol 1.0 launched by Google, Amazon, and Apple in collaboration with the CSA Alliance, as well as the OLA Alliance Interoperability Protocol 1.0 initiated by 24 academicians from the Chinese Academy of Sciences and Engineering, along with Alibaba, Huawei, the China Academy of Information and Communications Technology, and China Mobile, all support CoAP protocol access. Due to its suitability for low-power IoT scenarios, CoAP has been widely applied in communication, IoT, and artificial intelligence fields. This article will detail the characteristics, working principles, and other related knowledge of the CoAP protocol.
Author: Zhang Hui
Organization: China Mobile Smart Home Operation Center


CoAP is a lightweight RESTful architecture protocol designed by the IETF (Internet Engineering Task Force) for constrained resource environments. Currently, many IoT platforms from companies such as Alibaba, Xiaomi, and Huawei support CoAP protocol access. Due to its suitability for low-power scenarios, CoAP has found widespread application in communication, IoT, and artificial intelligence fields.
– Supports Resource Constraints
It supports low-power, low-bandwidth, and low-computation devices to connect to the internet for communication.
– Reliable Transmission
It supports reliable transmission, data retransmission, and block transfer to ensure data transmission quality.
– Lightweight and Efficient
The lightweight header has low parsing complexity, and the protocol packet is lightweight, with a minimum length of only 4 bytes.
– Multicast Support
It supports IP multicast, allowing simultaneous requests to multiple devices.
– Protocol Stack of CoAP
CoAP is developed based on the UDP protocol and mainly operates at the application layer of the IoT network protocol stack. On one hand, CoAP communicates through the transport layer UDP protocol and supports network layer protocols such as IPv4 and IPv6; on the other hand, CoAP can support LwM2M (Lightweight M2M) protocol communication.
As shown in the figure above, CoAP is an application layer protocol developed based on the transport layer UDP protocol. In contrast, protocols like MQTT and HTTP are developed based on TCP. For small IoT devices, implementing TCP and HTTP protocols is clearly an excessive and impractical requirement, while CoAP, being a lightweight protocol, can better adapt to resource-constrained devices and scenarios.
– Logical Model of CoAP Protocol
The CoAP protocol model is logically divided into two layers: the resource request/response layer and the message layer. The resource request/response layer is responsible for transmitting resource operation requests and responses, while the message layer controls the end-to-end message interaction. The logical model and message types of the CoAP protocol are shown in the following figure.
CoAP is based on the REST architecture, where the server’s resource address is similar to a URL on the internet. Clients can access the server using POST, GET, PUT, and DELETE methods, simplifying the code and packaging compared to HTTP, thus reducing the complexity of HTTP connections. It is important to note that CoAP supports four types of messages: Confirmable Message (CON), Non-Confirmable Message (NON), Acknowledgement Message (ACK), and Reset Message (RST).
– Working Modes of CoAP Protocol
The working modes of the CoAP protocol mainly involve the request-response mechanism between CoAP clients and servers. Due to the complexity of IoT connections and communication scenarios involving numerous types of smart hardware, CoAP supports the following three working modes to adapt to low-rate, low-power IoT scenarios and devices:
1) Carry Mode
In carry mode, using the GET method as an example, the CoAP client first sends a CON type message request to the CoAP server. Upon receiving it, the CoAP server replies with an ACK type message, where the payload of the ACK message contains the text response payload. The entire process requires at least two messages, as shown in the following figure:
2) Separate Mode
In separate mode, using the GET method as an example, the CoAP client first sends a CON type message request to the CoAP server. Upon receiving it, the CoAP server immediately replies with an ACK type message, but this ACK message does not contain a payload. After a short wait, the CoAP server responds with a CON type message (note: the CON response message must have the same token as the CON message previously sent by the client), which includes the payload. The CoAP client then sends an ACK type message in response. The entire process requires at least four messages, as shown in the following figure:
3) Non-confirmation Mode
In non-confirmation mode, using the GET method as an example, the CoAP client first sends a NON type message request to the CoAP server. If the CoAP server receives a NON type CoAP request from the CoAP client, it may choose not to return a response, and the CoAP client will not trigger the retransmission mechanism due to not receiving an ACK type message from the CoAP server within a certain time. The process is illustrated in the following figure:
After understanding the theoretical concepts of the CoAP protocol, let’s set up a minimal CoAP protocol testing environment on Windows to conduct simple CoAP protocol consistency testing.
1) Setting Up CoAP Request-Response Testing Environment
We will use the open-source Copper plugin as the testing tool for this minimal CoAP protocol testing environment. First, after downloading the Copper plugin, open the Chrome browser and enter chrome://extensions in the address bar to enable developer mode. Then, click the “Load unpacked extension” button in the upper right corner of the Chrome browser, select the app and extension folders in the Copper folder to add them, and finally, replace the appId in the Copper4Cr\extension\endpoint\ClientPortChrome.js file with the ID from Copper (Cu4Cr) Application 0.1.0 to complete the setup of the CoAP request-response testing environment.
2) Setting Up CoAP Packet Sniffing Testing Environment
We will use the free tool Wireshark for packet sniffing in this CoAP test. After completing the installation on Windows, select WLAN or Ethernet based on your computer’s network connection method to capture CoAP packets.
3) Capturing CoAP Broadcast Packets
After installing Wireshark and selecting the corresponding network connection method, click to start capturing. You can enter “coap” in the filter to filter the packets. From the captured CoAP broadcast packets, you can filter the necessary verification information. For example, the broadcast packet shows the protocol type as CoAP and the message type as NON, and you can also parse some content of the broadcast packet. This can be combined with IoT protocols for protocol consistency testing. Additionally, Wireshark supports detailed filtering methods such as coap.length, coap.mid, and coap.code for CoAP broadcast packets.
4) Validating CoAP Request Sending and Response Messages
After installing the Copper plugin, ensure the testing computer and the device to be tested are on the same local network. Enter the IP address and port number 5683 of the device to be tested. As shown in the following figure:
Click “Discover” to view the CoAP interfaces supported by the device to be tested. For example, using the control interface, you can enter the CoAP request message in the Outgoing section and click POST to send it. Then, from the Incoming section, you can view the CoAP response. Therefore, by comparing the CoAP requests in the Outgoing section and the CoAP responses in the Incoming section, you can verify protocol consistency with the corresponding IoT protocols. As shown in the following figures:
In summary, the CoAP protocol possesses characteristics such as being lightweight and efficient, and it supports resource constraints. Compared to widely used protocols like HTTP in the internet field, CoAP is more compatible with communication, IoT, and artificial intelligence fields.
● China Mobile Signs Strategic Cooperation Framework Agreement with China Academy of Information and Communications Technology
● Download | China Mobile Releases “Exploration of 5G Private Network International Development Experience”
● Highlights | 5G Enters the 10,000 Mbps Era; Two International Standards for Satellite-Ground Integration Completed by ITU