The IoT Protocol Stack

The IoT stack is similar to the internet stack but adapted for constrained devices.

  • Application layer: MQTT, CoAP, HTTP/HTTPS, AMQP, and data formats such as JSON.
  • Transport layer: TCP (reliable, connection-oriented) or UDP (light, connectionless).
  • Network layer: IPv6 with 6LoWPAN, or a mesh routing protocol.
  • Link and physical layer: IEEE 802.15.4, Wi-Fi, Bluetooth Low Energy, LoRa, cellular.

Constrained devices prefer UDP and small headers, because TCP handshakes and large headers waste energy.

MQTT

Message Queuing Telemetry Transport (MQTT) is a lightweight publish/subscribe protocol over TCP (port 1883, or 8883 with TLS). Devices do not talk to each other directly. A publisher sends a message to a topic (for example home/livingroom/temp) at a broker, and the broker forwards it to every subscriber of that topic. Subscribers can use wildcards: + matches one level and # matches all remaining levels. It has three QoS levels: 0 (at most once), 1 (at least once) and 2 (exactly once), a retained message (the last value stored for new subscribers), and a last will message sent if a client disconnects unexpectedly. The fixed header is as small as 2 bytes.

CoAP

Constrained Application Protocol (CoAP) works like a light HTTP over UDP (port 5683), using the request/response model with GET, POST, PUT, DELETE on resources identified by URIs. It supports confirmable (acknowledged) and non-confirmable messages, an observe option for notifications, and a small 4-byte header. It maps easily to HTTP through a proxy.

HTTP/HTTPS and AMQP

HTTP/HTTPS is the web protocol (TCP, ports 80 and 443). It is universal and easy for cloud APIs (REST), but its headers are large and each request is heavy for tiny devices. AMQP (Advanced Message Queuing Protocol) is a reliable, feature-rich message-queuing protocol with exchanges, queues and routing, used between servers and in enterprise back ends rather than on tiny sensors.

Definition: MQTT. A lightweight publish/subscribe messaging protocol over TCP in which clients publish messages to topics at a broker, which delivers them to all clients subscribed to those topics.

Table 2.1: Application protocols

Protocol Model Transport Overhead Best for
MQTT Publish/subscribe TCP Very small Telemetry to cloud
CoAP Request/response UDP Very small Constrained devices
HTTP/HTTPS Request/response TCP Large Web and cloud APIs
AMQP Message queues TCP Moderate Enterprise back ends

Example 2.1: Overhead of MQTT and HTTP

Problem: A sensor sends a 10-byte reading once a minute. Assume an MQTT message adds 10 bytes of header and topic, and an HTTP request adds 250 bytes of headers (assumed typical values). Find the bytes per day for each and the ratio.

Solution:

  1. Messages per day = 24 × 60 = 1,440.
  2. MQTT message = 10 + 10 = 20 bytes, so per day = 20 × 1,440 = 28,800 bytes.
  3. HTTP request = 250 + 10 = 260 bytes, so per day = 260 × 1,440 = 3,74,400 bytes.
  4. Ratio = 3,74,400 / 28,800 = 13.

Result: HTTP uses 13 times more data than MQTT, which means more energy and cost on a battery or mobile data plan.

n = 24 * 60
mqtt, http = (10 + 10) * n, (250 + 10) * n
print("MQTT bytes/day:", mqtt, "| HTTP bytes/day:", http)
print("Ratio:", http / mqtt)

def match(filt, topic):
    f, t = filt.split("/"), topic.split("/")
    for i, p in enumerate(f):
        if p == "#":
            return True
        if i >= len(t) or (p != "+" and p != t[i]):
            return False
    return len(f) == len(t)

print(match("home/+/temp", "home/hall/temp"))
print(match("home/#", "home/hall/light/on"))
print(match("home/+/temp", "home/hall/hum"))

Output

MQTT bytes/day: 28800 | HTTP bytes/day: 374400
Ratio: 13.0
True
True
False

Common mistakes

  • Using HTTP for tiny battery devices when MQTT or CoAP would do.
  • Thinking MQTT clients connect to each other directly.
  • Ignoring QoS and expecting guaranteed delivery at level 0.
  • Sending credentials or data without TLS.

Key takeaways

  • MQTT: publish/subscribe over TCP with a broker; QoS 0, 1, 2; wildcards + and #.
  • CoAP: REST-like over UDP; small header.
  • HTTP: heavy but universal; AMQP: queues for back ends.
  • Prefer small headers and UDP for constrained devices.

Practice questions

  1. What are the three MQTT QoS levels?
  2. Which transport does CoAP use?
  3. What does the MQTT wildcard + match?

Answers to the practice questions

  1. 0 at most once, 1 at least once, 2 exactly once.
  2. UDP.
  3. Exactly one topic level.

Sources

  • Official BCA syllabus Internet of Things, course code 25BCA406DS01, Unit II