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.

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

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
iotclass.org

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.
iotclass.org

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.
iotclass.org

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.
iotclass.org

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.
iotclass.org

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
Select CoAP Response Code
iotclass.org

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.

- You now know how an aiocoap resource class turns a URI path into a handler method and a payload.

iotclass.org

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.

Why it matters

Observe was not run in this lane because the shown server lacks the required handler; no notification trace is claimed.

iotclass.org

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.
iotclass.org

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.

Visualize the message exchange sequence between a CoAP client and server during an Observe subscription. Adjust parameters to see how CON/NON message types and notification intervals affect the communication pattern.

iotclass.org

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.

Why it matters

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.

iotclass.org

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.
iotclass.org

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.
iotclass.org

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:

Aclass TemperatureResource(resource.Resource):
Bclass TemperatureResource(coap.Endpoint):
Cclass TemperatureResource(resource.Observable):
Dclass TemperatureResource(aiocoap.Handler):
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()).

iotclass.org

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?

ACoAP 11 and HTTP 36 bytes, counting request bytes but no responses.
BCoAP 22 bytes (11+11); HTTP 78 bytes (36+42), about 3.5 times as much.
CCoAP and HTTP about equal total bytes after UDP framing is included.
DCoAP 24 and HTTP 240 bytes, making HTTP about ten times larger.
Show answer

Answer: B The literal /temp exchange is 11B CoAP request + 11B response = 22B.

iotclass.org

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?

AThe client adds an Observe option to the write
BThe server sends every reply as a Reset
CA config resource with a PUT handler is registered
DThe read handler returns a longer payload
Show answer

Answer: C The write needs its own resource and method handler.

iotclass.org

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?

ARun the host binary on ESP because its embedded CoAP library is compatible with the host build.
BCheck Wi-Fi join alone without a CoAP request or response trace.
CSend HTTP and call its reply proof of the board's CoAP path.
DRun the ESP CoAP client and inspect a request-and-response trace.
Show answer

Answer: D The embedded library and trace show the actual CoAP exchange.

iotclass.org

Print reference

Answers

Answer key.

  1. A · CoAP resources in aiocoap extend resource.Resource. The render_get method handles GET requests. Responses use Message objects with bytes payloads (hence .encode()).
  2. B · The literal /temp exchange is 11B CoAP request + 11B response = 22B.
  3. C · The write needs its own resource and method handler.
  4. D · The embedded library and trace show the actual CoAP exchange.
iotclass.org