55  CoAP: Comprehensive Review

In 60 Seconds

This review series covers CoAP across four focused chapters: protocol fundamentals (message types, 87% bandwidth reduction vs HTTP), Observe patterns (99% energy savings vs polling), hands-on ESP32 labs, and knowledge assessment quizzes. CoAP provides RESTful semantics over UDP with a 4-byte binary header, optional reliability via CON/NON messages, and DTLS security.

55.1 Learning Objectives

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

  • Synthesize CoAP protocol knowledge across message types, observe patterns, and security mechanisms into a coherent design model
  • Evaluate CoAP vs HTTP and MQTT trade-offs and justify protocol selection for specific IoT deployment scenarios
  • Compare CON and NON message types by calculating energy consumption, reliability trade-offs, and appropriate use cases for each
  • Apply CoAP design patterns including block-wise transfers and resource discovery to constrained-device architectures
  • Implement CoAP solutions on ESP32 and Python platforms with DTLS security and Observe-based notifications
  • Calculate bandwidth, energy, and latency savings to assess CoAP’s quantitative advantages over HTTP in large-scale sensor deployments
  • 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

55.2 For Beginners: CoAP Review

This comprehensive review covers everything about CoAP (Constrained Application Protocol), the web protocol designed specifically for tiny IoT devices. If HTTP is a luxury car with all the features, CoAP is a compact city car – it does the essential job of web communication but with much less fuel (bandwidth) and engine power (processing).

55.3 Overview

The Constrained Application Protocol (CoAP) is a lightweight RESTful protocol designed for IoT devices that uses UDP instead of TCP for lower power consumption. This comprehensive review series covers protocol fundamentals, implementation techniques, and practical applications across four focused chapters.

What You Will Learn:

  • CoAP message types (CON, NON, ACK, RST) and their trade-offs
  • Protocol efficiency: 87% bandwidth reduction vs HTTP
  • Observe pattern for 99% energy savings compared to polling
  • Hands-on implementation on ESP32 and Python
  • Security with DTLS, multicast discovery, and block-wise transfers

55.4 Review Chapters

This review is organized into four focused chapters for efficient learning:

55.4.1 1. Protocol Fundamentals

CoAP Review: Protocol Fundamentals

Core protocol concepts and architecture:

  • Common misconception: “UDP is unreliable” (debunked with data)
  • Message types: CON, NON, ACK, RST with energy/reliability trade-offs
  • CoAP vs HTTP comparison: 87% bandwidth reduction, 7.5x lower latency
  • 4-byte binary header efficiency vs HTTP text headers
  • Block1 vs Block2 option usage for large transfers

Estimated Time: 25 minutes

55.4.2 2. Observe Patterns

CoAP Review: Observe Patterns

Push notifications and subscriptions:

  • Observe mechanism: registration, notifications, deregistration
  • Performance analysis: 99% energy savings vs polling
  • NAT/firewall timeout workarounds
  • Sequence number handling with wraparound
  • Conditional observe for battery optimization

Estimated Time: 25 minutes

55.4.3 3. Labs and Implementation

CoAP Review: Labs and Implementation

Hands-on development experience:

  • ESP32 CoAP server with DHT22 sensor and LED control
  • Python smart home automation server
  • Resource discovery with .well-known/core
  • Content negotiation (CBOR, JSON, plain text)
  • Wokwi simulator for interactive testing

Estimated Time: 45 minutes

55.4.4 4. Knowledge Assessment

CoAP Review: Knowledge Assessment

Test your understanding:

  • 12 fundamentals quiz questions
  • 12 scenario-based decision questions
  • 3 deep-dive understanding checks
  • Real-world protocol selection guidance
  • Energy and bandwidth calculations

Estimated Time: 30 minutes

55.5 Quick Reference

55.5.1 Message Type Selection

Message Type Energy Reliability Use Case
CON 3.0 mJ 99.99% Critical commands, alarms, configuration
NON 1.5 mJ 90-95% Frequent sensor readings, status updates

Scenario: 1,000 sensors send 10-byte readings every 5 minutes.

Message overhead: \[ \begin{align} \text{CoAP total} &= 4 + 4 + 8 + 10 = 26 \text{ bytes} \\ \text{HTTP total} &= 150 + 10 = 160 \text{ bytes} \\ \text{Overhead reduction} &= \frac{160 - 26}{160} = 83.75\% \end{align} \]

Daily bandwidth: \[ \begin{align} \text{Messages/day/sensor} &= \frac{24 \times 60}{5} = 288 \\ \text{Total messages/day} &= 1{,}000 \times 288 = 288{,}000 \\ \text{CoAP bandwidth} &= 288{,}000 \times 26 = 7.49 \text{ MB/day} \\ \text{HTTP bandwidth} &= 288{,}000 \times 160 = 46.08 \text{ MB/day} \\ \text{Annual savings} &= (46.08 - 7.49) \times 365 = 14{,}084 \text{ MB = 13.76 GB} \end{align} \]

Battery life (AA 2,500 mAh): \[ \begin{align} \text{CoAP energy/msg} &= 0.11 \text{ mAh} \\ \text{HTTP energy/msg} &= 1.25 \text{ mAh} \\ \text{Daily consumption (CoAP)} &= 288 \times 0.11 = 31.68 \text{ mAh} \\ \text{Battery life} &= \frac{2{,}500}{31.68} = 78.9 \text{ days (CoAP)} \\ \text{Battery life} &= \frac{2{,}500}{288 \times 1.25} = 6.9 \text{ days (HTTP)} \\ \text{Life extension} &= 78.9 / 6.9 = 11.4\times \end{align} \]

55.5.2 Protocol Comparison

Metric CoAP HTTP Savings
Header Size 4 bytes 100+ bytes 25x smaller
Transaction Size 22 bytes 165 bytes 87% reduction
Latency 20ms 150ms 7.5x faster
Energy per Request 0.11mAh 1.25mAh 11x less

55.6 Interactive Calculators

55.6.1 CoAP vs HTTP Bandwidth Calculator

Calculate bandwidth savings when using CoAP instead of HTTP for sensor data transmission.

55.6.2 CoAP Message Energy Calculator

Compare energy consumption between Confirmable (CON) and Non-confirmable (NON) messages.

55.6.3 Battery Life Comparison: Polling vs Observe

Compare battery life between traditional polling and CoAP Observe pattern.

55.6.4 When to Use CoAP vs MQTT

Choose CoAP Choose MQTT
Direct device-to-device Cloud integration (AWS, Azure)
RESTful API needed Publish-subscribe pattern
No broker infrastructure Multiple subscribers
Battery life critical Persistent sessions needed
Constrained networks (LoRaWAN) Reliable networks (Wi-Fi, LTE)
Quick Check: Protocol Selection

55.7 Prerequisites

Required Chapters:

Recommended Reading:

55.8 Learning Path

Recommended sequence for maximum learning:

  1. Start with Protocol Fundamentals for core concepts
  2. Continue to Observe Patterns for push notifications
  3. Practice with Labs and Implementation for hands-on experience
  4. Validate with Knowledge Assessment to test understanding

Total Estimated Time: 2 hours 5 minutes

55.9 See Also

Related Topics:

Hands-On Learning:

Deployment Guidance:

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.

55.10 Further Reading

55.11 Knowledge Check