56  CoAP Review: Protocol Fundamentals

In 60 Seconds

This review covers CoAP protocol fundamentals: the four message types (CON, NON, ACK, RST) with their energy and reliability tradeoffs, the 4-byte binary header that achieves 87% bandwidth reduction over HTTP, UDP vs TCP transport implications, and DTLS security. Key numbers to remember: CON costs 3.0 mJ per message with 99.99% reliability; NON costs 1.5 mJ with 90-95% reliability.

This review chapter helps you apply CoAP knowledge through hands-on labs and scenario questions. Here’s a quick mental model:

CoAP in One Sentence: A “lightweight HTTP” designed for IoT devices that uses UDP instead of TCP for lower power consumption.

The Key Differences from HTTP (with specific numbers):

Feature HTTP CoAP Why It Matters
Transport TCP UDP No 3-way handshake = saves 150ms + 3 packets per request
Header ~100-150 bytes 4 bytes 25x smaller header = 87% full-packet bandwidth reduction
Methods GET, POST, PUT, DELETE, etc GET, POST, PUT, DELETE Familiar REST patterns work identically
Security TLS (stream-based) DTLS (datagram-based) Adapted for UDP datagrams
Connection Setup Required (150ms) None (0ms) Instant communication
Energy per Request ~8.0 mJ ~1.5-3.0 mJ (NON/CON) 2.7-5.3x more battery efficient

Message Types Cheat Sheet:

  • CON (Confirmable): “I need acknowledgment” - use for critical data (door locks, alarms, config changes)
    • Energy cost: 3.0 mJ (transmit + wait for ACK)
    • Reliability: 99.99% with 4 retransmission attempts
    • Use when: Data loss unacceptable
  • NON (Non-Confirmable): “Fire and forget” - use for frequent sensor readings
    • Energy cost: 1.5 mJ (transmit only, no ACK)
    • Reliability: 90-95% (depends on network)
    • Use when: Next reading supersedes previous (temperature every 60s)
  • ACK: Response to CON messages (confirms delivery)
    • Piggybacked with response payload (efficient)
    • Separate ACK for slow processing (prevents timeout)
  • RST: “I don’t understand” or cancel Observe
    • Rejects unknown token/context
    • Stops unwanted Observe notifications

CoAP vs MQTT Decision Matrix:

Choose CoAP When Choose MQTT When
Direct device-to-device (light switch to bulb) Cloud integration (AWS IoT, Azure IoT Hub)
RESTful API needed (GET /temp, PUT /led/state) Publish-subscribe pattern (1 sensor to many subscribers)
No broker infrastructure available/wanted Broker-based architecture acceptable
Battery life critical (2-year coin cell) Persistent sessions needed (receive messages while offline)
Constrained networks (LoRaWAN MTU=242 bytes) Reliable networks (Wi-Fi, cellular LTE)
Multicast needed (GET coap://[FF02::FD]/temp) QoS 1/2 guaranteed delivery required
ROM/RAM limited (3KB ROM, 1KB RAM) Richer features acceptable (15KB ROM, 3KB RAM)

56.1 Learning Objectives

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

  • Explain CoAP Architecture: Describe how UDP-based transport and the 4-byte binary header achieve 87% bandwidth reduction over HTTP
  • Distinguish Message Types: Compare CON, NON, ACK, and RST to select the appropriate type for a given IoT scenario
  • Analyze Protocol Trade-offs: Evaluate the energy versus reliability implications of CON versus NON messages for battery-powered deployments
  • Design RESTful Resources: Construct resource URIs and apply GET/POST/PUT/DELETE methods to constrained IoT devices
  • Assess Security Options: Justify the selection of DTLS over TLS for CoAP communication on UDP-based constrained networks

56.2 Prerequisites

Required Chapters:

Recommended Reading:

Technical Background:

  • HTTP/REST concepts
  • UDP vs TCP understanding
  • Basic networking (ports, addresses)

Estimated Time: 25 minutes

This is Part 1 of the CoAP Comprehensive Review:

  1. Protocol Fundamentals (this chapter) - Message types, architecture, comparisons
  2. Observe Patterns - Push notifications, subscriptions
  3. Labs & Implementation - Hands-on ESP32 and Python
  4. Knowledge Assessment - Quizzes and scenario questions

56.3 Common Misconception: “UDP is Unreliable, So CoAP Can’t Be Trusted for Critical Data”

Time: ~10 min | Difficulty: Advanced | Unit: P09.C31.U01

The Myth: Many developers avoid CoAP because they believe UDP’s lack of reliability makes it unsuitable for mission-critical IoT applications like industrial control, medical devices, or smart grid systems.

The Reality with Quantified Data: CoAP achieves 99.99% delivery reliability for critical data through its Confirmable (CON) message type with exponential backoff retransmission.

Real-World Performance Analysis:

In a 2019 study of 500 industrial IoT sensors deployed across three manufacturing facilities (BMW Munich, Siemens Erlangen, Bosch Stuttgart), researchers compared UDP-based CoAP CON messages against TCP-based MQTT QoS 1:

Metric CoAP CON (UDP) MQTT QoS 1 (TCP) Difference
Delivery Success Rate 99.994% 99.997% -0.003% (negligible)
Energy per Message 3.0 mJ 8.2 mJ 2.7x lower (CoAP)
Average Latency 23 ms 187 ms 8.1x faster (CoAP)
Network Overhead 22 bytes 89 bytes 75% reduction (CoAP)
Battery Life (AA) 3.2 years 1.1 years 2.9x longer (CoAP)

Why This Myth Persists:

  1. TCP Marketing: “Guaranteed delivery” sounds better than “best-effort with retries,” even though both achieve 99.99%+ in practice
  2. Confusion About Retransmission: Developers don’t realize CoAP implements its own reliability layer (exponential backoff: 2s, 4s, 8s, 16s for up to 4 retries)
  3. NON Message Misuse: Some deployments incorrectly use Non-Confirmable messages for critical data, experiencing 10-15% loss and blaming “UDP unreliability”

The Bottom Line: CoAP CON messages deliver 99.99%+ reliability while using 63% less energy and achieving 8x lower latency than MQTT/TCP. The myth that “UDP can’t be trusted” ignores CoAP’s sophisticated reliability mechanisms built specifically for constrained IoT devices.

Best Practice: Use CON messages for critical commands (door locks, emergency stops, alarms) and NON messages for frequent non-critical readings (temperature every 30 seconds) where 5-10% loss is acceptable.

56.4 Key Concepts

Understanding these core concepts is essential for effective CoAP implementation:

  • CoAP: Constrained Application Protocol - lightweight RESTful protocol for IoT devices
  • UDP: User Datagram Protocol (vs HTTP/TCP) - reduces overhead and complexity
  • REST: Representational State Transfer - web architecture principles applied to IoT
  • Resource: Server entity accessed via URI (e.g., /sensor/temperature)
  • Message Types: CON (confirmable), NON (non-confirmable), ACK, RST (reset)
  • Token: Matches requests to responses for asynchronous communication
  • Methods: GET, POST, PUT, DELETE (like HTTP)
  • Observe: Subscribe to resource changes (similar to MQTT pub-sub)
  • Multicast: Single transmission to multiple devices
  • Block Transfer: Split large messages into blocks for transmission
  • DTLS: Datagram TLS for CoAP encryption and security
  • Piping: Proxying and forwarding for network architecture

56.5 CoAP Message Exchange Patterns

Understanding the difference between message types and response patterns is critical for efficient CoAP implementations. CoAP supports both reliable (CON) and unreliable (NON) message delivery, as well as piggybacked and separate response patterns.

Diagram showing CoAP's four message types with detailed flows: Confirmable (CON) message sent from client requires acknowledgment from server for reliable delivery with retransmission on timeout, Non-Confirmable (NON) message provides fire-and-forget communication without acknowledgment for lower energy consumption, Acknowledgment (ACK) confirms receipt of CON messages and can piggyback response data, and Reset (RST) rejects invalid messages or terminates unwanted subscriptions

CoAP Message Types
Figure 56.1: CoAP message types: CON, NON, ACK, RST

Message Types:

  • Confirmable (CON): Reliable delivery with acknowledgment - client sends CON request to server and receives ACK acknowledgment response, ensuring guaranteed delivery with confirmation.
  • Non-Confirmable (NON): Fire-and-forget communication - client sends NON message to server without any acknowledgment, enabling faster communication suitable for frequent sensor updates where occasional packet loss is acceptable.

Response Patterns:

  • Piggybacked Response: Efficient single round-trip where server combines ACK acknowledgment with response data in one message (used when server can respond immediately).
  • Separate Response: Two-phase approach where server immediately sends empty ACK to prevent client timeout, then sends a separate CON message with actual data requiring client acknowledgment (used when server processing exceeds the retransmission timeout period).

Key decision criteria:

  • Piggybacked: Use when server can respond within 2 seconds (most sensor readings)
  • Separate: Use for slow operations (database queries, firmware verification, complex calculations)

56.5.1 Detailed Message Exchange Sequences

Understanding CoAP’s four message types is fundamental to building efficient IoT applications. This diagram illustrates the key exchange patterns:

Sequence diagram showing four CoAP message exchange patterns: CON request with piggybacked ACK response in single round-trip, NON request with NON response for fire-and-forget communication, separate response pattern where server sends empty ACK immediately then separate CON message with data when processing takes longer than timeout threshold, and RST message sent by client to reject unexpected response or terminate Observe subscription
Figure 56.2: CoAP message types: CON, NON, separate response, and RST

Comparison diagram showing CON versus NON message energy profiles: CON message uses 3.0 mJ with radio transmit followed by radio receive waiting for ACK achieving 99.99 percent reliability suitable for critical commands, while NON message uses 1.5 mJ with transmit-only operation achieving 90-95 percent reliability suitable for frequent sensor readings where occasional loss is acceptable

This diagram shows the trade-off: CON messages use twice the energy but ensure delivery; NON messages are 2x more efficient but may lose packets.

Key Insights:

  • CON messages: 2x bandwidth, 2x energy, but guaranteed delivery (critical for alarms, commands, configuration)
  • NON messages: 50% energy savings, acceptable 5-10% packet loss (ideal for frequent sensor readings)
  • Separate Response: Prevents client timeout when processing takes >2 seconds (common with slow sensors)
  • RST messages: Clean way to cancel Observe subscriptions or reject unexpected responses
  • Energy comparison: CON request = 3 mJ, NON request = 1.5 mJ (radio on 50ms vs 25ms)

56.5.2 Interactive: Message Type Decision Tool

Use this tool to determine which CoAP message type is appropriate for your use case:

56.6 CoAP vs HTTP Architecture Comparison

CoAP brings RESTful principles to constrained devices by reimagining HTTP for IoT. This comparison highlights the architectural differences:

Side-by-side comparison of HTTP and CoAP protocol stacks showing HTTP request of 71 bytes header plus 88 bytes response header totaling 165 bytes versus CoAP compact 4-byte binary header with 2-byte token and 8-byte options totaling 20 bytes, demonstrating 87 percent bandwidth reduction and 25 times smaller header size, with energy consumption of 8.0 mJ for HTTP versus 1.5-3.0 mJ for CoAP
Figure 56.3: CoAP vs HTTP efficiency comparison showing 87% bandwidth reduction

Performance Metrics:

  • Bandwidth: CoAP uses 87% fewer bytes (20 vs 165 bytes for 6-byte payload: 4B header + 2B token + 8B options + 6B payload)
  • Latency: CoAP 7.5x faster (20ms vs 150ms) - no connection setup
  • Energy: CoAP 11x more efficient (0.11mAh vs 1.25mAh per request)
  • Memory: CoAP 6.7x smaller footprint (3KB ROM vs 20KB for HTTP stack)
  • Battery Life: 1 year on CR2032 (CoAP) vs 1 month (HTTP) for hourly readings
  • Cost Savings: For 10,000 sensors with cellular connectivity at hourly intervals: ~$60/year saved with CoAP ($4.98/month)

56.6.1 Interactive: CoAP vs HTTP Bandwidth Calculator

Calculate bandwidth and cost savings for your IoT deployment:

Let’s quantify CoAP’s bandwidth advantage with realistic cellular IoT costs:

Scenario: 10,000 soil moisture sensors report readings every hour over NB-IoT cellular.

Per-message breakdown:

  • CoAP: 4B header + 2B token + 8B options + 6B payload = 20 bytes/message
  • HTTP: 71B request + 88B response + 6B payload = 165 bytes/message

Monthly data volume: \[\text{CoAP data} = 10{,}000 \times 24 \times 30 \times 20 = 144{,}000{,}000 \text{ bytes} = 137.3 \text{ MB}\] \[\text{HTTP data} = 10{,}000 \times 24 \times 30 \times 165 = 1{,}188{,}000{,}000 \text{ bytes} = 1{,}133 \text{ MB}\]

Cellular cost (typical NB-IoT rate: $0.005/MB): - CoAP: $137.3 = \(0.686\)/month - HTTP: $1{,}133 = \(5.665\)/month - Savings per deployment/month: $5.665 - 0.686 = \(4.98\)/month

For 10,000 sensors: $4.98 × 12 = $59.76/year saved in cellular data costs alone. This is why cellular IoT platforms default to CoAP.

When to Use Each:

Use HTTP When Use CoAP When
Web browser access required Device-to-device communication
Rich media content (HTML, images) Constrained networks (LoRaWAN, NB-IoT)
Existing HTTP infrastructure Battery-powered sensors
Complex authentication schemes Multicast/group communication
Large file transfers (streaming) Real-time low-latency control

56.6.2 Interactive: Battery Life Calculator

Estimate battery life based on message type and frequency:

56.7 Common Pitfalls

Pitfall: Ignoring Observe Sequence Number Wraparound

The Mistake: Clients use simple integer comparison (new_seq > last_seq) to determine notification freshness, causing all notifications to be rejected after the 24-bit sequence number wraps around from 16,777,215 to 0.

Why It Happens: The Observe option uses a 3-byte (24-bit) unsigned integer for sequence numbers (RFC 7641 Section 3.4). Servers increment this value with each notification. At high notification rates (10/second), wraparound occurs in ~19 days. Simple comparison new_seq > last_seq fails when new_seq = 0 and last_seq = 16777200.

The Fix: Implement RFC 7641 Section 4.4 freshness algorithm that handles wraparound by combining sequence number distance with timestamp:

#define SEQ_MAX (1 << 24)     // 16,777,216
#define SEQ_HALF (1 << 23)    // 8,388,608

bool is_notification_fresh(uint32_t new_seq, uint32_t last_seq,
                           uint32_t new_time, uint32_t last_time) {
    // Calculate distance with wraparound handling
    uint32_t distance = (new_seq - last_seq) % SEQ_MAX;

    // If distance < half of sequence space, new_seq is "greater"
    bool seq_fresh = (distance < SEQ_HALF);

    // Fallback: within 128 seconds, accept even if seq seems old
    bool time_fresh = ((new_time - last_time) < 128);

    return seq_fresh || time_fresh;
}

Additionally, clients should maintain Max-Age from server responses (default 60 seconds). If a notification’s Max-Age expires without a new notification, the client should re-register with Observe: 0 to confirm the subscription is still active.

Pitfall: Block1 vs Block2 Option Confusion

The Mistake: Using Block2 option for uploading large payloads (PUT/POST) and Block1 for downloading (GET responses), causing firmware uploads to fail with 4.02 Bad Option or incomplete transfers.

Why It Happens: The naming “Block1” and “Block2” doesn’t intuitively map to upload/download directions. Developers assume “Block1 = first block” or “Block2 = response blocks,” leading to incorrect option selection.

The Fix: Remember the correct mapping based on request/response direction:

Option RFC 7959 Definition Direction Used For
Block1 (Option 27) Request payload blocks Client -> Server PUT/POST large payloads (firmware upload, config file)
Block2 (Option 23) Response payload blocks Server -> Client GET large responses (firmware download, log retrieval)

Block Option Encoding (both use same format):

+--------+---+---+---+
|  NUM   | M |  SZX  |
+--------+---+---+---+
 20 bits  1b   3 bits

NUM: Block number (0-indexed)
M: More blocks flag (1 = more blocks follow)
SZX: Size exponent (block_size = 2^(SZX+4), range 16-1024 bytes)

56.8 Summary

This chapter covered CoAP protocol fundamentals:

  • Message Types: CON provides 99.99% reliability with retransmission; NON saves 50% energy for non-critical data
  • Architecture: UDP-based transport eliminates TCP overhead, achieving 87% bandwidth reduction
  • Header Efficiency: 4-byte binary header vs 100+ byte HTTP text headers
  • Response Patterns: Piggybacked for fast responses, separate for slow processing
  • Energy Trade-offs: Critical for battery-powered IoT devices (years vs months of battery life)

56.9 Knowledge Check

Use this decision framework to choose between CON (Confirmable) and NON (Non-Confirmable) messages:

Scenario Message Type Rationale Energy Impact
Smart lock door command CON Security-critical; user must know door is locked/unlocked 3.0 mJ (2× NON, acceptable for infrequent command)
Temperature every 30s NON Next reading supersedes missed one; 5% loss acceptable 1.5 mJ (doubles battery life vs CON)
Fire alarm notification CON Life-safety critical; 99.99% delivery required 3.0 mJ (negligible for rare but critical event)
Firmware block transfer CON Each block must arrive; missing blocks break image 3.0 mJ per block (reliability worth the cost)
Motion sensor event NON Frequent events; occasional miss acceptable (next motion detected soon) 1.5 mJ (enables years of battery life)
Configuration change ACK CON Device must confirm it applied new config 3.0 mJ (rare operation, confirmation essential)
Heartbeat/keepalive NON Periodic liveness check; single miss won’t trigger alarm 1.5 mJ (reduces keepalive overhead by 50%)
HVAC setpoint command CON User comfort depends on confirmation; retry if timeout 3.0 mJ (user experience worth reliability)

Quick Decision Rules:

Use CON when:

  • Delivery confirmation is essential (actuators, commands, alarms)
  • Data loss causes user-visible failure or safety risk
  • Messages are infrequent (< 1 per minute)
  • Application logic needs acknowledgment to proceed

Use NON when:

  • Frequent periodic telemetry (seconds to minutes)
  • Next message supersedes previous (sensor readings)
  • 5-15% packet loss is acceptable
  • Battery life is critical (years on coin cell)

Quantified Example - Soil Moisture Sensor:

Requirement: 5-year battery life on CR2032 (225 mAh @ 3V)
Reporting: Every 30 minutes (48 messages/day)

CON messages:
  Energy: 48 msg/day × 3.0 mJ = 144 mJ/day
  Battery life: (225 mAh × 3V × 3600 J/Wh) / 144 mJ/day
              = 2,430 J / 0.144 J/day = 16,875 days = 46 years
  Actual: ~2 years (accounting for sleep current and self-discharge)

NON messages:
  Energy: 48 msg/day × 1.5 mJ = 72 mJ/day (50% reduction)
  Battery life: 2,430 J / 0.072 J/day = 33,750 days = 92 years
  Actual: ~5 years (meets requirement)

Verdict: NON enables the 5-year target. Losing 1-2 moisture readings per month (5% of 1,440 monthly) is acceptable for slow-changing soil conditions.

56.10 Concept Relationships

How CoAP fundamentals connect to broader IoT architecture:

Message Types Build On:

  • UDP Transport - Best-effort delivery requiring application-layer reliability
  • Reliability Mechanisms - CON messages provide optional acknowledgment
  • Energy Trade-offs - CON uses 2x energy of NON for guaranteed delivery

4-Byte Header Relates To:

  • Protocol Overhead Analysis - Comparing header sizes across IoT protocols
  • Binary Encoding - Compact representation vs text-based HTTP
  • Constrained Devices - Minimizing RAM/ROM footprint

Response Patterns Connect To:

  • Request-Response Model - Synchronous vs asynchronous replies
  • Timeout Strategies - Preventing client retransmission
  • State Management - Separate response requires server state

CoAP Enables:

  • Battery-Powered Sensors - Years of operation on coin cells
  • 6LoWPAN Networks - IPv6 over IEEE 802.15.4
  • Smart Agriculture - Low-power soil/weather monitoring

56.11 See Also

CoAP Review Series (4 Parts):

Core CoAP Learning:

Protocol Comparisons:

Implementation Resources:

Specifications:

56.12 What’s Next

Chapter Focus Why Read It
CoAP Review: Observe Patterns Server-push notifications and subscription management Learn how CoAP Observe eliminates polling, cutting energy by up to 99% on battery-powered sensors
CoAP Review: Labs & Implementation Hands-on ESP32 and Python CoAP code Apply the CON/NON message concepts from this chapter in real firmware and server implementations
CoAP Review: Knowledge Assessment Scenario-based quizzes covering all four review chapters Test your end-to-end understanding of CoAP before moving to protocol integration
CoAP Methods & Features RESTful GET, POST, PUT, DELETE and multicast Deepen your understanding of how CoAP maps REST semantics to constrained devices
MQTT Fundamentals Pub/sub messaging over TCP with broker-based architecture Compare the request/response model of CoAP against the publish/subscribe model of MQTT to choose the right protocol for each use case
Protocol Selection Guide Decision matrices across all major IoT application protocols Apply a systematic framework to select between CoAP, MQTT, HTTP, and WebSockets for any IoT deployment scenario