1223  CoAP Fundamentals

1223.1 CoAP Fundamentals

⏱️ ~8 min | ⭐⭐ Intermediate | 📋 P09.C28.U01a

This chapter introduces the Constrained Application Protocol (CoAP), a lightweight RESTful protocol designed specifically for resource-constrained IoT devices.

1223.2 Learning Objectives

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

  • Understand CoAP Protocol: Explain how CoAP provides lightweight RESTful services
  • Compare with HTTP and MQTT: Differentiate between web protocols for IoT
  • Implement CoAP Communication: Use CoAP for machine-to-machine applications
  • Choose Message Types: Select appropriate confirmable vs non-confirmable messages
  • Design Resource-Constrained Systems: Apply CoAP in low-power IoT devices

Think of CoAP as “HTTP’s little sibling” built specifically for tiny devices.

You know how websites use HTTP to communicate? When you visit a webpage, your browser sends a request (GET /page) and the server responds with content. CoAP works the same way, but it’s designed for devices with very limited resources—like a battery-powered temperature sensor with only 10KB of memory.

The Problem CoAP Solves:

HTTP is great for web browsers but terrible for tiny IoT sensors because: - HTTP headers can be hundreds of bytes—wasteful when your sensor data is just “22.5°C” - HTTP requires TCP connections with multiple handshakes—drains batteries - HTTP parsing is complex—needs more processing power

How CoAP is Different:

Feature HTTP CoAP
Header size 100s of bytes Just 4 bytes!
Runs over TCP (reliable but heavy) UDP (lightweight)
Best for Web browsers Tiny sensors

Real-world analogy: HTTP is like sending a formal letter (envelope, address, return address, stamp, tracking). CoAP is like sending a postcard—minimal overhead, gets the message across.

When to Use CoAP:

  • Direct device control: “Turn on the light” → device responds “OK”
  • Reading sensor values: “What’s the temperature?” → “22.5°C”
  • Very constrained devices: Devices with <100KB RAM, running on coin-cell batteries
  • Where UDP works: Local networks, not blocked by firewalls

Key Concepts You’ll Learn:

  • CON (Confirmable): “Did you get my message?” - reliable delivery
  • NON (Non-confirmable): Fire-and-forget - fastest but no guarantee
  • GET/POST/PUT/DELETE: Same concepts as HTTP REST APIs
  • Observe: Like subscribing to updates (similar to MQTT’s publish-subscribe)

By the end of this chapter, you’ll understand when to choose CoAP vs MQTT vs HTTP for your IoT projects!

NoteKey Takeaway

In one sentence: CoAP brings REST to constrained devices over UDP, providing HTTP-like semantics with minimal overhead for resource-limited IoT sensors.

Remember this rule: Use CoAP for request-response patterns on battery-powered devices that need direct addressing; use MQTT when you need event-driven pub-sub streaming or broker-based decoupling.

1223.3 Prerequisites

Before diving into this chapter, you should be familiar with:

  • MQTT Protocol: Understanding the alternative publish-subscribe messaging approach helps appreciate CoAP’s request-response model and when to choose each protocol
  • Networking Basics: Foundation for understanding RESTful principles, HTTP methods (GET/POST/PUT/DELETE), URIs, and how CoAP adapts these concepts for constrained devices
  • UDP/IP Networking: CoAP operates over UDP rather than TCP, requiring knowledge of connectionless communication, packet loss, and reliability trade-offs

1223.4 What is CoAP?

TipIn Plain English

CoAP is like a lightweight version of the web (HTTP) designed for tiny devices. Instead of heavy web requests, CoAP uses small UDP packets - perfect for sensors with limited memory and battery.

Everyday Analogy: If HTTP is a formal letter with envelope, address, and tracking, CoAP is a postcard - gets the message across with much less overhead.

When you’d use it: A battery-powered temperature sensor that needs to last 2 years, a smart light bulb that responds to commands instantly, or a building with 1,000 sensors where bandwidth is limited.

TipDefinition

CoAP (Constrained Application Protocol) is a specialized web transfer protocol designed for resource-constrained nodes and networks. It provides a lightweight RESTful interface similar to HTTP, but optimized for IoT devices with limited processing power, memory, and energy.

Designed by: IETF Constrained RESTful Environment (CoRE) Working Group

Key Characteristics:

  • Transport: UDP (not TCP like HTTP)
  • Architecture: RESTful (like HTTP)
  • Security: DTLS (Datagram TLS)
  • Overhead: Minimal (4-byte header vs HTTP’s larger headers)
  • Network: IPv4 and IPv6 (esp. for IEEE 802.15.4)
  • Use Cases: Smart energy, building automation, sensor networks

1223.5 Why CoAP Instead of HTTP?

1223.5.1 The Problem with HTTP for IoT

HTTP is excellent for the web, but problematic for constrained devices:

Issue HTTP CoAP
Protocol TCP (connection-oriented) UDP (lightweight)
Diagram showing CoAP's four message types: Confirmable (CON) requires acknowledgment for reliable delivery, Non-Confirmable (NON) provides fire-and-forget communication without acknowledgment, Acknowledgment (ACK) confirms receipt of CON messages, and Reset (RST) rejects invalid messages
Figure 1223.1: CoAP message types: CON, NON, ACK, RST

Geometric diagram of CoAP message format showing compact 4-byte fixed header containing Version (2 bits), Type (2 bits for CON/NON/ACK/RST), Token Length (4 bits), Code (8 bits combining class and detail), and Message ID (16 bits). Variable-length Token, Options, and Payload follow the header. Shows how CoAP achieves minimal overhead compared to HTTP headers

CoAP Header Format
Figure 1223.2: CoAP’s 4-byte base header achieves minimal overhead while providing reliable messaging options. The compact design is essential for resource-constrained devices with limited memory and bandwidth.

Artistic visualization of CoAP Observe mechanism showing client registering interest in a resource, then server pushing updates whenever the resource value changes. Demonstrates how CoAP enables publish-subscribe patterns similar to MQTT while maintaining RESTful semantics. Shows notification sequence with token matching for multiple observations

CoAP Observe Mechanism
Figure 1223.3: CoAP Observe extends the RESTful model with server push notifications. Clients register interest in a resource, and the server pushes updates on value changes, combining the simplicity of REST with the efficiency of publish-subscribe.

Artistic visualization of CoAP protocol stack showing Application at top, CoAP layer providing RESTful interface, DTLS for security, UDP for transport, and IP/6LoWPAN at network layer, compared with HTTP stack showing HTTP over TLS over TCP.

CoAP Protocol Stack
Figure 1223.4: CoAP protocol stack vs HTTP stack

CoAP runs over UDP rather than TCP, eliminating connection establishment overhead. For security, CoAP uses DTLS (Datagram TLS) which provides the same cryptographic protection as TLS but adapted for unreliable transport. The 6LoWPAN adaptation layer enables CoAP over IEEE 802.15.4 networks.

Geometric diagram of CoAP request-response transaction showing confirmable GET request from client to server, followed by piggybacked ACK response containing the requested resource representation, demonstrating reliable message exchange.

CoAP Request-Response
Figure 1223.5: CoAP confirmable request with piggybacked response

Confirmable messages (CON) provide reliability over unreliable UDP. The server can piggyback the response directly on the ACK, reducing round trips. For slow operations, the server sends an empty ACK immediately and the response separately (separate response pattern).

Artistic representation of CoAP Block-wise transfer showing large resource split into multiple blocks with Block1 option for uploads and Block2 for downloads, enabling transfer of resources larger than typical CoAP message size limits.

CoAP Block Transfer
Figure 1223.6: CoAP block-wise transfer for large resources

Block-wise transfer (RFC 7959) enables transferring resources larger than typical CoAP MTU limits. Block1 handles request payloads (e.g., firmware uploads), while Block2 handles response payloads. Each block is individually acknowledged, providing partial reliability.

Geometric diagram of CoAP resource discovery showing client sending GET request to /.well-known/core endpoint, server responding with CoRE Link Format listing available resources with attributes like rt (resource type), if (interface), and sz (size).

CoAP Resource Discovery
Figure 1223.7: CoAP resource discovery using .well-known/core

CoAP devices expose their resources through the /.well-known/core URI, responding in CoRE Link Format. This enables automatic discovery of available sensors, actuators, and their capabilities, supporting zero-configuration deployments.

Header Size | 100s of bytes | 4 bytes minimum |
Power Consumption | High (TCP handshake) | Very low |
Overhead | Significant | Minimal |
Complexity | Complex parsing | Simple |
Best For | Web browsers | Sensors, actuators |
TipReal-World Example: Smart Light Bulb Control

Scenario: Your smartphone wants to turn on a Philips Hue-style smart light bulb.

HTTP Approach:

Total bytes: 547 bytes
- TCP handshake: 3 packets (SYN, SYN-ACK, ACK)
- HTTP request: ~200 bytes
  GET /light/on HTTP/1.1
  Host: 192.168.1.50
  User-Agent: SmartHome/1.0
  Accept: application/json
  Connection: keep-alive
  [blank line]
- HTTP response: ~150 bytes
  HTTP/1.1 200 OK
  Content-Type: application/json
  Content-Length: 15
  {"status":"on"}
- Time: 150-300ms
- Energy: ~45 mJ

CoAP Approach:

Total bytes: 28 bytes
- CoAP request: 14 bytes
  CON GET /light/on
  (4-byte header + 10 bytes token/options)
- CoAP response: 14 bytes
  ACK 2.04 Changed
  (4-byte header + 10 bytes payload)
- Time: 20-50ms
- Energy: ~3 mJ

Result: CoAP uses 95% fewer bytes, 10x faster response, and 15x less energy than HTTP. For a battery-powered light switch, this means 2 years of battery life vs 2 months with HTTP!

Why this matters: In a smart building with 500 light switches each pressed 20 times/day, CoAP saves 3.7 GB of bandwidth and $1,200/year in cellular data costs compared to HTTP.

WarningUDP Means No Reliability Guarantees

CoAP uses UDP, which has critical implications for IoT deployments:

UDP characteristics: - No connection - packets can arrive out-of-order or be lost - No automatic retransmission - application must handle with CON messages - No congestion control - can overwhelm networks if not careful - Firewall/NAT challenges - many networks block UDP traffic

When this causes problems: - Lossy Wi-Fi/cellular networks: 10-30% packet loss means frequent retransmissions - Firewall traversal: Corporate networks often block UDP, preventing CoAP communication - NAT timeout: UDP “connections” expire faster than TCP (30-60 seconds typical)

Solutions: - Use CON messages for important data (adds reliability) - Implement exponential backoff for retransmissions - Consider MQTT (TCP-based) for unreliable networks - Use CoAP over DTLS for secure, more reliable communication - Test thoroughly in production network conditions

NoteWhat Would Happen If: 15% Packet Loss Scenario

Scenario: You deploy 50 CoAP temperature sensors in a warehouse with spotty Wi-Fi. Network analysis shows 15% packet loss.

With NON (Non-Confirmable) Messages:

%%{init: {'theme': 'base', 'themeVariables': {'primaryColor': '#2C3E50', 'primaryTextColor': '#fff', 'primaryBorderColor': '#16A085', 'lineColor': '#16A085', 'secondaryColor': '#E67E22', 'tertiaryColor': '#7F8C8D'}}}%%
flowchart TB
    subgraph deployment["NON Message Deployment"]
        d1["50 sensors every 60 seconds"]
        d2["Expected: 3,000 readings/hour"]
        d3["Reality with 15% loss: 2,550/hour"]
        d4["450 readings lost!"]
    end

    subgraph impact["Impact"]
        i1["✗ Data gaps: 15% missing"]
        i2["✗ False alerts: zones appear offline"]
        i3["✗ Compliance: no continuous monitoring proof"]
        i4["✓ Energy: 50% better battery life"]
    end

    deployment --> impact

    style d4 fill:#E67E22,stroke:#2C3E50,stroke-width:2px,color:#fff
    style i1 fill:#7F8C8D,stroke:#2C3E50,stroke-width:2px,color:#fff
    style i2 fill:#7F8C8D,stroke:#2C3E50,stroke-width:2px,color:#fff
    style i3 fill:#7F8C8D,stroke:#2C3E50,stroke-width:2px,color:#fff
    style i4 fill:#16A085,stroke:#2C3E50,stroke-width:2px,color:#fff

Figure 1223.8: Impact of using NON messages showing 15 percent data loss but 50 percent better battery life

With CON (Confirmable) Messages:

%%{init: {'theme': 'base', 'themeVariables': {'primaryColor': '#2C3E50', 'primaryTextColor': '#fff', 'primaryBorderColor': '#16A085', 'lineColor': '#16A085', 'secondaryColor': '#E67E22', 'tertiaryColor': '#7F8C8D'}}}%%
flowchart TB
    subgraph retries["CON Retry Sequence"]
        r1["First attempt: 42.5 succeed, 7.5 fail"]
        r2["Retry #1: 6.4 succeed, 1.1 fail"]
        r3["Retry #2: 0.9 succeed, 0.2 fail"]
        r4["Final: 49.8 of 50 = 99.6% success!"]
    end

    subgraph impact["Impact"]
        i1["✓ Data completeness: 99.6%"]
        i2["⚠ Network overhead: 2.2x packets"]
        i3["⚠ Battery life: Cut in half"]
        i4["⚠ Latency: 150ms vs 50ms NON"]
    end

    r1 --> r2 --> r3 --> r4
    retries --> impact

    style r4 fill:#16A085,stroke:#2C3E50,stroke-width:2px,color:#fff
    style i1 fill:#16A085,stroke:#2C3E50,stroke-width:2px,color:#fff
    style i2 fill:#E67E22,stroke:#2C3E50,stroke-width:2px,color:#fff
    style i3 fill:#E67E22,stroke:#2C3E50,stroke-width:2px,color:#fff
    style i4 fill:#E67E22,stroke:#2C3E50,stroke-width:2px,color:#fff

Figure 1223.9: Impact of using CON messages showing 99.6 percent delivery rate but doubled battery consumption

The Trade-Off Decision:

Approach Use When Don’t Use When
Stick with NON Data is statistical (average temps okay), battery life critical, dashboards show “last reading: 2 min ago” is acceptable Regulatory compliance required, missing data causes alarms, each reading is critical
Switch to CON Each reading matters (billing, safety), can afford battery replacement, data integrity > battery life Battery replacement impossible (remote sensors), network has >20% loss (retransmissions overwhelm it), latency-sensitive
Hybrid approach Send NON normally, CON for alerts/changes -
Switch to MQTT/TCP Persistent 15%+ loss, infrastructure supports broker, many sensors Low power critical, direct device-to-device needed

Real-World Fix: The warehouse deployed a mesh Wi-Fi extender ($80), reducing packet loss to 2%. Kept NON messages, achieved 98% delivery rate with 2-year battery life. Cost of solution < cost of 6-month battery replacements.

TipWhen to Choose CoAP Over MQTT

Both are lightweight IoT protocols, but serve different use cases:

Choose CoAP when: - You need request-response pattern (like HTTP GET/POST) - RESTful API design is important (resources with URIs) - Minimal overhead is critical (4-byte header vs MQTT’s 2-byte minimum + TCP overhead) - Devices sleep most of the time (CoAP stateless, reconnects fast) - Integration with web services (CoAP to HTTP proxies exist)

Choose MQTT when: - You need publish-subscribe (one-to-many communication) - Persistent connections are acceptable - Network is unreliable (TCP handles retransmissions automatically) - Multiple subscribers need same data - Broker-based architecture fits your deployment

Example: Smart thermostat that responds to user commands → CoAP. Temperature sensor publishing to dashboard and logging service → MQTT.

1223.5.2 REST for Constrained Devices

REST (Representational State Transfer) is the standard interface between HTTP clients and servers. CoAP brings REST to IoT:

%%{init: {'theme': 'base', 'themeVariables': { 'primaryColor': '#2C3E50', 'primaryTextColor': '#fff', 'primaryBorderColor': '#16A085', 'lineColor': '#E67E22', 'secondaryColor': '#7F8C8D', 'tertiaryColor': '#ECF0F1', 'fontSize': '14px'}}}%%
graph TB
    Client[CoAP Client]
    Server[CoAP Server]

    Client -->|GET /temperature| Server
    Server -->|2.05 Content: 22.5°C| Client

    Client2[CoAP Client] -->|POST /sensor<br/>payload: data| Server2[CoAP Server]
    Server2 -->|2.01 Created| Client2

    Client3[CoAP Client] -->|PUT /config<br/>payload: settings| Server3[CoAP Server]
    Server3 -->|2.04 Changed| Client3

    style Client fill:#16A085,stroke:#2C3E50,stroke-width:2px,color:#fff
    style Server fill:#E67E22,stroke:#2C3E50,stroke-width:2px,color:#fff
    style Client2 fill:#16A085,stroke:#2C3E50,stroke-width:2px,color:#fff
    style Server2 fill:#E67E22,stroke:#2C3E50,stroke-width:2px,color:#fff
    style Client3 fill:#16A085,stroke:#2C3E50,stroke-width:2px,color:#fff
    style Server3 fill:#E67E22,stroke:#2C3E50,stroke-width:2px,color:#fff

Figure 1223.10: CoAP RESTful Operations: GET, POST, and PUT Method Examples

%%{init: {'theme': 'base', 'themeVariables': { 'primaryColor': '#2C3E50', 'primaryTextColor': '#fff', 'primaryBorderColor': '#16A085', 'lineColor': '#E67E22', 'secondaryColor': '#7F8C8D'}}}%%
graph TB
    subgraph Confirmable["CON (Confirmable)"]
        C1["Requires ACK"]
        C2["Reliable delivery"]
        C3["Retransmit if no ACK"]
    end

    subgraph NonConfirmable["NON (Non-Confirmable)"]
        N1["No ACK needed"]
        N2["Fire-and-forget"]
        N3["Periodic telemetry"]
    end

    subgraph Acknowledgment["ACK (Acknowledgment)"]
        A1["Response to CON"]
        A2["May carry payload"]
    end

    subgraph Reset["RST (Reset)"]
        R1["Error response"]
        R2["Unable to process"]
    end

    style Confirmable fill:#16A085,stroke:#2C3E50
    style NonConfirmable fill:#E67E22,stroke:#2C3E50
    style Acknowledgment fill:#7F8C8D,stroke:#2C3E50
    style Reset fill:#2C3E50,stroke:#16A085

This diagram shows CoAP’s four message types: CON for reliable transfer, NON for fire-and-forget, ACK for confirmations, and RST for error handling.

REST principles in CoAP: - Resources identified by URIs (like HTTP) - Standard methods: GET, POST, PUT, DELETE - Stateless client-server architecture - Content-type support (JSON, XML, etc.)

1223.6 CoAP Architecture

CoAP is divided into two sublayers:

%%{init: {'theme': 'base', 'themeVariables': { 'primaryColor': '#2C3E50', 'primaryTextColor': '#fff', 'primaryBorderColor': '#16A085', 'lineColor': '#E67E22', 'secondaryColor': '#7F8C8D', 'tertiaryColor': '#ECF0F1', 'fontSize': '14px'}}}%%
graph TB
    subgraph "CoAP Architecture"
        REQ[Request/Response Layer<br/>GET, POST, PUT, DELETE<br/>Resource Operations]
        MSG[Messaging Layer<br/>CON, NON, ACK, RST<br/>Reliability & Duplicate Detection]
        UDP[UDP Transport Layer]
    end

    REQ --> MSG
    MSG --> UDP

    style REQ fill:#E67E22,stroke:#2C3E50,stroke-width:2px,color:#fff
    style MSG fill:#16A085,stroke:#2C3E50,stroke-width:2px,color:#fff
    style UDP fill:#7F8C8D,stroke:#2C3E50,stroke-width:2px,color:#fff

Figure 1223.11: CoAP Two-Layer Architecture: Request/Response and Messaging

1223.6.1 Messaging Layer

Responsibilities: - Reliability (when needed) - Duplicate message detection - Message ID management - Acknowledgments

1223.6.2 Request/Response Layer

Responsibilities: - Client-server communication - Resource operations (GET, POST, PUT, DELETE) - URI/URL resource retrieval - Content negotiation

1223.7 What’s Next

Now that you understand the fundamentals of CoAP and its architecture, the next chapter explores CoAP Message Types where you’ll learn about:

  • Confirmable (CON) and Non-Confirmable (NON) messages
  • Acknowledgment (ACK) and Reset (RST) responses
  • Message exchange patterns (piggyback vs separate responses)
  • Trade-offs between reliability and battery life