πŸ“‘ IoT Engineering β€” MQTT, CoAP, LwM2M & Communication Protocols

Vault punya edge-computing-iot-security-architecture yang fokus ke security arsitektur IoT, dan embedded-systems untuk sistem embedded secara umum. Tapi belum ada yang bedah communication protocols yang menjadi tulang punggung IoT: bagaimana device dengan resource terbatas (baterai, CPU, bandwidth) bisa berkomunikasi secara efisien dan reliable. Catatan ini mencakup MQTT, CoAP, LwM2M, LPWAN, mesh protocols, dan serialization β€” dari teori sampai implementasi.

Posisi di Vault

Ini adalah protocol layer IoT yang melengkapi edge-computing-iot-security-architecture (security) dan embedded-systems (hardware). Juga terhubung dengan networking-fundamentals-tcpip-bgp (network layer), api-protocols-deepdive (API protocols secara umum), dan wireless-security-deepdive (wireless security).


Daftar Isi


1. IoT Architecture Overview

1.1 Four-Layer Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  APPLICATION LAYER                                β”‚
β”‚  Data analytics, dashboard, ML inference          β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  MIDDLEWARE LAYER                                 β”‚
β”‚  Message broker, device registry, rule engine     β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  NETWORK / PROTOCOL LAYER                         β”‚
β”‚  MQTT, CoAP, HTTP, LwM2M, LoRaWAN                β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  PERCEPTION / DEVICE LAYER                        β”‚
β”‚  Sensors, actuators, microcontroller, radio       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

1.2 IoT Protocol Stack vs Internet Stack

LayerInternet StackIoT Stack
ApplicationHTTP, WebSocketMQTT, CoAP, LwM2M, gRPC
TransportTCP, UDPTCP, UDP, QUIC
NetworkIPv4, IPv6IPv6, 6LoWPAN, RPL
LinkEthernet, Wi-FiLoRa, NB-IoT, BLE, Zigbee
PhysicalCopper, FiberRF 868/915MHz, 2.4GHz, Sub-GHz

1.3 Protocol Selection Matrix

ProtocolTransportModelQoSOverheadPowerUse Case
MQTTTCPPub/Sub3 levelsMinimalMediumSensor telemetry, push notif
CoAPUDPReq/Response + ObserveConfirmableMinimalLowConstrained device REST
HTTP/2TCPReq/ResponseStream priorityTinggiHighGateway-to-cloud
gRPCHTTP/2StreamingBidirectionalSedangHighHigh-throughput IoT
WebSocketTCPFull-duplexNoneSedangHighReal-time UI

2. MQTT β€” Message Queue Telemetry Transport

2.1 MQTT Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚Publisher│───→│  Broker  │───→│Subscriberβ”‚
β”‚(sensor) β”‚    β”‚ (Mosquitto) β”‚    β”‚ (app)   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                   β”‚
              β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”
              β”‚Subscriberβ”‚
              β”‚ (logger) β”‚
              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

MQTT menggunakan publish/subscribe pattern β€” publisher gak tahu siapa subscriber, subscriber gak tahu siapa publisher. Semua routing dilakukan oleh broker berdasarkan topic hierarchy.

2.2 Topic & Wildcard

Topic: home/kitchen/temperature
         ↑     ↑       ↑
      domain  room  measurement
 
Wildcards:
  + (single level):  home/+/temperature  β†’ home/kitchen/temperature, home/bedroom/temperature
  # (multi level):   home/#              β†’ home/kitchen/temperature, home/garage/door/status
 
Example topics:
  sensor/temperature/room-01
  sensor/humidity/room-01
  actuator/light/room-01/set     ← command topic
  actuator/light/room-01/status  ← status response
  system/alert/+                  ← all alert topics

2.3 QoS Levels

QoSDelivery GuaranteePacketUse Case
0At most oncePUBLISH (fire & forget)Sensor read non-kritis (temp)
1At least oncePUBLISH + PUBACKMust deliver, duplicate OK
2Exactly oncePUBLISH + PUBREC + PUBREL + PUBCOMPPayment, critical command
# QoS impact on throughput (test with Mosquitto)
mosquitto_pub -t test -m "hello" -q 0  # ~50,000 msg/sec
mosquitto_pub -t test -m "hello" -q 1  # ~15,000 msg/sec
mosquitto_pub -t test -m "hello" -q 2  # ~5,000 msg/sec

2.4 MQTT 5.0 Improvements

FeatureMQTT 3.1.1MQTT 5.0
Session expiryClean session onlySession expiry interval
Reason codesNoneFull reason codes in ACK
User propertiesNoneCustom metadata in header
Topic aliasNoneReduce topic size
Flow controlNoneReceive maximum
Server disconnect reasonNoneReason string

2.5 MQTT Broker Comparison

BrokerLanguageMax ConnectionsClusteringProtocols
MosquittoC~100K❌MQTT 5.0, WebSocket
EMQXErlang10M+βœ… NativeMQTT, CoAP, WebSocket, Stomp
VerneMQErlang~1Mβœ… NativeMQTT 3.1.1/5.0
NanoMQC~1Mβœ… via EMQXMQTT 5.0, ZeroMQ, WebSocket
HiveMQJava~500Kβœ…MQTT 5.0, Sparkplug

2.6 Practical MQTT with ESP32

// ESP32 + MQTT β€” sensor publish
#include <WiFi.h>
#include <PubSubClient.h>
 
WiFiClient espClient;
PubSubClient client(espClient);
 
void setup() {
    client.setServer("mqtt.example.com", 1883);
    client.setCallback(callback);
}
 
void loop() {
    if (!client.connected()) reconnect();
    client.loop();
 
    float temp = readTemperature();
    char msg[50];
    snprintf(msg, 50, "{"temp": %.2f, "hum": %.2f}", temp, readHumidity());
 
    // QoS 1 β€” at least once, sufficient for sensor data
    client.publish("sensor/temperature/living-room", msg, true);  // retain = true
    delay(30000);  // report every 30s
}

3. CoAP β€” Constrained Application Protocol

3.1 CoAP vs HTTP

CoAP adalah REST-like protocol untuk constrained device β€” mirip HTTP tapi di atas UDP, dengan overhead minimal.

HTTP:  GET /temperature β†’ TCP + TLS β†’ Header ~800 bytes
CoAP: GET /temperature β†’ UDP + DTLS β†’ Header ~4 bytes

CoAP Message Format:
  0                   1                   2                   3
  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
 β”‚Verβ”‚Tβ”‚  TKL  β”‚      Code     β”‚          Message ID             β”‚
 β”œβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”€
 β”‚   Token (if any, TKL bytes) ...                                β”‚
 β”œβ”€ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
 β”‚   Options (if any) ...                                         β”‚
 β”œβ”€ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
 β”‚1 1 1 1 1 1 1 1β”‚    Payload (if any) ...                       β”‚
 β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

3.2 CoAP Methods vs HTTP

HTTPCoAPBehavior
GETGETRetrieve resource
POSTPOSTCreate/update
PUTPUTUpdate resource
DELETEDELETEDelete resource
-OBSERVESubscribe to resource changes
-FETCHRetrieve with request body
-PATCHPartial update

3.3 Confirmable vs Non-Confirmable

CON (Confirmable):
  Request β†’ ACK timeout β†’ retransmit (max 4Γ—)
  Exponential backoff: 2s, 4s, 8s, 16s
  β†’ Reliable delivery, but higher latency
 
NON (Non-Confirmable):
  Fire-and-forget β€” no ACK expected
  β†’ Unreliable, but low latency & power
 
RST (Reset):
  Receiver doesn't understand message β†’ sender stop retransmit

3.4 Observe Pattern (CoAP Subscription)

Client                                  Server
  β”‚                                       β”‚
  β”œβ”€β”€β”€β”€ GET /temperature (Observe=0) ────→│
  │←──── 2.05 Content (token=0x42) ──────
  β”‚                                       β”‚
  β”‚         (temperature changes)         β”‚
  │←──── 2.05 Content (token=0x42) ──────
  β”‚                                       β”‚
  β”‚         (another change)              β”‚
  │←──── 2.05 Content (token=0x42) ──────
  β”‚                                       β”‚
  β”œβ”€β”€β”€β”€ GET /temperature (Observe=1) ────→│  ← cancel observation
  │←──── 2.05 Content                     β”‚

4. LwM2M β€” Lightweight Machine to Machine

4.1 LwM2M Architecture

LwM2M adalah protocol device management di atas CoAP β€” bukan cuma komunikasi data, tapi juga remote management:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      CoAP/DTLS       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   LwM2M Client   │◄───────────────────►│  LwM2M Server    β”‚
β”‚   (sensor, mote) β”‚                      β”‚  (cloud/backend) β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β–²                                         β–²
       β”‚ Bootstrap                               β”‚
       β–Ό                                         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Bootstrap Server β”‚                      β”‚  Smartphone App  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

4.2 LwM2M Interfaces

InterfaceFungsiContoh
BootstrapInitial config (server URL, credentials)First-time device setup
Device RegistrationRegister ke LwM2M serverRegister /rd
Device Management & Service EnablementRead/Write/Execute resourcesReboot, firmware update
Information ReportingObserve resource changesSensor value notification

4.3 Object/Resource Model

LwM2M Object = kumpulan resources yang mewakili satu fungsi
LwM2M Resource = parameter individual dalam object

Standard Objects (di define IPSO Alliance):
  /0: LwM2M Security      β†’ Server URI, credentials
  /1: LwM2M Server        β†’ Lifetime, binding, notification
  /2: Access Control      β†’ ACL per object
  /3: Device              β†’ Manufacturer, model, firmware version
  /4: Connectivity Monitoring β†’ Network bearer, signal strength
  /5: Firmware Update     β†’ Package URI, update state, result
  /6: Location            β†’ Latitude, longitude, altitude
  /3303: Temperature      β†’ Sensor value, units, range

Example:
  /3303/0/5700  β†’ Temperature sensor 0, resource 5700 (sensor value)
  /3/0/0        β†’ Device object 0, resource 0 (manufacturer)
  /5/0/1        β†’ Firmware Update, resource 1 (package URI)

4.4 Firmware Update (LwM2M OTA)

Step 1: Server write firmware URI ke /5/0/1 (Package URI)
Step 2: Client download firmware (CoAP block-wise transfer)
Step 3: Client update state: /5/0/3 (Update State) β†’ Downloading β†’ Downloaded β†’ Updating
Step 4: Client reboot dengan firmware baru
Step 5: Client register ulang β€” cek update result /5/0/5

5. LPWAN β€” LoRaWAN, NB-IoT, LTE-M

5.1 LPWAN Comparison

TeknologiFrequencyRangeBandwidthBatteryCost Module
LoRaWANSub-GHz (868/915 MHz)2-15 km0.3-50 kbps10+ years$2-5
NB-IoTLTE licensed1-10 km200 kbps5+ years$5-10
LTE-MLTE licensed1-10 km1 Mbps3+ years$8-15

5.2 LoRaWAN Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”    LoRa RF     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    IP Backhaul    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Node │◄──────────────►│ Gateway  │◄─────────────────►│ Network   β”‚
β”‚ (end β”‚                 β”‚ (concentrator) β”‚              β”‚ Server    β”‚
β”‚ device)                β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                                            β”‚
                                                      β”Œβ”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”
                                                      β”‚ App Server β”‚
                                                      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ”— Koneksi ke Catatan Lain

CatatanKoneksi
edge-computing-iot-security-architectureIoT security architecture
embedded-systemsSistem embedded untuk IoT
networking-fundamentals-tcpip-bgpNetwork layer fundamentals
api-protocols-deepdiveAPI protocol comparison
wireless-security-deepdiveWireless communication security
cloud-infrastructureIoT cloud backend

βœ… Checklist

  • Paham perbedaan MQTT, CoAP, LwM2M
  • Bisa setup MQTT broker (Mosquitto/EMQX)
  • Paham QoS levels MQTT dan trade-off masing-masing
  • Tau kapan pake CON vs NON di CoAP
  • Paham LwM2M object/resource model untuk device management
  • Bisa bedain LoRaWAN vs NB-IoT vs LTE-M
  • Tau protocol serialization yang cocok untuk constrained device
  • Paham DTLS vs TLS untuk IoT security

Roadmap Belajar

HARI 1: MQTT
  - Setup Mosquitto broker di Docker
  - Publish/subscribe dengan mosquitto_pub/sub
  - Test QoS 0, 1, 2 β€” lihat perbedaan throughput

HARI 2: CoAP
  - Test CoAP dengan libcoap / coap-client
  - Bandingkan latency CoAP vs HTTP
  - Setup Observe pattern

HARI 3: LwM2M
  - Deploy Leshan (LwM2M server open source)
  - Simulasi device registration + firmware update
  - Test Bootstrap sequence

HARI 4: LPWAN & Mesh
  - Baca spec LoRaWAN 1.1
  - Setup ChirpStack (LoRaWAN network server)
  - Bandingkan Thread vs Zigbee untuk mesh

HARI 5: Security & Production
  - Setup DTLS antara CoAP client dan server
  - Test OSCORE untuk end-to-end encryption
  - Audit security IoT architecture

Bottom Line

IoT protocols bukan cuma soal β€œpilih yang paling ringan”. MQTT bagus untuk sensor telemetry dengan broker, CoAP lebih cocok untuk device-to-device langsung tanpa infrastruktur, LwM2M essential kalau butuh remote device management. Jangan lupa DTLS untuk CoAP, TLS untuk MQTT β€” IoT tanpa enkripsi = botnet menunggu waktu.

Lanjutan

Catatan ini fokus ke communication protocols. Untuk security IoT, baca edge-computing-iot-security-architecture. Untuk embedded implementation, baca embedded-systems. Untuk wireless security, baca wireless-security-deepdive.