CoAP · Study deck
CoAP Lab: Stack and Trace Contracts
The lab goal is not to memorize every library call.
Broker Bex is your guide for this deck.

After studying this chapter
Learning objectives
You will be able to:
- Construct CoAP Servers: Implement Python CoAP servers with async resource handlers using aiocoap, registering GET, PUT, and POST endpoints
- Develop CoAP Clients: Create Python clients that send GET, PUT, POST, and Observe requests and process response payloads
- Configure Embedded CoAP: Adapt and deploy CoAP client code on Arduino ESP32 using the coap-simple library
- Distinguish Response Codes: Interpret CoAP Class.Detail response codes (2.xx, 4.xx, 5.xx) and select the correct code for each operation
Major section
In 60 Seconds
A successful response code can still arrive before the output moves, after a duplicate, or with content the application did not accept.
- An actuator is a device that turns an electrical command into physical action.
- CoAP means the compact request-and-response method used here.
- A payload is the useful content carried inside a message.
Major section
Key Concepts
Token: Client-generated value matching responses to requests — enables concurrent request/response pairing.
- Observe Option: CoAP extension enabling publish/subscribe: client registers to receive notifications on resource changes.
Major section
Interactive Tool: CoAP Response Code Lookup
ConfigResource supports GET for the current configuration and PUT for an idempotent update, returning Code.CHANGED only after decoded JSON has been applied.
- As you inspect the code, keep routing, method semantics, Content-Format 50, validation, and response codes aligned; sharing a server process does not make the resources interchangeable.
Try it: Interactive Tool: CoAP Response Code Lookup in the chapter
Major section
Checkpoint: Server Resource Contract
You can tell which operation should return Code.CONTENT, Code.CHANGED, Code.CREATED, or Code.BAD_REQUEST.
- You have a resource tree that separates readable state, configurable state, and submitted readings before any client code runs.
Major section
Try It: Observe Pattern Timeline Simulator
Register observer, then push NON sensor updates and a CON alarm update and watch the Event Trace keep one Token fixed while the MID and Observe number advance.
- Trigger Out-of-order delivery and Max-Age expiry to see how a client rejects stale notifications, then Deregister and RST to close the subscription cleanly.
Major section
Try It: CoAP-Style REST Sensor on ESP32
Resource Discovery (/.well-known/core) lists all available resources with attributes -- this is how CoAP clients find endpoints without configuration.
- Observe Pattern sends server-push notifications every 2 seconds -- every 5th notification uses CON (Confirmable) requiring an ACK, the rest use NON (fire-and-forget).
Deck summary
Key takeaways
A successful response code can still arrive before the output moves, after a duplicate, or with content the application did not accept.
- Token: Client-generated value matching responses to requests — enables concurrent request/response pairing.
- ConfigResource supports GET for the current configuration and PUT for an idempotent update, returning Code.CHANGED only after decoded JSON has been applied.
- You can tell which operation should return Code.CONTENT, Code.CHANGED, Code.CREATED, or Code.BAD_REQUEST.
- Register observer, then push NON sensor updates and a CON alarm update and watch the Event Trace keep one Token fixed while the MID and Observe number advance.
Retrieval practice
Recall check 1 of 2

Broker Bex says: answer from memory, then check your reasoning.
Q1Complete the CoAP server resource implementation in Python:
Show answer
Answer: A CoAP resources in aiocoap extend resource.Resource. The render_get method handles GET requests. Responses use Message objects with bytes payloads (hence .encode()).
Retrieval practice
Recall check 2 of 2

Broker Bex says: answer from memory, then check your reasoning.
Q2Per this chapter's 'Putting Numbers to It' worked example, what are the total CoAP and HTTP message sizes for a single temperature-reading request/response exchange?
Show answer
Answer: A The chapter's worked byte counts: CoAP request=13B, response=11B, total=24B; HTTP request=~45B, response=~52B, total=97B (about 4x larger) -- independently recomputed from the chapter's own header/token/payload byte breakdown and confirmed to match.
Print reference
Answers
Answer key.
- A · CoAP resources in aiocoap extend resource.Resource. The render_get method handles GET requests. Responses use Message objects with bytes payloads (hence .encode()).
- A · The chapter's worked byte counts: CoAP request=13B, response=11B, total=24B; HTTP request=~45B, response=~52B, total=97B (about 4x larger) -- independently recomputed from the chapter's own header/token/payload byte breakdown and confirmed to match.