41  CoAP Fundamentals and Architecture

Chapter Index: Constrained Application Protocol

In 60 Seconds

CoAP (Constrained Application Protocol) provides RESTful web semantics (GET/PUT/POST/DELETE) compressed into a 4-byte binary header running over UDP, enabling HTTP-like APIs on microcontrollers with as little as 2KB RAM. Unlike HTTP’s mandatory TCP connection overhead, CoAP offers optional reliability through confirmable messages, the Observe extension for push notifications, and block-wise transfer for large payloads – all designed for battery-powered constrained devices.

41.1 Learning Objectives

By the end of this chapter series, you will be able to:

  • Explain CoAP’s Design Philosophy: Articulate why CoAP exists and how it differs from HTTP in terms of transport, overhead, and use cases
  • Analyze Message Formats: Decode and construct CoAP messages at the byte level, distinguishing each header field’s role
  • Implement Reliability Patterns: Configure CON and NON messages appropriately for different deployment scenarios
  • Apply Advanced Features: Demonstrate use of block-wise transfer, Observe extension, and resource discovery in constrained environments
  • Design CoAP APIs: Construct RESTful APIs following best practices for constrained devices
  • Select and Justify Protocol Decisions: Evaluate CoAP vs MQTT vs HTTP based on measurable use case requirements and justify the choice
  • 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

41.2 For Beginners: CoAP Fundamentals

CoAP (Constrained Application Protocol) is a lightweight web protocol designed for tiny IoT devices with limited memory and battery. Think of it as a slimmed-down version of HTTP (the protocol your web browser uses). Where HTTP needs a powerful computer, CoAP can run on a sensor with just a few kilobytes of memory.

The Challenge: Web APIs for Constrained Devices

The Problem: You want sensors and actuators to expose web-like APIs (REST), but HTTP is too heavy:

  • HTTP headers: 200-800 bytes per request (your sensor reading is 10 bytes)
  • TCP overhead: 3-way handshake + TLS = seconds of setup time
  • Connection state: Each TCP connection needs ~1KB RAM (your device has 2KB total)
  • Text parsing: ASCII headers waste CPU cycles on 8-bit microcontrollers

What IoT Developers Need:

  • REST semantics: GET/PUT/POST/DELETE for familiar web patterns
  • Minimal overhead: Messages that fit in single UDP packets
  • Optional reliability: Acknowledgments when needed, fire-and-forget when not
  • Low power: No persistent connections draining batteries

The Solution: CoAP provides “HTTP for constrained environments” - RESTful semantics compressed into a 4-byte binary header running over UDP. This chapter series teaches you everything from basic concepts to advanced implementations.

41.3 Overview

This comprehensive chapter series covers CoAP from fundamentals through advanced deployment, organized into focused topics for progressive learning. Select a topic below based on your learning goals, or follow one of the recommended learning paths.

41.4 Learning Path Visualization

CoAP learning path showing recommended progression from core concepts like introduction and message format, through intermediate topics of observe and block transfer, to advanced deployment with DTLS security and proxy patterns

CoAP Learning Path - Follow the sequence or jump to specific topics

41.5 Chapter Contents

41.5.1 Core Concepts (Start Here)

Chapter Focus Difficulty Time
CoAP Introduction Why CoAP exists, HTTP comparison, design philosophy Beginner 20 min
Message Format 4-byte header, options encoding, response codes, tokens Intermediate 25 min
Message Types and Reliability CON, NON, ACK, RST, retransmission, exponential backoff Intermediate 20 min

41.5.2 Advanced Features

Chapter Focus Difficulty Time
Observe Extension Server push notifications, observer management, efficiency Intermediate 20 min
Advanced Features Block-wise transfer, resource discovery, CoAP/TCP, DTLS Advanced 30 min

41.5.3 Design and Selection

Chapter Focus Difficulty Time
API Design Best Practices RESTful patterns, URI conventions, content formats Intermediate 25 min
Decision Framework CoAP vs MQTT vs HTTP decision matrix, real examples Intermediate 20 min

41.5.4 Hands-On Learning

Chapter Focus Difficulty Time
Features and Labs Practical implementations, code examples, ESP32 labs Intermediate 45 min
Comprehensive Review Complete protocol reference, advanced topics Advanced 60 min

41.6 Recommended Learning Paths

41.6.1 Quick Start Path (1-2 hours)

For developers who need to evaluate CoAP or make quick protocol decisions:

  1. CoAP Introduction - Understand why CoAP exists and its core value
  2. Message Types - Learn when to use CON vs NON messages
  3. Decision Framework - Know when CoAP is the right choice

41.6.2 Full Course Path (4-6 hours)

For comprehensive understanding and implementation readiness:

  1. CoAP Introduction - Foundation concepts
  2. Message Format - Byte-level understanding
  3. Message Types - Reliability mechanisms
  4. Observe Extension - Server push patterns
  5. Advanced Features - Block transfer, discovery, security
  6. API Design - Best practices for CoAP services
  7. Decision Framework - Protocol selection guidance
  8. Features and Labs - Hands-on practice

41.6.3 Reference Path

For experienced developers needing quick lookups:

41.7 Prerequisites

Before starting this chapter series, you should be familiar with:

  • Networking Basics: TCP/IP fundamentals, UDP vs TCP trade-offs
  • RESTful Concepts: HTTP methods (GET/POST/PUT/DELETE), URIs, status codes
  • IoT Architectures: Understanding of constrained devices and IoT system layers
  • Basic programming: Familiarity with any language (Python, C++, or JavaScript examples used)

This chapter connects to multiple learning resources across the IoT curriculum:

Video Resources - Visit the Videos Hub for:

  • Visual explanations of CoAP message flows and reliability mechanisms
  • Protocol comparison demonstrations (CoAP vs HTTP vs MQTT)
  • Real-world deployment case studies with network traffic analysis

Practice Quizzes - Test your understanding at Quizzes Hub:

  • CoAP message format and header field encoding
  • Message type selection (CON vs NON) scenarios
  • Response code interpretation and error handling
  • Block-wise transfer and Observe extension mechanics

Simulations - Explore interactive tools at Simulations Hub:

  • CoAP Message Builder: Construct and decode CoAP packets byte-by-byte
  • Reliability Simulator: Compare CON vs NON under different packet loss rates
  • Protocol Comparator: Side-by-side overhead analysis (CoAP vs HTTP vs MQTT)

Knowledge Gaps - Common misconceptions addressed at Knowledge Gaps Hub:

  • “CoAP is just compressed HTTP” - Understanding UDP vs TCP differences
  • “NON messages are unreliable” - When fire-and-forget is optimal
  • “CoAP can’t handle large files” - Block-wise transfer mechanisms

41.8 Key Takeaway

Remember This

CoAP is “HTTP for constrained devices” - it provides familiar RESTful semantics (GET, PUT, POST, DELETE on URIs) but compresses everything into a compact binary format running over UDP. Where HTTP requires hundreds of bytes of headers and a TCP connection, CoAP uses just 4 bytes of fixed header and no connection state. The protocol offers two message types: Confirmable (CON) for reliable delivery with acknowledgments, and Non-confirmable (NON) for fire-and-forget efficiency.

If you remember nothing else: CoAP lets your 8-bit microcontroller with 2KB RAM expose web-style REST APIs, enabling direct integration with cloud services and standard tooling without HTTP’s overhead.

Hey there, future IoT engineer! Let’s learn about CoAP with the Sensor Squad!

Sammy the Temperature Sensor wants to share readings with the cloud, just like websites do. But there’s a problem…

The Problem: Imagine you want to send a postcard (your sensor reading is just 10 words), but the post office (HTTP) makes you fill out a 100-page form every time! That’s way too much work for tiny sensors with small batteries.

The Solution - CoAP to the rescue!

Think of CoAP like a super-efficient text message system:

  • Regular websites (HTTP): Like sending a formal letter with a big envelope, address labels, stamps, and waiting for a reply before sending another
  • CoAP: Like sending a quick text message - short, fast, and you don’t have to wait!

Two types of CoAP messages:

  1. CON (Confirmable): Like asking “Did you get my message?” and waiting for “Yes!”
  2. NON (Non-confirmable): Like shouting “The temperature is 72!” and trusting someone heard you

Real-world example: Your smart thermostat uses CoAP to tell your phone the room temperature. It’s so efficient that the battery can last for years!

Fun fact: The entire CoAP message header is only 4 bytes - that’s shorter than the word “temperature”!

41.9 CoAP at a Glance

Feature Value
Full Name Constrained Application Protocol
RFC RFC 7252 (2014), Extensions: RFC 7641 (Observe), RFC 7959 (Block)
Transport UDP (default port 5683), DTLS (port 5684)
Header Size 4 bytes fixed (vs HTTP’s 200+ bytes)
Pattern Request-Response (REST style)
Message Types CON (reliable), NON (fire-and-forget), ACK, RST
Methods GET, PUT, POST, DELETE (same as HTTP)
Content Formats CBOR, JSON, plain text, application-specific
Key Features Observe (server push), Block transfer (large payloads), Discovery

41.10 Protocol Comparison

Aspect CoAP HTTP MQTT
Transport UDP (connectionless) TCP (connection-oriented) TCP (persistent)
Header Size 4 bytes fixed 200-800 bytes text 2 bytes minimum
Pattern Request-response (REST) Request-response (REST) Publish-subscribe
Broker Required No (direct device-to-device) No Yes (mandatory)
Reliability Optional (CON/NON) TCP guarantees QoS 0/1/2 levels
Server Push Observe extension Not native Core feature
RAM Required ~2KB minimum ~50KB+ typical ~10KB minimum
Best For Constrained devices, REST APIs Web integration, browsers Fan-out, many subscribers
Typical Latency Low (no handshake) Higher (TCP + TLS setup) Medium (persistent conn)
Check Your Understanding: Protocol Comparison

RAM requirements comparison: \[ \begin{align} \text{CoAP stack (minimal)} &= 2{,}048 \text{ bytes} \\ \text{MQTT stack (minimal)} &= 10{,}240 \text{ bytes} \\ \text{HTTP stack (minimal)} &= 51{,}200 \text{ bytes} \\ \text{Ratio} &= \text{CoAP : MQTT : HTTP = 1 : 5 : 25} \end{align} \]

Message size for 20-byte payload: \[ \begin{align} \text{CoAP} &= 4 + 2 + 8 + 20 = 34 \text{ bytes} \\ \text{MQTT} &= 2 + 20 + 20 = 42 \text{ bytes (header + topic + payload)} \\ \text{HTTP} &= 200 + 20 = 220 \text{ bytes (headers + payload)} \end{align} \]

Power consumption per transaction (cellular): \[ \begin{align} \text{CoAP (UDP)} &: 20 \text{ ms} \times 120 \text{ mA} = 2.4 \text{ mAs} = 0.67 \text{ }\mu\text{Ah} \\ \text{MQTT (TCP)} &: 150 \text{ ms} \times 120 \text{ mA} = 18 \text{ mAs} = 5.0 \text{ }\mu\text{Ah} \\ \text{HTTP (TCP+TLS)} &: 400 \text{ ms} \times 120 \text{ mA} = 48 \text{ mAs} = 13.3 \text{ }\mu\text{Ah} \end{align} \]

Battery life (AA 2,500 mAh, 1 msg/min): \[ \begin{align} \text{Messages/day} &= 1{,}440 \\ \text{CoAP daily} &= 1{,}440 \times 0.67 \text{ }\mu\text{Ah} = 0.96 \text{ mAh} \\ \text{Battery life} &= \frac{2{,}500}{0.96} \approx 2{,}604 \text{ days} \approx 7.1 \text{ years} \\ \text{MQTT daily} &= 1{,}440 \times 5.0 \text{ }\mu\text{Ah} = 7.2 \text{ mAh} \rightarrow 347 \text{ days} \\ \text{HTTP daily} &= 1{,}440 \times 13.3 \text{ }\mu\text{Ah} = 19.2 \text{ mAh} \rightarrow 130 \text{ days} \end{align} \]

Conclusion: CoAP’s minimal overhead enables multi-year battery life (7+ years at 1 msg/min). MQTT lasts ~1 year; HTTP only ~4 months. For battery-powered devices, CoAP is clearly optimal.

41.11 Interactive Calculators

41.11.1 CoAP Message Size Calculator

Calculate the total CoAP message size based on header components. Adjust the token length, number of options, and payload size to see how CoAP keeps messages compact.

41.11.2 Protocol Battery Life Comparator

Model your deployment scenario and compare battery life across CoAP, MQTT, and HTTP. Adjust device current, transmission time, and message frequency.

41.11.3 CoAP Retransmission Backoff Calculator

CoAP uses exponential backoff for Confirmable (CON) messages. The initial timeout is randomly chosen between ACK_TIMEOUT and ACK_TIMEOUT * ACK_RANDOM_FACTOR, then doubles on each retry up to MAX_RETRANSMIT attempts.

41.11.4 Protocol Overhead Ratio Calculator

Compare the overhead-to-payload ratio for CoAP, MQTT, and HTTP across different payload sizes. Smaller payloads amplify protocol overhead differences.

41.11.5 When to Choose Each Protocol

When evaluating protocols for your IoT deployment, consider these key decision factors:

Factor Choose CoAP Choose MQTT Choose HTTP
Communication Pattern Direct request-response (sensor queries, actuator commands) Publish-subscribe, one-to-many distribution Traditional client-server, web integration
Device Constraints <10KB RAM, battery-powered, sleep cycles >10KB RAM, persistent connectivity >50KB RAM, mains power
Message Frequency Infrequent (minutes), event-driven Frequent (seconds), continuous streaming Sporadic (hours), human-initiated
Reliability Needs Application-controlled (CON/NON choice) QoS 0/1/2 levels for different data types TCP guarantees delivery
Network Type Lossy wireless (6LoWPAN, NB-IoT) Stable (Wi-Fi, cellular) Reliable (Ethernet, 4G/5G)
Broker Infrastructure None required (direct device-to-device) Mandatory central broker Optional (load balancer)
Discovery Mechanism Native multicast + .well-known/core Broker-managed topic hierarchy DNS + service registry

Real-World Example: Smart Agriculture System

You need to monitor 500 soil sensors and control 50 irrigation valves:

Sensors to Gateway (CoAP):

  • Direct reporting: POST coap://gateway/readings {"moisture": 45}
  • 4-byte header + UDP = minimal overhead for battery life
  • NON messages for readings (next reading supersedes lost ones)
  • No broker = simpler infrastructure

Gateway to Cloud Dashboard (MQTT):

  • Multiple subscribers: mobile app, analytics, alerts
  • Retained messages: dashboard shows last reading immediately
  • QoS 1: ensure alert delivery even if network drops briefly
  • Broker handles fan-out to N subscribers efficiently

Cloud to Valve Commands (CoAP):

  • Request-response: PUT coap://valve23/state {"open": true}
  • CON messages for critical commands (guaranteed delivery)
  • Multicast for broadcast: POST coap://[FF02::FD]/all-valves {"emergency_close": true}

Hybrid Architecture Benefits:

  • CoAP for edge (500 sensors, 50 valves) = no broker, lowest power
  • MQTT for cloud = natural pub/sub, multiple consumers
  • Total infrastructure: 1 gateway + 1 MQTT broker (vs 550 MQTT connections)

41.13 Knowledge Check

41.14 Concept Relationships

This architecture overview connects all CoAP concepts in the learning journey:

Foundation Layer:

Core CoAP Concepts (Progressive Learning):

  1. CoAP Introduction - Why CoAP exists, HTTP comparison, design philosophy
  2. Message Format - Binary header, options encoding, response codes
  3. Message Types - CON/NON/ACK/RST reliability patterns
  4. Observe Extension - Server-push notifications
  5. Advanced Features - Block transfer, discovery, DTLS

Application Layer:

Deployment Context:

41.15 See Also

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.

41.16 What’s Next?

Ready to dive in? Select the chapter that matches your current goal:

Chapter Focus Why Read It
CoAP Introduction Design philosophy, HTTP comparison, core concepts Start here if you are new to CoAP — establishes the “why” before the “how”
Message Format 4-byte header, options encoding, response codes Essential for implementation — teaches you to read and write CoAP at the byte level
Message Types and Reliability CON, NON, ACK, RST, retransmission, backoff Critical for production deployments — explains how CoAP achieves reliability over UDP
Observe Extension Server push, observer management, event-driven IoT Read this to replace polling loops with efficient push notifications
Decision Framework CoAP vs MQTT vs HTTP selection matrix Use this when your team needs to justify a protocol choice with measurable criteria
Features and Labs Hands-on ESP32 implementation, code examples Go here when you are ready to write and run real CoAP code