element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Tech Connection
  • Learn
  • Learning Center
  • Tech Connection
  • More
  • Cancel
Tech Connection
Documents What is the CoAP Communication Protocol?
  • Blog
  • Forum
  • Documents
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Tech Connection to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Engagement
  • Author Author: rscasny
  • Date Created: 18 Aug 2018 4:59 PM Date Created
  • Last Updated Last Updated: 11 Oct 2021 4:36 PM
  • Views 4912 views
  • Likes 15 likes
  • Comments 8 comments
Related
Recommended

What is the CoAP Communication Protocol?

Introductionimage

IoT technology has triggered an exponential growth of connected devices. Businesses that operate huge networks of connected devices will require increased resources in terms of connectivity and power. As a result, it becomes extremely important for the devices to be ultra-low powered while using limited processing and network resources.

 

In general, connected devices are meant to send aggregated data to the cloud or local servers by using communication protocols for further analysis. There are many protocols for communication between end nodes and servers. The most popular and widely used is the HTTP, a protocol which has to transmit a large amount of payload data for very small size of information data. As such, this means more processing power resulting in higher power consumption.

 

However, since IOT nodes are very low powered, they have to work on 8-bit microcontrollers with limited CPU performance. The communication network might be LoWPAN having a very low throughput and high packet error rate. Such requirements have driven the emergence of lightweight communication protocols using fewer network resources like CoAP (Constrained Application Protocol) ideally suited for M2M and Home Automation applications.

 

Main Features of CoAP

  • REST Model for small devices: This enables CoAP servers to serve resources under a URL using methods like GET, POST, PUT and DELETE.
  • Flexible Integration: Data can be transferred from HTTP applications to CoAP applications and vice-versa using cross-protocol proxies.
  • Multiple Data Models: Can carry multiple types of data payloads. Data format can be of type XML, JSON, CBOR or any other.
  • Low Footprint: CoAP is designed to work with microcontrollers which have 10KB of RAM and 100KB of code space.
  • Low Overhead: CoAP is designed to work with UDP over IP network and meant to use minimal network resources.
  • Integrated Discovery: The CoAP resource directory provides a way to discover the properties of the nodes in the network.
  • Observe: The client node can make a GET request to the server with a observe flag so that server can reply to nodes on state changes avoiding periodic requests by the nodes.

 

CoAP Communication Model

CoAP is a Service Layer protocol influenced by HTTP and uses the same Server-Client or Request-Response model along with REST architecture. Though it is a single layer protocol, it logically uses a two-layer approach using a Messaging code and Request-Response code, both forming part of the CoAP header.

 

The CoAP messaging model is based on the exchange of messages over UDP.  CoAP uses a 4Bytes fixed length binary header followed by compact binary options and a payload. To prevent duplication each message contains a unique Message-ID which is of 16bit length and thus allows up to 250 messages to be transferred in one second from one point to another with the default protocol parameters. Marking a message as Confirmable (CON) provides reliability as the sender expects an acknowledgment from the recipient within a set timeout. If ACK is not received, sender resends the message with an exponential backoff or decrease in the rate of retry. The recipient acknowledges with the message ID to the sender when it processes the message. If it happens that the recipient is unable to process the message and also unable to provide a suitable error code it replies with an RST message instead of an ACK. Messages not needing acknowledgement can be sent as a Non-Confirmable message (NON) and are not acknowledged by the recipient. NON Messages also contains message ID to avoid duplication and the recipient might reply with an RST message if it is not able to process the message.

 

CoAP request and response semantics are carried in CoAP messages as options, which include method code or response code. A token is used to match responses to requests from the underlying messages. If the request is through confirmable message and the response to the request is available instantly, the response can be piggybacked into the acknowledgment. This avoids separate transmission of the response from the server and ACK from the client upon successful reception of the response. If the acknowledgement is lost the client will make the request again. If the response is not available immediately, the server responds with a blank acknowledgement to avoid retransmission of the request by the client. The response is sent by the Server in a confirmable message when the response gets ready and has to be acknowledged by the client. If the request is carried in a non-confirmable message, the server can send the response using confirmable or non-confirmable message.

 

CoAP also supports caching of the responses to make the response fast and is enabled using freshness and validity information available with the response.

 

Frame Format

image

CoAP messages are encoded in a simple binary format including a 4 Byte header, Token, Options, and Payload. For any type of message, the header is compulsory since it may contain Token or Options or Payload. Different fields in a CoAP message are:

 

CoAP Version: (2 Bit) is a part of the fixed header and defines the version of the CoAP used.

 

Message Type: (2 Bit) defines the type of the message.

 

0: Confirmable (CON)

1: Non-Confirmable (NON)

2: Acknowledgement (ACK)

3: Reset (RST)

 

Token Length: (4 Bit) indicating the length of the token variable which can be up to 8 bytes.

 

Request/Response Code: (1 Byte)   c.dd split into class c of 3 bits and details dd of 5 bits.

image

Table 1: CoAP Method and Response Codes

 

Unique Message ID (2 Bytes): is a 16-bit number in the network byte order. It is used to detect message duplication. Message ID is also used to match messages of type Acknowledgement/Reset to messages of type Confirmable/Non-confirmable.

 

Token value is used to correlate requests and responses. Every request carries a client-generated token that the server MUST echo without modification in any resulting response. A token is intended for use as a client-local identifier or a Request ID for differentiating between concurrent requests. The client should generate tokens in such a way that tokens currently in use for a given source/destination endpoint pair are unique.

 

Options follow Tokens. Both requests and responses may include a list of one or more options. Options are used to provide Information like URI and payload media type. CoAP defines a single set of options that are used in both requests and responses like Content-Format, ETag, Location-Path, Location-Query, Max-Age, Proxy-Uri, Proxy-Scheme, Uri-Host, Uri-Path, Uri-Port, Uri-Query, Accept, If-Match, If-None-Match, and Size1. Not all options are defined for use with all methods and response codes.

 

Payload can be a representation of resource or result of a requested action and is the primary content or data. The content format is specified by the internet media type, which can be plain-text, XML, Json etc.  Diagnostic payloads explain the client-server error in human readable format and do not have the content-format option. Packets containing information like ACK, RST do not need payloads.  A special byte 0xFF marks the end of the options and start of the payload.

 

Applications and Tools

There are numerous tools available to work with CoAP, ranging from Server Side applications to end nodes. Some of the applications and tools are listed below,

 

Server Side:

  • Californium, nCoAP, Leshan: JAVA Implementation for Servers.
  • LibCoAP: Implementation in C
  • CoAP.NET, CoAPSharp: C# Implementation for Servers.

 

End Nodes:

  • Erbium: Implementation for Contiki IOT OS
  • LibCoAP, SMCP, MicroCoAP: Implementation in C
  • MR-CoAP: JAVA Implementation of CoAP

 

Browser based:

  • Copper is available as an extension for Firefox web browser to test out CoAP.

 

Commercial Implementation:

  • ARM ‘mbed OS’ comes with CoAP implementation. Website thethings.io provides cloud services to carry out CoAP based projects; it supports devices to send data using CoAP.
  • communication protocol
  • rest
  • coap
  • network
  • http
  • iot
  • communication
  • Share
  • History
  • More
  • Cancel
  • Sign in to reply

Top Comments

  • oksbwn
    oksbwn over 6 years ago +3
    Nice One...
  • DAB
    DAB over 6 years ago +2
    Nice post. DAB
  • stevesmythe
    stevesmythe over 6 years ago

    Sadly, the "Copper" Firefox extension has been deprecated in the latest versions of Firefox.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Workshopshed
    Workshopshed over 6 years ago

    https?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Fred27
    Fred27 over 6 years ago

    Some useful information. There are so many competing protocols that it's good to have an overview of each so you can at least start making a decision.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • neilk
    neilk over 6 years ago

    Thanks for that, Randall. Very interesting.

     

    Neil

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Gough Lui
    Gough Lui over 6 years ago

    Running over UDP can help simplify things a bit, at the cost of the protocol needing to assure delivery, deduplicate any copies of the same packet and handle out of order reception.

     

    On the other hand, MQTT seems to have a following as well, TCP based so potentially heavier on the implementation.

     

    Old fashioned HTTP based REST still has its place but if only there was a way to keep things simple, performant, compatible and secure ... Each seems to still have its own strengths and weaknesses.

     

    - Gough

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • 14rhb
    14rhb over 6 years ago

    Thank you for the informative article - always good to know about new technologies and protocols even if you are not working on them at that time.

     

    Rod

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • oksbwn
    oksbwn over 6 years ago

    Nice One...

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • DAB
    DAB over 6 years ago

    Nice post.

    DAB

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube