52  CoAP Features and Labs

In 60 Seconds

This is the index for hands-on CoAP implementation, organized into three chapters: methods and communication patterns (GET/POST/PUT/DELETE, CON vs NON tradeoffs), implementation labs (Python aiocoap servers and Arduino ESP32 clients), and advanced features (Observe for push notifications, block-wise transfer for large payloads, and /.well-known/core resource discovery).

52.1 Learning Objectives

This section covers hands-on CoAP implementation, from basic methods to advanced features like Observe and Block Transfer. The content has been organized into three focused chapters:

  1. CoAP Methods and Patterns - Communication patterns, tradeoffs, and design decisions
  2. CoAP Implementation Labs - Python and Arduino code examples
  3. CoAP Advanced Features Lab - ESP32 Wokwi simulation with Observe, Block Transfer, and Discovery

By completing these chapters, you will be able to:

  • Implement CoAP Methods: Use GET, POST, PUT, DELETE for RESTful IoT communication
  • Configure Message Options: Set content formats, URIs, and observe tokens correctly
  • Build CoAP Servers: Develop embedded CoAP servers with resource handlers
  • Use Block Transfers: Implement blockwise transfer for large payloads on constrained devices
  • Apply Observe Pattern: Create publish-subscribe behavior using CoAP observe extension
  • Diagnose and Evaluate CoAP: Use Copper, CoAP.me, and Wireshark to analyze protocol behavior and troubleshoot communication failures
  • CoAP: Constrained Application Protocol — REST-style request/response protocol using UDP instead of TCP
  • Confirmable Message (CON): Requires ACK from recipient — provides reliable delivery over UDP at the cost of one roundtrip
  • Non-confirmable Message (NON): Fire-and-forget UDP datagram — lowest latency, no delivery guarantee
  • Observe Option: CoAP extension enabling publish/subscribe: client registers to receive notifications on resource changes
  • Block-wise Transfer: Fragmentation mechanism for transferring payloads larger than a single CoAP datagram
  • Token: Client-generated value matching responses to requests — enables concurrent request/response pairing
  • DTLS: Datagram TLS — CoAP’s security layer providing encryption and authentication over UDP

52.2 For Beginners: CoAP Features and Labs

This chapter combines learning about CoAP features with hands-on practice. You will explore how CoAP handles observations, block transfers, and caching while building working examples. It is like learning to cook by following a recipe – you understand the ingredients while making something real.

52.3 Prerequisites

Before diving into these chapters, you should be able to:

  • CoAP Fundamentals and Architecture: Explain CoAP’s core design, distinguish message types (CON, NON, ACK, RST), and compare how CoAP differs from HTTP — essential for implementing advanced features
  • IoT Protocols Fundamentals: Describe the IoT protocol stack, justify the use of UDP transport, and apply RESTful principles to constrained device communication
  • Networking Basics: Apply client-server architecture concepts, trace request-response patterns, and analyze basic networking to understand resource discovery and multicast operations

52.4 Chapter Overview

52.4.1 CoAP Methods and Communication Patterns

Focus: Design decisions and tradeoffs for CoAP communication

Topics Covered:

  • CoAP methods (GET, POST, PUT, DELETE) and their use cases
  • Confirmable (CON) vs Non-Confirmable (NON) message tradeoffs
  • Polling vs Observe pattern comparison with bandwidth calculations
  • Common misconception: “CoAP is just HTTP over UDP”
  • Worked example: Observe bandwidth savings calculation (97%+ reduction)

Best For: Architects and developers making protocol design decisions


52.4.2 CoAP Implementation Labs

Focus: Hands-on code examples for CoAP clients and servers

Topics Covered:

  • Python CoAP server with aiocoap (temperature resource, config PUT)
  • Python CoAP client (GET, PUT, POST, Observe patterns)
  • Arduino ESP32 CoAP client with coap-simple library
  • Response code reference (2.05 Content, 4.04 Not Found, etc.)
  • Basic Wokwi simulation for UDP communication

Best For: Developers implementing CoAP in Python or embedded systems


52.4.3 CoAP Advanced Features Lab

Focus: Production-grade ESP32 CoAP server with advanced features

Topics Covered:

  • Observe pattern with automatic notifications and sequence numbers
  • Block-wise transfer for firmware updates (4KB in 64-byte blocks)
  • Resource discovery via /.well-known/core (RFC 6690 Link Format)
  • Full Wokwi ESP32 simulation with DHT22 sensor
  • Challenge exercises: deregistration, retransmission, DTLS simulation
  • Python test client for Observe and Block Transfer

Best For: Advanced developers building production IoT deployments


52.6 Quick Reference: CoAP Features

Feature Description Chapter
GET/POST/PUT/DELETE RESTful methods for resource manipulation Methods and Patterns
CON vs NON Reliability vs efficiency tradeoff Methods and Patterns
Observe Push notifications when resources change Advanced Lab
Block Transfer Large payload handling in chunks Advanced Lab
Discovery /.well-known/core for service listing Advanced Lab
Python aiocoap Async CoAP library examples Implementation Labs
ESP32 coap-simple Embedded CoAP client Implementation Labs

Common Pitfalls

CON messages require an ACK roundtrip — on lossy networks with 20% packet loss, a 4-attempt retry with exponential backoff can delay responses by 45 seconds. Use NON for periodic telemetry where data freshness matters more than guaranteed delivery; reserve CON for actuation commands.

CoAP proxies cache GET responses based on Max-Age option — a sensor returning temperature with Max-Age=60 will serve cached values for 60 seconds even if the physical reading changes. Set Max-Age to match your data freshness requirement, not the default 60 seconds.

DTLS handshake (6-8 roundtrips) dominates latency for short-lived CoAP connections — repeatedly creating new DTLS sessions for each request adds 500-2000ms overhead. Use DTLS session resumption (RFC 5077) to reduce reconnection to 1 roundtrip after the initial handshake.

52.8 Concept Relationships

This lab series connects practical implementation to theoretical concepts:

Foundation Knowledge:

Lab Components:

Supporting Concepts:

52.9 See Also

Hands-On Resources:

Comparison Labs:

Deployment Guides:

52.10 What’s Next

Chapter Focus Why Read It
CoAP Methods and Patterns CON vs NON tradeoffs, Observe vs polling bandwidth calculations Start here to build the design intuition required before writing a single line of CoAP code
CoAP Implementation Labs Python aiocoap server and client, Arduino ESP32 coap-simple Implement working CoAP endpoints you can run and extend immediately
CoAP Advanced Features Lab Observe notifications, block-wise transfer, /.well-known/core discovery Construct a production-grade ESP32 CoAP server with all three advanced features in a Wokwi simulation
CoAP Fundamentals and Architecture Message format, response codes, UDP transport design Return here to deepen understanding of the binary header and option encoding that underlies every lab
CoAP Comprehensive Review Advanced topics, best practices, protocol comparison Assess your overall CoAP mastery with comprehensive exercises and design challenges
MQTT Implementation Labs Broker-based publish-subscribe, QoS levels, retained messages Compare CoAP’s REST model against MQTT’s event-driven model to select the right protocol for each IoT scenario