1 of 49

Internet of Things

Lecture 5 - CoAP & MQTT

2 of 49

CoAP

Constrained Application Protocol

3 of 49

CoAP:The Web of Things Protocol

  • IETF CORE WG => RFC 7252
  • Resource-constrained devices
  • Lightweight
  • Similar to HTTP
    • GET, POST, PUT, DELETE
  • Request-response
  • Proxy translates CoAP <-> HTTP

3

Source: https://www.iab.org/wp-content/IAB-uploads/2011/04/Shelby.pdf

Lecture 5 - CoAP & MQTT

4 of 49

When to use CoAP?

  • Resource-constrained, battery-powered devices
    • Not enough resources to run HTTP & TLS

  • CoAP is
    • energy efficient & stateless
    • low communication overhead
  • Over UDP to reduce communication overhead & resource consumption
  • Uses DTLS to secure traffic (more lightweight than TLS)

4

Lecture 5 - CoAP & MQTT

5 of 49

CoAP Design Requirements

5

Source: https://www.iab.org/wp-content/IAB-uploads/2011/04/Shelby.pdf

Lecture 5 - CoAP & MQTT

6 of 49

CoAP Design Requirements

  1. Run on devices with limited Flash and RAM
  2. Run in constrained networks (LLN)
    • low-power devices
    • wireless networks with packet loss
  3. Duty Cycle
    • nodes sleep most of the time
    • are not always available to communicate

6

7 of 49

CoAP Design Requirements

  1. Message caching on proxy
  2. Resource manipulation
  3. Subscription & notify
  4. Proxy translates CoAP <-> HTTP
  5. Resource discovery
  6. Support for multicast

7

8 of 49

CoAP Design Requirements

  1. Run over UDP - stateless
  2. Reliability - packet retransmission
  3. Low latency
  4. Support for MIME types (text, image, audio, video, etc.)
  5. Remote network management

8

9 of 49

What CoAP is (and is not)

  • CoAP is
    • REST protocol
    • Ideal for constrained devices
    • IoT/M2M applications
    • Easy to translate to/from HTTP
  • CoAP is NOT
    • a replacement for HTTP
    • a HTTP compression
    • restricted to isolated “automation” networks

9

Lecture 5 - CoAP & MQTT

10 of 49

CoAP Features

  • Web transfer protocol
    • Can be used in the Internet
  • Similar to HTTP
    • URI
    • content-type support
    • GET, POST, PUT, DELETE
    • MIME
    • response codes

10

Lecture 5 - CoAP & MQTT

11 of 49

CoAP Features

  • Low overhead
    • 4 bytes header
  • More efficient than HTTP
  • Asynchronous transaction model
  • UDP (TCP, SMS)
  • DTLS: authentication & encryption
  • Proxy: translate and cache

11

Lecture 5 - CoAP & MQTT

12 of 49

CoAP Features

  • Device management
  • Discover devices
  • Discover resources

  • Resource observation
    • client subscribes to a type of data
  • Block transfers - fragmentation
  • Unicast, multicast

12

Lecture 5 - CoAP & MQTT

13 of 49

Transaction Model

  • Base Messaging
    • simple message exchange
    • Confirmable, Non-Confirmable messages
    • Acknowledgement, Reset
  • REST Semantics
    • REST Request/Response
    • methods, response codes, options

13

Lecture 5 - CoAP & MQTT

Method

CRUD

Action

GET

read

returns requested data

POST

create

creates a new record

PUT

update

updates an existing record

DELETE

delete

deletes an existing record

14 of 49

CoAP Message Header (4 bytes)

14

Ver: CoAP version - 2 bits

T: message type - 2 bits

TKL: Token Length - 4 bits

Code: method or response code - 8 bits

Message ID: 16 bits

Token: 0-8 bytes

Message type:

  • confirmable (0)
  • non-confirmable (1)
  • ack (2)
  • reset (3)

15 of 49

CoAP Message Header (4 bytes)

  • Response code (8 bits)
    • type of request (get, post, put, delete)
    • type of response (success, error)
  • Message ID (16 bits)
    • match between Confirmable msgs and Ack/Reset
    • detect duplicate messages
  • Token (8 bits)
    • match between request and response in some cases

15

16 of 49

Confirmable Request Example

16

Confirmable Request

Piggy-backed Response

Source: https://www.iab.org/wp-content/IAB-uploads/2011/04/Shelby.pdf

Message ID

Response code

Type

Lecture 5 - CoAP & MQTT

IoT node

17 of 49

Non-confirmable Request Example

17

Source: https://www.iab.org/wp-content/IAB-uploads/2011/04/Shelby.pdf

Message ID

Type

Lecture 5 - CoAP & MQTT

18 of 49

Dealing with Packet Loss

18

Source: https://www.iab.org/wp-content/IAB-uploads/2011/04/Shelby.pdf

Message ID

Type

Response code

Lecture 5 - CoAP & MQTT

19 of 49

Separate Response

19

Source: https://www.iab.org/wp-content/IAB-uploads/2011/04/Shelby.pdf

Same token

Lecture 5 - CoAP & MQTT

Message ID

Type

20 of 49

Bits and bytes...

20

21 of 49

CoAP - Caching Model

  • Store data on other devices
  • Data freshness model
    • Max-Age option = cache lifetime
    • data expires after this period
  • A proxy performs caching on behalf of constrained nodes
    • let nodes sleep
    • reduce network load, energy consumption

21

Lecture 5 - CoAP & MQTT

22 of 49

Proxying and caching

22

Source: https://www.iab.org/wp-content/IAB-uploads/2011/04/Shelby.pdf

23 of 49

RFC 7641: Observing Resources (Subscription)

23

Source: https://www.iab.org/wp-content/IAB-uploads/2011/04/Shelby.pdf

24 of 49

RFC 7959: Block-wise transfer

24

Source: https://www.iab.org/wp-content/IAB-uploads/2011/04/Shelby.pdf

25 of 49

Getting Started with CoAP

  • Open source implementations
    • C libraries: libCoAP, Erbium, microcoap, nanoCoAP, etc
    • Java: Californium, jCoAP, java-coap, nCoap, etc.
    • Python: aiocoap, CoAPthon, etc.
    • Go, Rust, JavaScript, C#, etc.
    • mbed OS, TinyOS, Contiki OS, NuttX OS, etc.

25

Lecture 5 - CoAP & MQTT

26 of 49

Getting Started with CoAP

  • Copper plugin for Firefox & Chrome
  • Wireshark has CoAP dissector support
  • Part of commercial products/systems
    • ARM Sensinode NanoService
    • RTX 4100 WiFi Module

26

Lecture 5 - CoAP & MQTT

27 of 49

MQTT

Message Queueing Telemetry Transport

28 of 49

MQTT

  • Used in IoT and M2M
  • Dr. Andy Stanford-Clark (IBM) and Arlen Nipper (Eurotech) in 1999
  • OASIS standard in 2013, ISO/IEC 20922
  • Public and royalty-free license
  • Used in Cloud apps and web services
    • Amazon Web Services, IBM WebSphere MQ
    • Microsoft Azure IoT, Adafruit, Facebook Messenger, etc.

28

Lecture 5 - CoAP & MQTT

29 of 49

MQTT Features

  • Small code footprint
  • Ideal for resource constrained devices
  • Ideal for networks with low bandwidth and packet loss

  • Publish/subscribe pattern
    • publishers
    • subscribers
    • broker

29

Lecture 5 - CoAP & MQTT

30 of 49

MQTT Features

  • Works on top of TCP/IP
    • usually over TCP
    • can run over UDP

  • 3 Quality of Service levels:
    • at most once
    • at least once
    • exactly once

30

Lecture 5 - CoAP & MQTT

31 of 49

MQTT Features

  • Security
    • user authentication - username/password
    • integrate with SSL/TLS for authentication & encryption

  • Support for persistent messages stored on the broker
    • similar to caching in CoAP

31

Lecture 5 - CoAP & MQTT

32 of 49

MQTT Features

  • Client libraries
    • C, C++, C#
    • Java, JavaScript
    • Android
    • Arduino
    • NuttX OS

32

Lecture 5 - CoAP & MQTT

33 of 49

Publish/Subscribe

  • Multiple clients connect to the broker and publish data to topics
  • Multiple clients connect to the broker and subscribe to topics

33

Image source: https://www.paessler.com/it-explained/mqtt

Lecture 5 - CoAP & MQTT

34 of 49

Publish/Subscribe

34

Image source: https://www.twilio.com/en-us/blog/what-is-mqtt

35 of 49

Topics

  • Topics are treated as a hierarchy
    • using a slash (/) as a separator

    • multiple sensor devices publish temperature
      • sensors/DEVICE_NAME/temperature

35

Lecture 5 - CoAP & MQTT

36 of 49

Topics - Wildcards

  • Subscription to an explicit topic or a topic that includes wildcards
    • “+” matches a single sublevel

    • “#” matches all sublevels

    • Example: office/room1/+/temperature
      • + = device name
      • all temperature data from devices in room 1

36

Lecture 5 - CoAP & MQTT

37 of 49

Actions in MQTT

  • Actions: publish, subscribe, ping, disconnect
  • Publish:
    • sends data to broker on a certain topic
    • topic created if it does not exist
  • Subscribe:
    • client subscribes to a certain topic
    • broker sends SUBACK response & maybe data
    • receive data when it becomes available

37

Lecture 5 - CoAP & MQTT

38 of 49

Actions in MQTT

  • Ping:
    • used by the client
    • PINGREQ & PINGRESP messages
    • ensure that the connection is still working
  • Disconnect
    • publishers & subscribers may disconnect from broker
    • client cannot send or receive data

38

Lecture 5 - CoAP & MQTT

39 of 49

QoS Levels

  • 3 levels - choice between minimizing data transmission and maximizing reliability
  • QoS 0 = “At most once”
    • minimizing data transmission
    • send only once, no Ack expected
    • best effort
    • data is not stored
    • when links are reliable, number of connections is limited

39

Lecture 5 - CoAP & MQTT

Image source: https://emqx.medium.com/introduction-to-mqtt-5-0-protocol-qos-quality-of-service-e6d9b0aaf9fb

40 of 49

QoS Levels

  • QoS 1 = “At least once”
    • expect PUBACK
    • retransmitted if PUBACK is not received within a period of time
    • messages stored until PUBACK is received
    • subscriber may receive duplicate messages
    • compromise between QoS 0 and 2

40

Lecture 5 - CoAP & MQTT

Image source: https://emqx.medium.com/introduction-to-mqtt-5-0-protocol-qos-quality-of-service-e6d9b0aaf9fb

41 of 49

QoS Levels

  • QoS 2 -> “Exactly once”
    • 2-step confirmation process
    • 4 types of MQTT messages
      • Publish, Publish received, Publish release, Publish complete
    • messages stored until PUBCOMP is received
    • links are not reliable, number of connections is not limited

41

Lecture 5 - CoAP & MQTT

42 of 49

MQTT Messages

42

Image source: https://www.techtarget.com/iotagenda/definition/MQTT-MQ-Telemetry-Transport

43 of 49

QoS Levels

  • QoS 1&2
    • messages are saved when destination is offline
    • retransmitted when they are online

  • Retained Messages = method of caching
    • broker keeps messages even after sending them to all subscribers
    • new subscribers get the retained messages

43

Lecture 5 - CoAP & MQTT

44 of 49

MQTT Options

  • Clean Sessions
    • clean flag
    • all subscriptions are removed on disconnect

  • Persistent Sessions
    • subscriptions remain in effect after disconnection
    • across multiple connections
    • high QoS msgs stored for delivery after reconnection

44

Lecture 5 - CoAP & MQTT

45 of 49

MQTT Options

  • Last Will Testament
    • specified by publisher in the Connect packet
    • msg to be published if unexpected disconnection
    • notify subscribers when publisher is disconnected
    • send the last collected data

45

Lecture 5 - CoAP & MQTT

https://www.hivemq.com/blog/mqtt-essentials-part-9-last-will-and-testament/

46 of 49

MQTT vs. CoAP

46

Image Source: https://www.trendmicro.com/vinfo/es/security/news/internet-of-things/mqtt-and-coap-security-and-privacy-issues-in-iot-and-iiot-communication-protocols

Lecture 5 - CoAP & MQTT

47 of 49

MQTT vs. CoAP

47

Features

MQTT

CoAP

Base protocol

TCP

UDP

Model used for communication

Publish-Subscribe

Request-Response

Publish-Subscribe (observing resources)

Communication node

many-to-many

one-to-one

Power consumption

Higher than CoAP

Lower than MQTT

RESTful

No

Yes

Number of messages type used

16

4

Header size

2 Bytes

4 Bytes

Messaging

Asynchronous

Asynchronous & Synchronous

Reliability

3 Quality of service levels

Confirmable messages

Caching

On Broker

On Proxy

Security

Username/Password

Can use TLS/SSL

Can use DTLS or IPSec

48 of 49

Bibliography

48

Lecture 5 - CoAP & MQTT

49 of 49

Bibliography

49

Lecture 5 - CoAP & MQTT