0% found this document useful (0 votes)
54 views27 pages

An Iot Oriented Comparison: Rest and MQTT Rest and MQTT

REST and MQTT are protocols for IoT communication. REST leverages HTTP to provide data services using verbs like GET, PUT, POST, and DELETE. It represents data in JSON or XML format. MQTT is a lightweight publish-subscribe protocol designed for low bandwidth devices. It uses topics to filter messages and publish/subscribe to decouple devices. Messages can be published with different quality of service levels to ensure reliable delivery.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views27 pages

An Iot Oriented Comparison: Rest and MQTT Rest and MQTT

REST and MQTT are protocols for IoT communication. REST leverages HTTP to provide data services using verbs like GET, PUT, POST, and DELETE. It represents data in JSON or XML format. MQTT is a lightweight publish-subscribe protocol designed for low bandwidth devices. It uses topics to filter messages and publish/subscribe to decouple devices. Messages can be published with different quality of service levels to ensure reliable delivery.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

REST and MQTT

An IoT oriented comparison

Credits: Prof.Pietro Manzoni, University of Valencia, Spain

REST vs MQTT 1
REST

 Basic concepts
 Basic programming

REST vs MQTT 12
Data Representation {“value": 237} vs. <value>237</value>

 Data-interchange format. Should be easy for humans to read and


write. Should be easy for machines to parse and generate
 Two main formats:
JavaScript Object Notation (JSON) XML
[http://www.json.org/]
<menu>
<id>file</id>
{"menu": { <value>File</value>
"id": "file", <popup>
"value": "File",
<menuitem>
"popup": {
"menuitem": [ <value>New</value>
{"value": "New", "onclick": ”NewDoc()"}, <onclick>NewDoc()</onclick>
{"value": "Open", "onclick": "OpenDoc()"}, </menuitem>
{"value": "Close", "onclick": "CloseDoc()"} <menuitem>
] <value>Open</value>
}
<onclick>OpenDoc()</onclick>
}
} </menuitem>
<menuitem>
<value>Close</value>
<onclick>CloseDoc()</onclick>
</menuitem>
</popup>
</menu>
REST vs MQTT 13
REST and HTTP
 REST stands for Representational State Transfer.
 It basically leverages the HTTP protocol and its related frameworks to
provide data services.
 The motivation for REST was to capture the characteristics of the
Web which made the Web successful.
 Make a Request – Receive Response – Display Response
 REST is not a standard… but it uses several standards:
 HTTP
 URL
 XML/HTML/GIF/JPEG/etc (Resource Representations)
 text/xml, text/html, image/gif, image/jpeg, etc (Resource Types, MIME Types)

REST vs MQTT 14
Verbs

 Represent the actions to be performed on resources

 HTTP GET
 HTTP POST
 HTTP PUT
 HTTP DELETE

REST vs MQTT 20
HTTP GET
 How clients ask for the information they seek.
 Issuing a GET request transfers the data from the server to the client
in some representation (JSON, XML, …)

GET http://www.library.edu/books
 Retrieve all books

GET http://www.library.edu/books/ISBN-0011021
 Retrieve book identified with ISBN-0011021

GET http://www.library.edu/books/ISBN-0011021/authors
 Retrieve authors for book identified with ISBN-0011021

REST vs MQTT 21
HTTP PUT, HTTP POST
 HTTP POST creates a resource
 HTTP PUT updates a resource

 POST http://www.library.edu/books/
Content: {title, authors[], …}
 Creates a new book with given properties

 PUT http://www.library.edu/books/isbn-111
Content: {isbn, title, authors[], …}
 Updates book identified by isbn-111 with submitted properties

REST vs MQTT 22
REST for devices control communication

GET /status/power all-lights.floor-d.example.com

PUT /control/onoff

PUT /control/color
#00FF00

REST vs MQTT 24
HTTP DELETE

 Removes the resource identified by the URI

 DELETE http://www.library.edu/books/ISBN-0011
 Delete book identified by ISBN-0011

REST vs MQTT 25
REST is widely used
 Google Maps:
 https://developers.google.com/maps/web-services/
 Try: http://maps.googleapis.com/maps/api/geocode/json?address=bangkok
 Twitter:
 https://dev.twitter.com/rest/public
 Facebook:
 https://developers.facebook.com/docs/atlas-apis
 Amazon offers several REST services, e.g., for their S3 storage solution
 http://docs.aws.amazon.com/AmazonS3/latest/API/Welcome.html
 Also:
 The Google Glass API, known as "Mirror API", is a pure REST API.
 Here is (https://youtu.be/JpWmGX55a40) a video talk about this API. (The actual API
discussion starts after 16 minutes or so.)
 Tesla Model S uses an (undocumented) REST API between the car systems and its
Android/iOS apps.
 http://docs.timdorr.apiary.io/#reference/vehicles/state-and-settings

REST vs MQTT 41
Very widely…
 https://apigee.com/providers
 https://apigee.com/resources/instagram

REST vs MQTT 42
MQTT

 Basic concepts
 Basic programming

Source: https://zoetrope.io/tech-blog/brief-practical-introduction-mqtt-protocol-and-its-application-iot

REST vs MQTT 43
Message Queuing Telemetry Transport
 A lightweight publish-subscribe protocol that runs on embedded
devices and mobile platforms designed to connect the physical world
devices with applications and middleware.
 http://mqtt.org/
 the MQTT community wiki: https://github.com/mqtt/mqtt.github.io/wiki
 http://www.hivemq.com/mqtt-essentials/
 Designed to provide a low latency two-way communication channel
and efficient distribution to one or many receivers.
 Minimizes the amount of bytes flowing over the wire
 Low power usage.

REST vs MQTT 44
Message Queuing Telemetry Transport

REST vs MQTT 45
Publish/subscribe pattern
 Pub/Sub decouples a client, who is sending a particular message
(called publisher) from another client (or more clients), who is
receiving the message (called subscriber). This means that the
publisher and subscriber don’t know about the existence of one
another.
 There is a third component, called broker, which is known by both the
publisher and subscriber, which filters all incoming messages and
distributes them accordingly.
Publisher 1 Publisher 2

Software bus

Subscriber 1 Subscriber 2 Subscriber 3


REST vs MQTT 48
Publish
 MQTT is data-agnostic and it totally depends on the use case how the
payload is structured. It’s completely up to the sender if it wants to
send binary data, textual data or even full-fledged XML or JSON.

REST vs MQTT 50
Topics
 Topic subscriptions can have wildcards
 ‘+’ matches anything at a given tree level so the topic “sensor/+/temp” would
match “sensor/dev1/temp”, “sensor/dev2/temp”, etc.
 ‘#’ matches a whole sub-tree, so “sensor/#” would match all topics under
“sensor/”.
 These enable nodes to subscribe to groups of topics that don’t exist
yet, allowing greater flexibility in the network’s messaging structure.

REST vs MQTT 52
Quality of Service (QoS)
 Messages are published with a Quality of Service (QoS) level, which
specifies delivery requirements.
 A QoS-0 (“at most once”) message is fire-and-forget.
 For example, a notification from a doorbell may only matter when immediately
delivered.
 With QoS-1 (“at least once”), the broker stores messages on disk and
retries until clients have acknowledged their delivery.
 (Possibly with duplicates.) It’s usually worth ensuring error messages are
delivered, even with a delay.
 QoS-2 (“exactly once”) messages have a second acknowledgement
round-trip, to ensure that non-idempotent messages can be delivered
exactly once.

REST vs MQTT 54
“Will” message
 When clients connect, they can specify an optional “will” message, to
be delivered if they are unexpectedly disconnected from the network.
 (In the absence of other activity, a 2-byte ping message is sent to clients at a
configurable interval.)
 This “last will and testament” can be used to notify other parts of the
system that a node has gone down.

REST vs MQTT 58
MQTT

 Basic concepts
 Basic programming

REST vs MQTT 62
Software available
 Brokers (https://github.com/mqtt/mqtt.github.io/wiki/servers):
 http://mosquitto.org/
 http://www.hivemq.com/
 https://www.rabbitmq.com/mqtt.html
 http://activemq.apache.org/mqtt.html
 https://github.com/mcollina/mosca
 Clients
 http://www.eclipse.org/paho/
 Source: https://github.com/eclipse/paho.mqtt.python
 http://www.hivemq.com/demos/websocket-client/
 Tools
 https://github.com/mqtt/mqtt.github.io/wiki/tools

REST vs MQTT 63
The suggested working environment
 Installing the Python client
sudo pip install paho-mqtt
 or
git clone https://github.com/eclipse/paho.mqtt.python.git
cd org.eclipse.paho.mqtt.python.git
python setup.py install

 Installing the whole Mosquitto environment


sudo wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
sudo apt-key add mosquitto-repo.gpg.key
cd /etc/apt/sources.list.d/

sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list


sudo apt-get update sudo apt-get install mosquitto

sudo apt-get install mosquitto mosquitto-clients python-mosquitto

REST vs MQTT 64
Controlling the broker
 Starting / stopping the broker
sudo /etc/init.d/mosquitto stop
sudo /etc/init.d/mosquitto start

 To avoid that it restarts automatically


sudo stop mosquitto

 Running verbose mode:


sudo mosquitto -v

 To check whether is running:


sudo netstat -tanlp | grep 1883
ps -ef | grep mosquitto

 man page:
https://mosquitto.org/man/mosquitto-8.html

REST vs MQTT 65
Sandboxes
 https://iot.eclipse.org/getting-started#sandboxes
 hostname iot.eclipse.org and port 1883.
 encrypted port 8883
 http://test.mosquitto.org/
 http://www.hivemq.com/try-out/
 http://www.mqtt-dashboard.com/

 Google CLOUD PUB/SUB A global service for real-time and reliable


messaging and streaming data
 https://cloud.google.com/pubsub/

REST vs MQTT 66
Excercise

REST vs MQTT 67
Excercise
 Broker information:

 WiFi SSID: mqtt


 WiFi password: brokerpi
 Broker IP address: 172.24.1.1

REST vs MQTT 68
Excercise
1. Start with the MQTT example. It will send a text message via MQTT
to the broker. Does it show up?

2. Next develop a MQTT + Pysense example. It will read sensor values


from Pysense and send them to the broker. Can you read the values
in the broker?

3. Install a MQTT client on your PC/smartphone and subscribe to the


topics of interest.

REST vs MQTT 69

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy