This is the learning path index for CoAP (Constrained Application Protocol), a lightweight RESTful protocol for IoT devices. The five-chapter series covers fundamentals and architecture, message types (CON/NON/ACK/RST), methods and multicast features, security with DTLS, and hands-on practice – building from basic concepts to complete implementations on constrained devices.
38.1 Learning Objectives
By the end of this series, you will be able to:
Explain CoAP’s architecture including the two-layer design (messaging + request/response) over UDP
Compare and Select CoAP message types (CON, NON, ACK, RST) appropriate for each reliability and energy scenario
Apply CoAP’s RESTful methods (GET, POST, PUT, DELETE) with resource discovery and multicast to solve IoT integration problems
Implement secure CoAP communication using DTLS encryption on constrained devices
Design and Construct CoAP applications with ESP32 and Python, justifying architectural decisions through hands-on practice
For Beginners: CoAP Overview
CoAP (Constrained Application Protocol) brings web-style communication to the world of tiny IoT sensors and devices. Just as your web browser uses HTTP to load websites, IoT devices use CoAP to exchange data – but in a much more lightweight and energy-efficient way designed for devices with limited resources.
Sensor Squad: The Lightweight Web for Sensors
“I want to share my data on the internet, but I’m just a tiny sensor with barely any memory!” said Sammy the Sensor, looking worried.
Max the Microcontroller reassured him. “That’s exactly why CoAP exists! It gives you web powers – GET, PUT, POST, DELETE – but in a tiny package. Regular HTTP is like a thick textbook, while CoAP is like a pocket notebook. Same information, fraction of the size.”
“Here’s what makes it special,” added Lila the LED. “CoAP uses UDP instead of TCP, so you skip all that handshaking overhead. And it uses binary headers instead of text – just 4 bytes compared to HTTP’s hundreds. For a sensor that sends thousands of readings a day, those saved bytes really add up!”
Bella the Battery was thrilled: “With CoAP, I can keep Sammy running for years on a single battery. HTTP would drain me in months because of all the extra overhead. CoAP is proof that good things come in small packets!”
38.2 CoAP Protocol Overview
⏱️ ~40 min total | ⭐⭐ Intermediate | 📋 P09.C28
Key Concepts
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
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
This section provides a comprehensive introduction to the Constrained Application Protocol (CoAP), a specialized web transfer protocol designed for resource-constrained IoT devices and networks.
38.3 What You’ll Learn
CoAP brings RESTful web services to constrained devices, providing HTTP-like semantics with minimal overhead. This multi-chapter series covers:
Learn the basics of CoAP and why it’s essential for IoT: - What is CoAP and why was it created? - Why CoAP instead of HTTP for IoT devices? - Understanding the 4-byte header advantage - CoAP’s two-layer architecture (messaging + request/response) - Real-world battery life comparisons
Key Topics: Protocol basics, UDP vs TCP, REST for constrained devices, architecture overview
Master CoAP’s four message types and communication patterns: - Confirmable (CON) messages for reliable delivery - Non-Confirmable (NON) for fire-and-forget efficiency - Acknowledgment (ACK) and Reset (RST) responses - Piggyback vs separate response patterns - CON vs NON battery life trade-offs
Implement CoAP securely in real-world applications: - Security with DTLS encryption - Smart energy and building automation use cases - Industrial IoT applications - Implementation patterns and best practices - Common pitfalls and troubleshooting
Apply your knowledge with hands-on practice: - Visual reference gallery (protocol diagrams) - Worked examples with calculations - Practice exercises (ESP32, multicast, proxies) - Further reading and academic resources
Key Topics: Worked examples, hands-on labs, exercises, resources
38.4 Prerequisites
Before starting this series, you should understand:
Common Mistake: Treating CoAP as “Simple HTTP over UDP”
The Misconception: Developers with HTTP experience often implement CoAP as “just replace TCP with UDP and shrink the headers,” leading to incorrect reliability assumptions and missed optimization opportunities.
What’s Wrong:
Reliability is optional, not automatic: HTTP over TCP guarantees delivery. CoAP requires explicit CON messages for reliability, but NON messages intentionally sacrifice reliability for efficiency.
Observe pattern has no HTTP equivalent: HTTP requires polling (GET /temp every 10 seconds). CoAP Observe registers once and receives push notifications only when data changes, saving 95%+ bandwidth.
Multicast discovery is native: CoAP supports GET coap://[FF02::FD]/temp to query all sensors on a subnet with one packet. HTTP requires individual requests to each known device.
Real-World Example:
A developer ported an HTTP-based sensor network to CoAP by changing:
# Old HTTP coderesponse = requests.get('http://sensor/temp')
Still polling every 10 seconds (didn’t use Observe) → no bandwidth savings
Used CON for all messages (didn’t leverage NON) → no power savings
Didn’t implement multicast discovery → no infrastructure simplification
Result: Same energy cost as HTTP, just different syntax
Correct CoAP Implementation:
# Use Observe pattern (register once, get push updates)request = Message(code=GET, uri='coap://sensor/temp', observe=0)asyncfor response in protocol.request(request).observation: process(response) # Only when temp actually changes# Use NON for frequent telemetrytelemetry = Message(code=POST, uri='coap://gateway/data', mtype=NON, payload=sensor_data)protocol.request(telemetry) # Fire-and-forget, no ACK wait# Use multicast for discoverydiscover = Message(code=GET, uri='coap://[FF02::FD]/.well-known/core')
Result: 97% bandwidth reduction, 5× battery life improvement by using CoAP’s unique features rather than treating it as HTTP-over-UDP.
38.11 Concept Relationships
How this CoAP learning path connects to broader IoT concepts:
CoAP Foundation Concepts:
UDP vs TCP Transport - Why CoAP chose connectionless delivery
RESTful Architecture - HTTP-like semantics for constrained devices
Binary vs Text Protocols - 4-byte binary header efficiency