Basics of CoAP

Basics of CoAP

CoAP (draft-18) for Beginners

The best way to understand CoAP is to read the specification. The specification document is surprisingly not big enough. However, if you are like me, lazy, then here is a short introductory course on CoAP to get you started.

  • WHAT IS COAP
    CoAP stands for “Constrained Application Protocol”. In coming years, pundits predict that there will be more machines, connected to each other, sharing data, than humans. This gave rise to the concept of “Internet of Things” or “Machine-2-Machine”. While we take connectivity for granted, imagine how can really small, tiny devices connect to the internet. By small, I mean really small, something possibly smaller than your key ring. In the PC world, the message exchange is over TCP and application protocol is HTTP. However, for small devices, implementing the TCP stack and then HTTP protocol is really too much to ask for. That is where CoAP was devised. It’s a tiny protocol at the application layer (an alternative to HTTP) that is envisaged to fit in tiny form factors. It runs on UDP and not on TCP. The smallest message size that CoAP mandates is 4-bytes!
  • SO IS COAP A REPLACEMENT FOR HTTP
    CoAP is not a replacement for HTTP. It’s an alternative suggestion for tiny devices that have severe resource constraints (e.g. 256 KB memory, 32 KB RAM, 20 MHz speed).
  • WHAT ARE THE DIFFERENT TYPES OF COAP MESSAGE
    CoAP messages follow the same request/response paradigm (largely) that is prevalent in HTTP world. There are 4 types of CoAP messages

    1. CON – This is called “Confirmable” request. When a CON request is sent, the recipient must respond.
    2. NON – This is called “Non-Confirmable” request. When a NON request is sent, the recipient is not required to respond back.
    3. ACK – This is called “Acknowledgement”. This is a response to a CON message. When processing succeeds, the recipient of a CON message should respond back with an ACK. The ACK message can also contain result of the processing alongwith.
    4. RST – This is called “Reset” message. When the recipient of a message encounters an error, does not understand the message or is no longer interested in the message sender, this response is sent back.
  • WHAT ARE THE DIFFERENT PARTS OF A COAP MESSAGE
    A CoAP message, at minimum, must be 4 bytes long. The different parts, that can make up a CoAP message are:

    1. Version – What is the CoAP version that this message follows (as of now its V 1)
    2. The message type (CON, NON, ACK, RST)
    3. The message code (Like PUT, GET, UNAUTHORIZED)
    4. The message id. Each CoAP message has an ID associated with it. The ID is generally unique for a conversation for some period of time, after which, it can be recycled.
    5. The token. A token is another value that may be attached to the message for matching. Messages can be sent out of order and at that time, token becomes important.
    6. A set of options. Options in CoAP are like HTTP request headers. They contain metadata about the CoAP message itself (e.g. the CoAP port, the CoAP host, the CoAP querystring)
    7. The payload. This is the actual message that is being exchanged.
  • WHAT IS THE SIGNIFICANCE OF URL IN COAP
    In HTTP world, RESTful services have become popular due to their simplicity and great applicability. The same concept has been extended to CoAP. A CoAP resource is identified by a URI. So for example, it a machine is measuring temperature, the URI could be http://machine.address:5683/sensors/temperature Please note, the default CoAP UDP port is 5683.
  • WHAT IS OBSERVE IN COAP
    Sometimes, you want to monitor a value, let’s say, temperature. In that case, the client does not need to continuously poll the server. The client can send one “Observable” request to the server. From that point onwards, the server will remember the client connection details, and on every change in temperature, will send the new value to the client.If client no longer wishes to receive the temperature values, it can respond back with an RST message and server will clear it’s memory. More on this in examples.
  • WHAT IS BLOCK TRANSFER IN COAP
    Given that CoAP messages should be key very small in size, there will be cases when you need to exchange large data streams, either from client to server (like a PUT) or from server to client (like a GET). In that case, the CoAP message can contain options associated with block transfer and then the client and server can exchange multiple messages with blocks of data in each. More on this later on, in tutrials.

This completes your “Beginners Guide to CoAP” session. Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>