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.

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

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

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

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

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

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

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:

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 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?

ACoAP: 24 bytes total (13-byte request + 11-byte response); HTTP: 97 bytes total (~45-byte request + ~52-byte response) -- roughly 4x more
BCoAP: 13 bytes total; HTTP: 45 bytes total -- counting only the request, not the response
CCoAP and HTTP use nearly the same total size once UDP framing is included
DCoAP: 24 bytes; HTTP: 240 bytes -- roughly 10x more
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.

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