Marcio Cunha

How the CoAP Protocol Enables REST Requests over UDP in IoT

Discover how the CoAP protocol adapts the REST architecture for constrained devices and sensor networks using UDP, ensuring efficiency without losing web compatibility.

Marcio Cunha5 min
Also available in:EspañolPortuguês
Summary
  • The CoAP protocol replicates the simplicity of traditional web REST models for microcontrollers with minimal memory.
  • Using UDP as the transport foundation significantly reduces battery consumption and network traffic compared to TCP.
  • The CoAP messaging layer adds lightweight reliability without the operational overhead of a full TCP connection.
  • Direct mapping to HTTP facilitates the seamless integration of sensor data with cloud systems and existing APIs.
  • Automatic resource discovery allows IoT devices to autonomously advertise services on the local network.

The Connectivity Challenge in Constrained Devices

Connecting tiny sensors, smart lights, and energy meters to the internet requires engineering choices vastly different from those made when building traditional web applications. While a cloud server boasts gigabytes of memory and a constant electrical connection, an Internet of Things (IoT) device often runs on battery power, uses 8-bit or 32-bit processors with limited RAM, and operates over unstable wireless links. In practice, this means that using standard web protocols like HTTP over TCP creates an insurmountable waste of energy and bandwidth resources.

To understand the problem, we must examine the TCP protocol, the foundation that guarantees every data packet arrives intact and in order for a standard HTTP request. TCP requires initial handshake messages to establish a connection, maintains constant state memory on the device, and performs complex automatic retransmissions during packet loss. In low-power sensor networks, known as LLNs (Low-Power and Lossy Networks), this model creates unsustainable overhead. The microcontroller radio must remain powered on simply to keep the TCP control conversation alive, draining the battery rapidly.

The REST Architecture Adapted for the Physical World

The REST (Representational State Transfer) architecture, which underpins most modern web APIs, relies on simple verbs like GET, POST, PUT, and DELETE to manipulate resources identified by URLs. This approach is extremely intuitive and simplifies system integration. The great merit of the CoAP (Constrained Application Protocol) protocol, standardized by the IETF via RFC 7252, was bringing precisely this request-response model to the constrained hardware ecosystem, allowing microcontrollers to speak the same language as web servers without carrying the heavy footprint of HTTP.

In CoAP, every sensor or actuator exposes its data as resources accessible via URIs, exactly like a conventional web server. A thermometer can make its current temperature available at the path /sensor/temperature. Any external application can query this value by sending a request that mirrors the behavior of the HTTP GET method. The brilliant design choice in CoAP is that this conceptual translation is so direct that network gateways can convert CoAP requests into standard HTTP calls transparently, enabling end-to-end integration without complex backend rewrites.

Why UDP Becomes the Ideal Choice for Sensor Networks

Unlike TCP, the User Datagram Protocol (UDP) operates in a totally decentralized, connectionless manner: the sender packages the data and transmits it across the network without negotiating a session or guaranteeing delivery through complex machinery. In network engineering, UDP is frequently criticized for being unreliable, but this exact characteristic makes it perfect for constrained devices. Because UDP requires no state memory control—the so-called connection handshake—the microcontroller radio can wake up, transmit a sensor reading in a single packet, and return to sleep immediately, conserving precious energy.

However, blindly trusting raw UDP in a noisy wireless network could result in the constant loss of critical data. To solve this dilemma without resorting to heavy TCP, CoAP implements its own lightweight messaging layer directly on top of UDP. It introduces the concept of confirmable messages (CON), which require a simple acknowledgment (ACK) from the recipient. If the ACK does not arrive within a predefined timeframe, the sender retransmits the message using an exponential backoff mechanism, ensuring the necessary robustness with a tiny fraction of TCP complexity.

The Compact Binary Structure of the CoAP Header

One of the greatest bottlenecks in sensor networks is the size of the data packet transmitted over the air. A standard HTTP request header can easily exceed hundreds of bytes purely with textual metadata like User-Agent, cookies, and extensive header fields. In IEEE 802.15.4 networks, common in home and industrial automation, the maximum physical packet size is a mere 127 bytes. If the application protocol consumes too much space with headers, little room remains for the actual sensor payload.

CoAP solves this issue by adopting an extremely lean binary header of just 4 fixed bytes. Fields like message type, method code (GET, POST, etc.), and transaction ID are packed into specific bits and bytes. Additional information, such as resource paths or format options, is attached using compact delta blocks known as CoAP Options. In practice, this means a complete CoAP request can comfortably fit inside a single low-power radio packet, reducing fragmentation and lowering network interface power consumption.

Resource Observation and Automatic Discovery in Practice

Beyond traditional request-response operations, CoAP introduces highly optimized mechanisms for the IoT world, such as the Resource Observation extension. Instead of performing periodic polling queries—which forces the device to constantly wake up and respond even if nothing changed—a client application can register interest in a specific resource. The sensor then pushes updates to the client only when the monitored value changes, drastically saving bandwidth and battery life.

Another foundational feature is automatic service discovery. Constrained devices frequently operate in dynamic networks where new sensors join and leave continuously without prior manual configuration. CoAP defines a standard endpoint located at /.well-known/core, where any device can query the complete list of resources offered by that node. This allows automation systems to discover new sensors on the local network instantly, interpret their metadata, and integrate them into the ecosystem without human intervention.

Final Thoughts on CoAP Adoption

The CoAP protocol proves that it is possible to translate the clean, understandable architecture of the web into the hostile, constrained environment of low-power microcontrollers. By choosing UDP as the transport layer and adding a lean, optional reliability layer, the protocol successfully balances extreme energy efficiency with the conceptual familiarity of the REST model. For engineers designing connected embedded systems, mastering CoAP paves the way for scalable, interoperable, and energy-efficient solutions in modern smart device architecture.