Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 36 ยท Connectivity Protocols

HTTP, CoAP and Choosing an IoT Protocol

Learn how devices use HTTP REST, TCP and UDP, and how CoAP shrinks requests to a few bytes, with message overhead simulations and a protocol decision guide.

A radio link only carries bits. To exchange meaningful messages, devices need application protocols: agreed formats for "here is my temperature" and "turn the light on". This lesson starts with the protocol every developer knows, HTTP, shows where it hurts on constrained devices, then introduces CoAP, which was designed for exactly those devices. It closes with a guide to choose among the options, including the publish/subscribe protocols that later lessons cover in depth.

The protocol stack in brief

IoT messages sit on top of a stack. From bottom to top: the link (Wi-Fi, BLE, LoRa, cellular), the network layer (IPv4, IPv6, 6LoWPAN on tiny radios), the transport (TCP or UDP), and the application protocol (HTTP, CoAP, MQTT, and others).

  • TCP gives a reliable, ordered byte stream. It needs a three-way handshake, keeps connection state and retransmits; this costs time and energy, especially with TLS on top.
  • UDP sends independent datagrams with no connection setup. It is light and fast, but nothing is guaranteed unless the application adds it.
flowchart TB
    A["Application: HTTP / CoAP / MQTT / LwM2M"]
    S["Security: TLS (over TCP) or DTLS (over UDP)"]
    T["Transport: TCP or UDP"]
    N["Network: IPv4 / IPv6 / 6LoWPAN"]
    L["Link: Wi-Fi / BLE / 802.15.4 / LoRaWAN / cellular"]
    A --> S --> T --> N --> L

Each layer wraps the one above it, adding its own header bytes.

HTTP and REST on devices

HTTP works everywhere: every language, proxy, load balancer and cloud has it. A device can post readings with a simple request:

TEXT
POST /api/v1/readings HTTP/1.1
Host: iot.example.com
Content-Type: application/json
Authorization: Bearer <token>
Content-Length: 31

{"sensor":"kitchen","temp":21.4}

REST maps naturally onto things: GET /devices/42/temperature reads, PUT /devices/42/led with {"on":true} commands. For mains-powered devices that report occasionally, plain HTTPS is perfectly fine and very easy to debug and secure.

The costs show on constrained, battery or low-bandwidth devices:

  • Text headers repeated in every request (hundreds of bytes).
  • TCP + TLS handshake for each short message unless you keep connections open.
  • Request/response only: the server cannot push a command to a device unless the device keeps polling or holds a connection open.
  • Large libraries and RAM use for TLS.
Python How much of an HTTP message is overhead?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The 10-byte reading travels inside messages roughly 28 times its size. Over a metered cellular plan, this adds up.

CoAP: the web for constrained devices

The Constrained Application Protocol (CoAP, RFC 7252) keeps the REST model (GET, POST, PUT, DELETE on URIs with response codes) but is compact and runs over UDP. Features:

  • A 4-byte fixed header, then options and payload: a small request fits in a handful of bytes.
  • Message types: Confirmable (CON, retransmitted until acknowledged), Non-confirmable (NON, fire and forget), ACK and RST.
  • Observe (RFC 7641): a client subscribes to a resource and the server pushes updates, a lightweight publish/subscribe.
  • Block-wise transfer for larger data and firmware pieces.
  • DTLS (or OSCORE) for security.
  • Resource discovery at /.well-known/core.
  • Proxying and mapping to HTTP, so a gateway can translate between CoAP devices and web clients.
  • Response codes like 2.05 Content, 4.04 Not Found correspond to HTTP's 200 and 404.

The CoAP header

The first four bytes hold: version (2 bits), type (2 bits), token length (4 bits), code (8 bits: class and detail), and message ID (16 bits). The next simulation builds a real CoAP GET request.

Python Building and decoding a CoAP GET request

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The CoAP request needs only about 18 bytes where HTTP needs over 70, before counting TCP and TLS savings.

Reliability without TCP

CoAP adds simple reliability on UDP. A confirmable message is retransmitted with exponential backoff until an ACK arrives or attempts run out. The simulation shows the timing:

Python CoAP confirmable retransmission with backoff

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Other protocols you will meet

Protocol Style Notes
MQTT Publish/subscribe over TCP Dominant for cloud messaging; the next chapter covers it
AMQP Message queuing Enterprise brokers, reliable delivery
LwM2M Device management over CoAP Firmware, configuration, object model
WebSocket Full-duplex over TCP Browsers and dashboards
Modbus Industrial request/response PLCs, meters
OPC UA Industrial information model Factory automation
DDS Data-centric pub/sub Robotics, real-time systems
Matter Application layer for smart homes Runs over Wi-Fi and Thread

Data formats on the wire

Protocols carry payloads, and the payload format matters as much as the protocol:

  • JSON: readable, universal, bulky.
  • CBOR: binary JSON; compact and standard in IoT (used by CoAP and LwM2M).
  • Protocol Buffers, MessagePack: compact binary with schemas or generic.
  • Custom packed bytes: smallest, used on LoRa, but needs documentation and versioning.
Python The same reading in four formats

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Choosing a protocol

Ask these questions:

  1. Who initiates? Device reports upward only (HTTP POST, CoAP, MQTT) or the cloud must command devices (MQTT, CoAP Observe, LwM2M)?
  2. How constrained is the device? Tiny RAM and battery favor CoAP over UDP or MQTT-SN; comfortable devices can use HTTPS or MQTT.
  3. How many devices and how often? Thousands of persistent connections favor a broker such as MQTT.
  4. Is the network lossy or sleepy? Pick reliability features to match.
  5. What does your platform support? Cloud IoT services speak MQTT and HTTPS almost universally.
  6. Security needs: TLS or DTLS support, certificates and keys on the device.
Python A rule-based protocol picker

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

There is no single right answer. Many systems combine several: CoAP or BLE at the edge, an MQTT broker in the middle and HTTPS APIs for apps.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is plain HTTP wasteful for tiny sensor readings?

    Headers and handshakes may be tens of times larger than the few payload bytes.
  2. 2. What transport does CoAP normally use?

    CoAP runs over UDP and adds its own optional reliability with confirmable messages.
  3. 3. What does CoAP Observe provide?

    Observe lets a client register interest and receive notifications when a resource changes.
  4. 4. What is CBOR?

    Concise Binary Object Representation encodes JSON-like data in far fewer bytes.

Summary

  • Application protocols ride on TCP or UDP over IP (or 6LoWPAN) and need their own security (TLS or DTLS).
  • HTTP/REST is simple and universal but heavy for constrained devices; CoAP gives REST semantics over UDP with a 4-byte header, Observe and optional reliability.
  • Payload format matters: JSON for convenience, CBOR or packed binary for efficiency.
  • Choose by direction of communication, device constraints, scale, network and platform support.

Next: MQTT: The Publish/Subscribe Model