This article describes the CoAP protocol through three aspects: the background of the protocol, application scenarios, and comparison with MQTT.
Completing the reading of the text provides an initial understanding of what the CoAP protocol is and what it addresses.
Background and significance of the CoAP protocol
Trying to figure out the CoAP protocol requires some understanding of MQTT, and MQTT at the application level of the Internet of Things is a topic that can't be gotten around.
And in terms of the current state of the Internet of Things, a lot of work is still being done on MQTT-related devices.
So why is there even a CoAP?
A: Because part of the environmental information for the MQTT is a bit of a waste of resources, so in order to solve these kinds of problems to solve the direction of the Internet of Things will appear some simpler and easier to operate the chip, specifically for the collection of the environment.
This saves resources, and also saves money on the hardware side of the device.
So CoAP came along!
In the application of IoT in agriculture similar sensor related devices want to send data to the cloud or based on the CoAP protocol to realize.
MQTT is an application layer protocol and its transport layer is TCP, which is a reliable connection and is classified as a long connection.
The CoAP protocol requires the device to remain powered if it is to maintain a long connection.
In order to solve this situation CoAP protocol transport layer is UDP, but UDP belongs to the unreliable transmission, so in the application layer is transformed, here simply understood as UDP unreliability is solved by the application layer.
The solid CoAP protocol is also a reliable connection protocol
MQTT vs. CoAP
MQTT: uplink data can be downlinked with a relatively large amount of data
MQTT: Relatively speaking, the battery capacity of the device is also larger than that of CoAP, and the purpose of adding more batteries to the internal part of some devices is to maintain a long connection, so that the device can sleep in time to reawaken the connection.
CoAP: Because the transport layer is using UDP, the device will send data directly after waking up and can sleep directly after sending. More power saving than MQTT protocol
CoAP can also be used when resources are particularly small
Main application scenarios of CoAP
Only data reporting without control issuance, e.g., water meters, electricity meters, smoke alarms, etc.
Official message format of the CoAP protocol (understand)
Ver => version number, you can determine what device it belongs to by the version number yourself
T => type
TKL => COAP Identifier Length.The COAP protocol has two functionally similar identifiers, a MessageID (message number) and a Token (identifier). Each of these contains the message number, but the identifier is not required for the message.
Message ID => Generally used to locate the problem.
Code => Operation Code/Function Code/Response Code.Code has different manifestations in COAP request message and response message.Code occupies one byte and it is divided into two parts, the first 3 bits partly and the last 5 bits partly.For the convenience of description it is written as structure. Which indicates a certain method of the COAP request, while, or indicates a specific manifestation of the CoAP response.
Token => Identifier specifics.
Option => Message Options, through which you can set the COAP host, COAP URI, COAP request parameters and load media type, etc.