CoAP · Study deck

CoAP Messages: Codes, Tokens, and Options

A CoAP request can share a message ID yet belong to a different exchange.

Broker Bex is your guide for this deck.

messageformat
Broker Bex, the module guide, in a scene from this chapter.
iotclass.org

After studying this chapter

Learning objectives

You will be able to:

  • Explain: Key Takeaway: The 4-byte header efficiency is CoAP's main advantage over HTTP - it encodes everything needed for request-response matching, reliability selection, and method identification in just 32 bits.
  • Explain: Just like how a bottle label is tiny compared to the letter inside, CoAP's header is tiny (4 bytes!) compared to HTTP's headers (hundreds of bytes!).
  • Explain: On constrained links, 60 application/cbor carries structured data in a compact binary representation; 40 application/link-format has the separate job of encoding discovery links.
  • Map CoAP method and response codes to their wire values.
iotclass.org

Major section

Minimum Viable Understanding: CoAP Message Structure

The fixed header contains version, message type, token length, method/response code, and message ID.

  • Key Takeaway: The 4-byte header efficiency is CoAP's main advantage over HTTP - it encodes everything needed for request-response matching, reliability selection, and method identification in just 32 bits.
iotclass.org

Major section

The 4-Byte Fixed Header

Choosing between CON and NON is one of the most important decisions in a CoAP implementation.

  • The wrong choice either wastes battery on unnecessary retransmissions or silently loses critical commands.
  • Rule of thumb: If losing the message would require human intervention to notice or correct, use CON.

Key terms

If one
If one is lost, the next one arrives in minutes.
iotclass.org

Major section

Message in a Bottle

The CoAP header is like the label you put on the bottle.".

  • Code: What you want - "Can I have the temperature?" (GET).
  • Just like how a bottle label is tiny compared to the letter inside, CoAP's header is tiny (4 bytes!) compared to HTTP's headers (hundreds of bytes!).
iotclass.org

Major section

Token Security Consideration

In secure deployments (DTLS), tokens should be unpredictable (randomly generated) to prevent response spoofing attacks.

  • A malicious actor who can predict tokens could inject fake responses.
iotclass.org

Major section

CoAP Options

Extended delta: 0, 1, or 2 extra bytes when the delta does not fit in 4 bits.

  • Option 1: Uri-Path = "sensors" uses delta 11, length 7, and encodes as 0xB7 | "sensors".
  • Option 2: Uri-Path = "temp" uses delta 0, length 4, and encodes as 0x04 | "temp".
iotclass.org

Major section

CoAP Options (continued)

The Content-Format option tells the receiver how to decode the payload, so select it as part of the resource contract rather than after serialization.

  • 0 text/plain fits a human-readable scalar, while 50 application/json supports familiar structured objects.
  • On constrained links, 60 application/cbor carries structured data in a compact binary representation; 40 application/link-format has the separate job of encoding discovery links.
  • A receiver must interpret the numeric code before parsing payload bytes.
iotclass.org

Major section

Worked Example: GET /sensors/temp with JSON Response

Real-world HTTP requests with cookies, user-agent, and other headers typically reach 300-800 bytes.

  • The ACK + response is piggybacked -- one message serves as both "I received your request" and "here is the data.".

Numbers to remember

300-800 bytesand other headers typically reach 300-800 bytes.
1.2 mseach byte costs approximately 1.2 ms of airtime.
iotclass.org

Major section

Putting Numbers to It

We can calculate the exact battery impact over the device lifetime.

  • Battery impact: At 0.5 mJ per byte (typical LoRa SF12), the CoAP exchange costs 32 mJ vs 160 mJ for HTTP per reading.
iotclass.org

Deck summary

Key takeaways

The fixed header contains version, message type, token length, method/response code, and message ID.

  • Choosing between CON and NON is one of the most important decisions in a CoAP implementation.
  • The CoAP header is like the label you put on the bottle.".
  • In secure deployments (DTLS), tokens should be unpredictable (randomly generated) to prevent response spoofing attacks.
  • Extended delta: 0, 1, or 2 extra bytes when the delta does not fit in 4 bits.
iotclass.org

Retrieval practice

Recall check 1 of 3

Broker Bex says: answer from memory, then check your reasoning.

Q1A CoAP client sends PUT /actuator/valve {"position": 50} to set a valve to 50% open. The server successfully updates the valve. What response code should the server return?

A2.01 Created - a new resource was created
B2.04 Changed
C2.05 Content - returning the current state
D2.00 OK - general success
Show answer

Answer: B Correct!

iotclass.org

Retrieval practice

Recall check 2 of 3

Broker Bex says: answer from memory, then check your reasoning.

Q2A CoAP client has two in-flight requests: GET /temperature (Token=0xAA, MsgID=0x0001) and GET /humidity (Token=0xBB, MsgID=0x0002). The CON for temperature times out and is retransmitted. What values should the retransmission carry?

AToken=0xAA, MsgID=0x0001 (same token, same message ID)
BToken=0xAA, MsgID=0x0003 (same token, new message ID)
CToken=0xCC, MsgID=0x0001 (new token, same message ID)
DToken=0xCC, MsgID=0x0003 (new token, new message ID)
Show answer

Answer: A Correct!

Q3Complete the CoAP message construction:

Amsg = Message(mtype=CON, code=GET)
Bmsg = Message(type='confirmable', method='GET')
Cmsg = CoAPMessage(reliable=True, code=GET)
Dmsg = Request(confirmable=True, method=GET)
Show answer

Answer: A CoAP messages use mtype=CON for confirmable (reliable) delivery.

iotclass.org

Retrieval practice

Recall check 3 of 3

Broker Bex says: answer from memory, then check your reasoning.

Q4You capture a CoAP packet with first byte 0x50. What can you determine about this message?

AVersion 1, NON message, Token length 0
BVersion 1, CON message, Token length 4
CVersion 2, ACK message, Token length 0
DInvalid CoAP message
Show answer

Answer: A Correct!

iotclass.org

Print reference

Answers

Answer key.

  1. B · Correct!
  2. A · Correct!
  3. A · CoAP messages use mtype=CON for confirmable (reliable) delivery.
  4. A · Correct!
iotclass.org