1218  CoAP Decision Framework: Protocol Selection Guide

1218.1 Learning Objectives

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

  • Select the Right Protocol: Choose between CoAP, MQTT, and HTTP based on requirements
  • Evaluate Trade-offs: Compare overhead, reliability, and resource consumption
  • Match to Use Cases: Apply decision matrix to real IoT deployment scenarios
  • Justify Choices: Explain protocol selection to stakeholders with data

1218.2 Prerequisites

Before diving into this chapter, you should be familiar with:

1218.3 Protocol Decision Matrix

TipMinimum Viable Understanding: Protocol Selection

Core Concept: CoAP, MQTT, and HTTP each excel in different scenarios. CoAP = lightweight request-response. MQTT = efficient pub-sub with broker. HTTP = universal web compatibility.

Why It Matters: Wrong protocol choice can cost battery life (years vs months), bandwidth ($1000s/year for cellular), or development time (reimplementing features).

Key Takeaway: Use this heuristic: Battery-critical request-response? CoAP. Multiple subscribers? MQTT. Web integration? HTTP.

1218.3.1 Quick Decision Table

Factor Use CoAP Use MQTT Use HTTP
Communication Pattern Request-response, device queries Publish-subscribe, multiple subscribers Web browser access, rich media
Device Constraints 8KB RAM, 8-bit MCU 16KB+ RAM, moderate CPU Full OS, abundant resources
Network Type LoRaWAN, NB-IoT, 802.15.4 Wi-Fi, cellular with persistent connection Broadband, Wi-Fi, LTE
Battery Life Multi-year on coin cell Months-1 year on rechargeable Mains-powered acceptable
Message Frequency Infrequent (hourly/daily) Continuous streaming On-demand
Broker Acceptable? No (direct device-to-device) Yes (centralized routing) N/A
Multicast Support Native (FF0X::FD) Not supported Not supported
Latency Requirements <100ms (no setup) <500ms (broker hop) <1s (TCP handshake)

1218.3.2 Detailed Comparison

Metric CoAP MQTT HTTP
Header Size 4 bytes 2 bytes (+ TCP overhead) 100+ bytes
Total Overhead (1 reading) 30 bytes 55 bytes (with TCP) 320 bytes
Transport UDP TCP TCP
Connection Setup None (0ms) 3-way handshake (150ms) 3-way handshake (150ms)
Energy per Message 1.5 mJ (NON), 3.0 mJ (CON) 4.5 mJ 8.0 mJ
Reliability Optional (CON/NON) QoS 0/1/2 TCP guarantees
Pub-Sub Observe (1-to-many) Native (many-to-many) Not native
Request-Response Native (RESTful) Emulated (request/response topics) Native
Resource Discovery .well-known/core Not standardized Not standardized
Multicast Yes No No
Broker Required No Yes No
ROM Footprint 3-5 KB 15-20 KB 25-50 KB
RAM Usage 1-2 KB 3-5 KB 10-20 KB

1218.4 When to Use CoAP

Strong indicators CoAP is the right choice:

1218.4.1 1. Battery-powered sensors reporting to gateway

Example: 500 soil moisture sensors reporting to central farm controller

Why CoAP: - NON messages use 50% less energy than MQTT TCP handshakes - No connection state to maintain (sleeps fully between transmissions) - Savings: 2-year battery life vs 6-month with MQTT

1218.4.2 2. Direct device-to-device communication

Example: Smart light switch controlling smart bulb (no broker)

Why CoAP: - RESTful PUT coap://bulb.local/state {"on": true} is simpler than MQTT broker setup - Works offline, no single point of failure - Lower latency (no broker hop)

1218.4.3 3. Constrained networks (LoRaWAN, NB-IoT)

Example: Remote environmental monitoring over LoRaWAN

Why CoAP: - LoRaWAN MTU = 51-242 bytes, CoAP message fits easily - MQTT minimum header + TCP often exceeds MTU

1218.4.4 4. Multicast queries

Example: โ€œAll temperature sensors, report current valueโ€

Why CoAP: - GET coap://[FF02::FD]/temperature reaches all with 1 packet - MQTT would need N separate topics

1218.5 When to Use MQTT

MQTT is better for:

1218.5.1 1. Multiple subscribers to same data

Example: Temperature sensor data to Dashboard + Analytics + Alarm system

Why MQTT: - Single publish to topic, broker distributes to all subscribers - CoAP Observe would need subscriptions from each (3x bandwidth)

1218.5.2 2. Cloud platform integration

Example: Devices connecting to AWS IoT Core, Azure IoT Hub

Why MQTT: - Native MQTT support, managed scaling, rule engines - CoAP requires custom gateway/translation

1218.5.3 3. Unreliable networks needing guaranteed delivery

Example: Mobile sensors on cellular with 20% packet loss

Why MQTT: - TCP retransmission automatic - QoS 1/2 ensures delivery - CoAP CON retries 4 times then gives up

1218.5.4 4. Persistent sessions

Example: Remote devices that receive commands while offline

Why MQTT: - Broker queues messages for disconnected clients (QoS 1+) - CoAP has no message persistence

1218.6 Real-World Decision Examples

Requirements: - 1,000 soil sensors (moisture, temperature) - Battery-powered (solar recharged monthly) - Report hourly to gateway - Occasional configuration updates

Decision: CoAP

Reasoning: 1. Battery life: NON messages = 2-year battery vs MQTT 6 months 2. Request-response pattern: Gateway queries sensors on-demand 3. No cloud: Farm gateway processes locally (no MQTT broker) 4. Multicast: Query all sensors simultaneously 5. Cost: Saves $254,000/year in cellular data (CoAP 30 bytes vs HTTP 320 bytes)

Implementation:

# Gateway queries sensors via multicast
response = await coap_client.multicast_get("coap://[FF02::FD]/moisture")

# Configure specific sensor
await coap_client.put("coap://sensor-042.local/config",
                      {"interval": 3600, "threshold": 30})

Requirements: - 200 machines with vibration sensors - Real-time dashboard (5 simultaneous users) - Cloud analytics (AWS) - Alarms to multiple recipients

Decision: MQTT

Reasoning: 1. Publish-subscribe: 1 sensor to Dashboard + Analytics + Alarms (3 subscribers) 2. Cloud integration: AWS IoT Core native MQTT support 3. Guaranteed delivery: QoS 1 ensures alarm messages reach all recipients 4. Scalability: MQTT broker handles 10,000+ concurrent connections 5. Persistent sessions: Dashboard reconnects and receives missed updates

Implementation:

# Sensor publishes once
mqtt_client.publish("factory/machine/042/vibration",
                   {"value": 2.5, "unit": "mm/s", "alarm": true},
                   qos=1)

# Multiple subscribers receive automatically
# Dashboard: subscribes to "factory/+/vibration"
# Analytics: subscribes to "factory/#"
# Alarms: subscribes to "factory/+/vibration" with alarm=true filter

Scenario: 500 delivery trucks reporting GPS every 10 seconds to cloud dashboard viewed by 5 dispatchers.

Decision: MQTT

Reasoning: - One-to-many communication (trucks to multiple dashboards) - Each truck publishes to fleet/truck-id/gps - All 5 dispatchers subscribe to fleet/+/gps - Broker handles distribution efficiently

Why not CoAP: - CoAP Observe would require 500 x 5 = 2,500 subscriptions - MQTTโ€™s broker-based fan-out is more scalable

1218.7 Multicast Advantages

1218.8 Decision Flowchart

%%{init: {'theme': 'base', 'themeVariables': { 'primaryColor': '#2C3E50', 'primaryTextColor': '#fff', 'primaryBorderColor': '#16A085', 'lineColor': '#16A085', 'secondaryColor': '#E67E22'}}}%%
flowchart TD
    Start["IoT Protocol Selection"]
    Q1{"Multiple subscribers<br/>for same data?"}
    Q2{"Battery-powered<br/>constrained device?"}
    Q3{"Need request-response<br/>(query/command)?"}
    Q4{"Broker infrastructure<br/>acceptable?"}
    Q5{"Firewall blocks UDP?"}

    CoAP["**Use CoAP**<br/>- Lightweight REST<br/>- UDP-based<br/>- Battery efficient"]
    MQTT["**Use MQTT**<br/>- Pub/sub pattern<br/>- Broker routing<br/>- Cloud integration"]
    HTTP["**Use HTTP**<br/>- Universal support<br/>- Web integration<br/>- Rich tooling"]
    CoAP_TCP["**Use CoAP/TCP**<br/>- REST semantics<br/>- Firewall friendly"]

    Start --> Q1
    Q1 -->|Yes| MQTT
    Q1 -->|No| Q2
    Q2 -->|Yes| Q3
    Q2 -->|No| Q4
    Q3 -->|Yes| Q5
    Q3 -->|No| MQTT
    Q4 -->|Yes| MQTT
    Q4 -->|No| HTTP
    Q5 -->|Yes| CoAP_TCP
    Q5 -->|No| CoAP

    style CoAP fill:#16A085,stroke:#16A085,color:#fff
    style MQTT fill:#E67E22,stroke:#D35400,color:#fff
    style HTTP fill:#3498DB,stroke:#2980B9,color:#fff
    style CoAP_TCP fill:#16A085,stroke:#2C3E50,color:#fff

1218.9 Summary

Protocol selection depends on your specific requirements:

Choose CoAP when: - Battery life is critical (multi-year on coin cell) - Direct device-to-device communication (no broker) - Constrained networks (LoRaWAN, NB-IoT) - Multicast queries needed - RESTful API semantics required

Choose MQTT when: - Multiple subscribers need same data - Cloud platform integration (AWS IoT, Azure) - Persistent sessions for offline devices - Unreliable networks (TCP handles retransmission)

Choose HTTP when: - Web browser integration needed - Rich tooling required - Resources are abundant - Standard web APIs preferred

Key metrics to compare: - Message overhead: CoAP (30 bytes) < MQTT (55 bytes) < HTTP (320 bytes) - Energy per message: CoAP NON (1.5 mJ) < CON (3.0 mJ) < MQTT (4.5 mJ) < HTTP (8.0 mJ) - RAM footprint: CoAP (1-2 KB) < MQTT (3-5 KB) < HTTP (10-20 KB)

1218.10 Whatโ€™s Next

Now that you can select the right protocol: