↑ Click the above“IoT Time and Space” to follow us
Reprint must indicate the source: “IoT Time and Space” WeChat official account (IoT-LPWAN)。
The main IoT application layer protocols include CoAP, MQTT, XMPP, HTTP, etc. The “IoT Time and Space” public account will analyze and compare these protocols separately. First, let’s take a look at the CoAP protocol.
1. What is CoAP?
CoAP is the Constrained Application Protocol, which is one of the Internet standards published by the IETF (RFC7252).
In the current world composed of PCs, information exchange is achieved through HTTP/TCP. However, for small devices, implementing TCP and HTTP protocols is clearly an excessive requirement. To allow small devices to connect to the Internet, the CoAP protocol was designed. CoAP is an application layer protocol designed for resource-constrained devices in the IoT, and its protocol model corresponds to HTTP as follows (Note: CoAP has supported TCP; this article will not elaborate on that):
2. Detailed Explanation of CoAP Protocol
Like other protocols in the TCP/IP protocol suite, the CoAP protocol has a message header, and the payload is separated from the header by a single byte 0xFF. The structure of CoAP protocol messages is as follows:
【Ver】 Version number, indicating the version of the CoAP protocol. Similar to HTTP 1.0 and HTTP 1.1. The version number occupies 2 bits, with a value of 01B.
【T】 Message type, the CoAP protocol defines four different types of messages: CON, NON, ACK, RST.
【CON】 A request that needs to be confirmed; if a CON request is sent, the other party must respond.
【NON】 A request that does not need to be confirmed; if a NON request is sent, the other party does not need to respond.
【ACK】 Response message, the response to a received CON message.
【RST】 Reset message; when the receiver receives a message that contains an error or does not care about the content sent by the sender, a reset message will be sent.
【TKL】 CoAP token length.
【Code】 Request code/response code. See detailed explanation below.
【Message ID】 Message number. Each CoAP message has a message ID, which remains unchanged during a session but is recycled after the session ends.
【Token】 Specific content of the identifier, with the length specified by TKL.
【Option】 Message options, including CoAP host, port number, resource path, resource parameters, etc. Similar to HTTP request headers.
【11111111】 Separator between CoAP message and specific payload.
【Payload】 The actual useful data being exchanged.
CoAP request methods include: GET, POST, PUT, DELETE, which are very similar to HTTP:
【0.01】GET: Obtain a certain resource
【0.02】POST: Create a certain resource
【0.03】PUT: Update a certain resource
【0.04】DELETE: Delete a certain resource
CoAP response codes are similar to HTTP 200 OK, etc.:
【2.01】Created
【2.02】Deleted
【2.03】Valid
【2.04】Changed
【2.05】Content. Similar to HTTP 200 OK
【4.00】Bad Request; request error, server cannot process. Similar to HTTP 400.
【4.01】Unauthorized; no scope permission. Similar to HTTP 401.
【4.02】Bad Option; request contains an erroneous option.
【4.03】Forbidden; server refuses the request. Similar to HTTP 403.
【4.04】Not Found; server cannot find the resource. Similar to HTTP 404.
【4.05】Method Not Allowed; illegal request method. Similar to HTTP 405.
【4.06】Not Acceptable; request options and server-generated content options are inconsistent. Similar to HTTP 406.
【4.12】Precondition Failed; insufficient request parameters. Similar to HTTP 412.
【4.15】Unsupported Content-Type; media type in request is not supported. Similar to HTTP 415.
【5.00】Internal Server Error; server internal error. Similar to HTTP 500.
【5.01】Not Implemented; server cannot support the request content. Similar to HTTP 501.
【5.02】Bad Gateway; server received an erroneous response while acting as a gateway. Similar to HTTP 502.
【5.03】Service Unavailable; server overloaded or maintenance downtime. Similar to HTTP 503.
【5.04】Gateway Timeout; server experienced a timeout error while executing the request as a gateway. Similar to HTTP 504.
【5.05】Proxying Not Supported; server does not support proxy functionality.
3. Features of CoAP Protocol
By interpreting the CoAP protocol, it is clear that it is specifically designed for IoT applications, including:
1. Based on REST architecture, which is the general design style for Internet resource access protocols.
2. Based on the lightweight UDP protocol and allows IP multicast.
3. Defines a transaction processing mechanism with a retransmission mechanism, allowing reliable transmission.
4. Provides a resource discovery mechanism with resource descriptions.
5. The protocol is compact, with the minimum data packet being only 4 bytes.
6. Can use DTLS as a security encryption layer.
7. Low resource consumption, requiring less than 10KB of RAM and ROM.
8. Its dual-layer (transaction layer, request/response layer) processing method supports asynchronous communication.
9. Supports observation mode. In IoT scenarios, CoAP clients sometimes do not need to continuously query the CoAP server for data changes (for example, monitoring a temperature or humidity sensor). In this case, the CoAP client can send an observation request to the server, from that point on, the server will remember the client’s connection information, and once the temperature changes, the server will send the new result to the client. If the client no longer wishes to receive temperature detection results, it will send a RST reset request, at which point the server will clear the connection information with the client.
10. Supports block transfer.CoAP protocol is characterized by the compactness of the content transmitted, but in some cases, larger data must be transmitted. In such cases, the options in the CoAP protocol can be used to set the size of block transfers, allowing the server and client to handle segmentation and assembly.
4. Application Example
For example, a CoAP client obtains temperature sensor data from the server using the GET method, assuming the CoAP URI is coap://www.server.com/temperature, the interaction process and message format are as follows:
Long press the QR code to follow the “IoT Time and Space” public account