Site icon ucdev

CoAP protocol

This article provides a general introduction to the Constrained Application Protocol (CoAP) for those interested in a lightweight, easy-to-implement data transfer solution for devices with limited hardware resources, also known as ‘constrained devices’. CoAP is a UDP-based protocol defined by the RFC 7252 specification. CoAP is very similar to HTTP, has a similar client-server model, is based on request-response messages and is based on the REST model.

The REST (Representational State Transfer) model, in a nutshell, is an architectural style that defines a certain pattern of information exchange between two devices, one acting as a server and the other as a client. The role of the server is to provide clients with access to different types of resources. Each client may need to access different types of resources, so in order to group and separate them, each is assigned a textual identifier called a URI (Uniform Resource Identifier).
Different types of operations can be performed on resources, e.g. read, add, create, delete, modify. The methods used are GET, POST, PUT, DELETE.

Message format

The CoAP message format, also known as PDU (Protocol Data Unit), has 4 main components: the header, the token, the options and the payload. The payload marker is an additional 1 byte value 0xFF that is only present if there is a payload in the message.

Message types

There are 4 types of messages in CoAP to support reliability and message acknowledgement.

Message types short summary:

Message TypeAbbreviatonReliabilityAck requiredSummary
ConfirmableCONYESYESCritical requests needing confirmation
Non-confirmableNONNONONon-critical data such as telemetry
AcknowledgmentACK—-—-Confirms receipt of a CON message
ResetRST—-—-Indicates inability to process a message

Message identifier

The message id is a 16 bit field with 65,536 possible values, which is sufficient to generate a unique value over a short period of time. The first message id should be randomized for each client to reduce the change of collisions in concurrent message exchanges. The next one can simply be incremented by one. The purpose of unique message identifiers in CoAP is to provide reliability, duplicate detection and to match requests with responses.

Message codes

They are used to determine what type of responses or requests are sent, for example, whether they indicate a successfully processed request or an error. They have a similar role to status codes in HTTP, e.g. 200 to indicate a correctly processed request or 500 to indicate an internal server error. The main difference is that these codes are slightly more optimized.

This code is transmitted on a single byte that is divided into two parts:

0.xxRequest codes (Methods)
2.xxSuccess responses
4.xxClient error responses
5.xxServer error responses

CoAP vs MQTT

These two widely used communication protocols in IoT seem to have similar application, but there is a significant difference in architecture that determines in which cases one is more suitable than the other.

CoAP uses a request/response method, while MQTT has a publish/subscribe method, but they are not the same. CoAP has a one-to-one direct architecture between client and server without a middleman like MQTT which uses a central broker to send messages between different devices (clients and servers). In CoAP, clients cannot communicate directly with other clients. They can „watch” endpoint sources with data from other devices. Similarly, in MQTT, a client can send data to another client, but it must be routed through a „broker”. CoAP uses UDP as the transport protocol, which is not as reliable as the TCP-based connection that MQTT uses. However, this results in high power consumption because the connection between the client and the broker must be maintained. MQTT supports multiple nodes connected at the same time. Unlike the TCP-based protocol, CoAP has a connectionless architecture, so devices can communicate between two endpoints without prior agreement. In summary, MQTT is ideal for event-based systems where you have multiple nodes interacting based on their sensor values, for example. CoAP is better suited for systems that monitor state changes, where you can pass state information between client and server.

CoAP Request Codes (Method Codes)

Code (Class.Detail)Method
0.01GET
0.02POST
0.03PUT
0.04DELETE

CoAP Response Codes

Success Responses

Code (Class.Detail)Meaning
2.01Created
2.02Deleted
2.03Valid
2.04Changed
2.05Content

Client Error Responses

Code (Class.Detail)Meaning
4.00Bad Request
4.01Unauthorized
4.03Forbidden
4.04Not Found
4.05Method Not Allowed

Server Error Responses

Code (Class.Detail)Meaning
5.00Internal Server Error
5.01Not implemented
5.02Bad Gateway
5.03Service Unavailable
5.04Gateway Timeout
5.05Proxying Not Supported

As you can see, most of these error codes are very similar to those used in HTTP. However, the format is slightly different.

In summary, the COAP protocol is a lightweight application layer protocol designed for use in constrained environments such as IoT devices. It enables efficient communication over low-power networks by following a request-response model similar to HTTP, but optimized for resource-constrained devices. It is designed for devices with limited processing power and memory. Supports operations such as GET, POST, PUT, and DELETE. Uses UDP instead of TCP to reduce communication overhead. Supports multicast, caching, and asynchronous communication. Typical applications include: smart home systems, industrial IoT applications, remote monitoring and control of sensors and actuators.

Exit mobile version