CoAP · Study deck
CoAP Lab: Stack and Trace Contracts
A resource is the named item a client can read or change.
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
Start With the Decision
A resource is the named item a client can read or change.
- Observe is a way to subscribe to changes in that item.
- A trace records the messages in order.
- The lab goal is not to memorize every library call.
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
Python CoAP Server
In the local run here, an equivalent aiocoap resource bound to 127.0.0.1:56830 logged server GET token=. Payload=b''; the site registration supplies /temperature.
- The code below registers only /temperature; /config, /readings, and Observe need additional handlers before those clients can succeed.
- The /config PUT and /readings POST clients below need matching server resource handlers before they can succeed.
- To receive Observe updates, make the temperature handler an ObservableResource and call updated_state() after a value changes; the plain Resource above does not register an observation.
Major section
Interactive Tool: CoAP Response Code Lookup
Select different response codes to understand when each is used
- Notice the pattern: 2.xx = success, 4.xx = client error, 5.xx = server error
- Compare CoAP codes to their HTTP equivalents

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
Python CoAP Client
In a second Python terminal, point the GET client below at the listening server and run it once.
- If the server is absent, preserve the timeout or connection error instead of substituting the expected 2.05 line.
- A separate local GET /config returned 4.04 Not Found, confirming that the published /config example needs its own server handler.
- The local run sent PUT to /temperature and observed server PUT payload=b'23.5' followed by client PUT -> 2.04 Changed.
Major section
Python CoAP Client (continued)
The POST client below sends JSON to /readings; inspect its application/json content format and encoded payload before sending.
- Against the chapter's unextended server, the local aiocoap run returned client POST /readings -> 4.04 Not Found.
- The endpoint extension was not run here, so 2.01 Created remains the expected result after the learner implements the handler.
- The plain TemperatureResource above does not implement Observe; change it to an ObservableResource in your lab copy.
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
This sketch prints illustrative exchanges; it does not send UDP packets or implement a CoAP server.
- If Wokwi has capacity, paste the sketch below into its real ESP32 editor, start the simulation, and open Serial Monitor.
- Wokwi was not run in this lane because the CI quota is exhausted; the expected serial lines below are from the sketch, not observed output.
Major section
Arduino ESP32 CoAP Client
In the ESP32 serial monitor, record the GET, PUT, and POST send lines and the callback's response code and payload.
- No ESP32, Wi-Fi packet capture, or physical radio path was run here; the localhost aiocoap exchange is the only real CoAP network result in this lane.
Deck summary
Key takeaways
A resource is the named item a client can read or change.
- 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.
- In the local run here, an equivalent aiocoap resource bound to 127.0.0.1:56830 logged server GET token=. Payload=b''; the site registration supplies /temperature.
- ConfigResource supports GET for the current configuration and PUT for an idempotent update, returning Code.CHANGED only after decoded JSON has been applied.
Retrieval practice
Recall check 1 of 4

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 4

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: B The literal /temp exchange is 11B CoAP request + 11B response = 22B.
Retrieval practice
Recall check 3 of 4

Broker Bex says: answer from memory, then check your reasoning.
Q3A CoAP client can read temperature but its config write fails. What should the server developer check?
Show answer
Answer: C The write needs its own resource and method handler.
Retrieval practice
Recall check 4 of 4

Broker Bex says: answer from memory, then check your reasoning.
Q4An embedded client sketch is moved to an ESP board. Which review step shows that CoAP still works?
Show answer
Answer: D The embedded library and trace show the actual CoAP exchange.
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()).
- B · The literal /temp exchange is 11B CoAP request + 11B response = 22B.
- C · The write needs its own resource and method handler.
- D · The embedded library and trace show the actual CoAP exchange.