CoAP · Study deck

CoAP Messages: Codes, Tokens, and Options

Sensor temperature request → CoAP message → reply. Message ID tracks the message exchange; token links the reply to its request.

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

Decode the packet before interpreting the application result.

  • Read the fixed header, token, options, and payload in order.
  • Match Message ID to the exchange and token to the response.
  • Compare byte costs only under the stated model assumptions.
iotclass.org

Major section

Start With the Decision

A CoAP message exchange pairs a message with its acknowledgment or reset, if one is used.

  • A Message ID helps detect duplicates and match that acknowledgment; it does not match a response to a request.

A token links a reply to its request.

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: CoAP's 4-byte fixed header carries message type, code, token length, and Message ID.
  • The following token field, which can be zero bytes long, matches a response to its request; the header alone does not.
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.

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.".

This example traces a complete request-response pair for reading a temperature sensor, showing every byte in the CoAP message and comparing to the equivalent HTTP exchange.

iotclass.org

Major section

Putting Numbers to It

Compare modeled byte costs while keeping battery life a separate measurement.

  • The example uses 175,200 readings.
  • The stated byte-cost model gives about 5,606 joules versus 28,032 joules.
  • The larger modeled transmit term is five times the smaller.
  • Battery life also depends on receive, idle, retries, conversion loss, and usable cell energy.

Numbers to remember

10,000 JTotal capacity
iotclass.org

Deck summary

Key takeaways

A CoAP message exchange pairs a message with its acknowledgment or reset, if one is used.

  • 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.
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, which would fit a new URI rather than updating the valve.
B2.04 Changed, confirming the existing valve resource was updated.
C2.05 Content, which fits retrieval rather than the stated update.
D2.00 OK, an HTTP-style success code rather than this CoAP result.
Show answer

Answer: B 2.04 Changed is the proper response for successful PUT operations. The resource existed and was modified.

iotclass.org

Retrieval practice

Recall checks 2 and 3 of 4

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 and 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 On retransmission of a CON, both the Token AND the Message ID stay the same.

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 4 of 4

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 type, and a zero-length Token from header 0x50.
BVersion 1, CON type, and a four-byte Token from header 0x50.
CVersion 2, ACK type, and a zero-length Token from header 0x50.
DAn invalid packet, because 0x50 cannot begin any CoAP message.
Show answer

Answer: A 0x50 = 0101 0000.

iotclass.org

Print reference

Answers

Answer key.

  1. B · 2.04 Changed is the proper response for successful PUT operations. The resource existed and was modified.
  2. A · On retransmission of a CON, both the Token AND the Message ID stay the same.
  3. A · CoAP messages use mtype=CON for confirmable (reliable) delivery.
  4. A · 0x50 = 0101 0000.
iotclass.org
Q1: B — 2.04 Changed. Q2: A — keep the same token and Message ID on retransmission. Q3: A — Message with message type confirmable and code get in the shown Python library. Q4: A — hexadecimal 5 0 means version 1, non-confirmable, token length 0. Reveal each answer after five seconds. The complete print key retains the same four answers.