Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Module 4 : Communication Models and Protocols: M2M VS IOT, Resource

Management, Registration, Discovery data exchange formats -XML and JSON,MQTT


Protocol, RESTful Architecture, HTTP REST Model, CoaP Protocol
5 marks questions
1) Diffrentiate Between IOT and M2M.
2) Write short note -CoaP
3) Write short note- MQTT
4) Advantage of CoaP over HTTP
5) Explain XML Format
6) Explain MQTT protocols used in IOT.
7) Explain CoaP and its advantages.
10 Marks Questions
1) Explain the difference between M2M and IOT.
2) Explain the potential and benefits of an IOT oriented approach over M2M by
considering a Health Band as the real-world use case Example.
3) What is XML? Explain the different parts of XML Program for creating student
database to store the student information.
4) What is JSON? Explain different parts of JSON. Write a program for creating weather
database to store the information.
5) Explain MQTT Protocol used in IOT details.
6) Explain CoaP and its advantages.
7) Describe in details with suitable example CoaP and MQTT protocol used in Application
Layers.
Difference between IoT and M2M
 
1. Internet of Things : IOT is known as the Internet of Things where things are said to be the
communicating devices that can interact with each other using a communication media.
Usually every day some new devices are being integrated which uses IoT devices for its
function. These devices use various sensors and actuators for sending and receiving data over
the internet. It is an ecosystem where the devices share data through a communication media
known as the internet or Iot is an ecosystem of connected physical object that are accessible
through internet. Iot means anything which can be connected to internet and can be controlled
or monitored using internet from smart devices or PC.
2. Machine to Machine : This is commonly known as Machine to machine communication.
It is a concept where two or more than two machines communicate with each other without
human interaction using a wired or wireless mechanism. M2M is an technology that helps the
devices to connect between devices without using internet. M2M communications offer
several applications such as security, tracking and tracing, manufacturing and facility
management.
M2M is also named as Machine Type Communication (MTC) in 3GPP ( 3rd Generation
Partnership Project).
M2M is communication could carried over mobile networks, for ex- GSM-GPRS, CDMA
EVDO Networks . M2M communication, the role of mobile networks is largely confined
to server as a transport networks.
M2M is only subset of IoT .
Difference between IoT and M2M :
Basis of IoT M2M

Abbreviation Internet of Things Machine to Machine

Devices have objects that are Some degree of intelligence


Intelligence
responsible for decision making is observed in this.

The connection is via Network


Connection type The connection is a point to
and using various
used point
communication types.

Traditional protocols and


Communication Internet protocols are used such
communication technology
protocol used as HTTP, FTP, and Telnet.
techniques are used

Data is shared between other


applications that are used to Data is shared with only the
Data Sharing
improve the end-user communicating parties.
experience.

Internet connection is required Devices are not dependent


Internet
for communication on the Internet.
Basis of IoT M2M

Type of It supports point-to-point


It supports cloud communication
Communication communication.

Involves the usage of both Mostly hardware-based


Computer System
Hardware and Software. technology

A large number of devices yet


Scope Limited Scope for devices.
scope is large.

Business Type Business 2 Business(B2B) and


Business 2 Business (B2B)
used Business 2 Consumer(B2C)

There is no support for


Open API support Supports Open API integrations.
Open APIs

Specialized device
It requires Generic commodity devices.
solutions.

Communication and device


Centric Information and service centric
centric.

Vertical system solution


Approach used Horizontal enabler approach
approach .

Devices/sensors, connectivity, Device, area networks,


Components
data processing, user interface gateway, Application server.

Smart wearables, Big Data and Sensors, Data and


Examples
Cloud, etc. Information, etc.

MQTT VS CoAP: Understanding the Differences

MQTT is a client-server protocol designed for the Internet of Things with publish-
subscribe messaging as its core. The MQTT server, also known as the broker, allows you to
inspect and manage connected IoT devices and messages. The Broker can also provide shared
services, such as retained messages. There are many client libraries and brokers available:
free, open source and commercially supported.

CoAP (The Constrained Application Protocol) is also a client-server protocol designed for the
Internet of Things. It too targets constrained devices, but is modeled on the World Wide Web
of resources (URIs) and HTTP. It uses GET, POST, PUT and DELETE verbs like HTTP,
which it can interoperate with through proxies. URIs represent device state, but devices can
be a CoAP client or server. For example, a client posts updates to a server or a server
responds to GET requests from clients. MQTT is a layer over TCP, whereas CoAP works
over UDP. Some features of TCP that are not in UDP, such as fragmentation, have to be re-
implemented in CoAP. On the other hand, CoAP is able to use the multicast feature of UDP,
which does not exist in TCP. Like MQTT, CoAP has implementations of clients and servers
in multiple languages.

What is MQTT?

MQTT is a communication protocol with features specifically targeted at IoT solutions.


MQTT:

 Uses TCP connections for reliability (assured delivery and packet error checking),
fragmentation, and ordering.
 Aims to minimize the data overhead of each MQTT packet.
 Can store the last known good data value for a device (retained messages).
 Can send Notifications when clients unexpectedly disconnect to allow client state to
be monitored (will messages).
 Uses bi-directional message flow - data from and commands to devices can use the
same TCP connection.
 Employs publish-subscribe routing, which makes it easy to add more consumers and
producers of data.

The MQTT commands are CONNECT, SUBSCRIBE, PUBLISH, UNSUBSCRIBE, and


DISCONNECT. Topics are the distribution units, to which clients can PUBLISH and
SUBSCRIBE. All authorized subscribers to a topic will receive all messages published to it.
MQTT topics do not have to be pre-defined: applications can create them simply by using
them.

What is CoAP?

RFC 7252 defines the core of the CoAP protocol. It was designed for machine-to-machine
(M2M) applications such as smart energy and building automation, supporting constrained
devices and networks while cooperating with HTTP through simple proxies. CoAP reuses the
web concepts of URIs and Internet media types to enable the building of IoT solutions.

The core interaction between clients and servers is request-response:

Server resources are discovered by sending a GET request to the /.well-known/core path.
Using UDP multicast you can discover servers in a group, or to send requests to multiple
endpoints at the same time – to turn a set of lights on or off for instance.

The observe extension allows clients to “subscribe” to a server URI, so that notifications are
sent whenever the state represented by that URI changes:
Non-confirmable messages have no acknowledgement, similar to QoS 0 in MQTT. Message
retries implement reliability. If no acknowledgment is received after sending a confirmable
message, that message is retried at increasing intervals until it is received or reaches a
timeout. Non-confirmable messages use a client-generated token of up to 8 bytes in length to
match responses to requests. To de-duplicate confirmable messages, the receiver uses a
combination of message ID and the source endpoint (IP address in UDP). It is possible to use
transports other than UDP for CoAP, although that is far less common than UDP. TCP and
SMS are possible.

MQTT and CoAP Comparison Summary

MQTT CoAP

Full name MQTT (the OASIS The Constrained


standardization Application Protocol
group decided it
would not stand for
anything)

Architecture Client/Server Client/Server

Command Topics URIs


targets

Underlying TCP UDP


transport

Default 1883 5684


insecure TCP
port

Default 8883 5684


secure UDP
port

Secure TLS + DTLS (TLS for UDP)


connections username/password
MQTT CoAP

(SASL support
possible)

Client Known connection None


observability status (will
messages)

Retained Yes No
messages

Messaging Asynchronous, Synchronous (or


Mode event-based asynchronous with
observe extension)

Message The broker can None


queuing queue messages for
disconnected
subscribers

Message 2 bytes minimum 4 bytes minimum


overhead

Message Size 256MB maximum Limit of underlying


transport - RFC 7252
suggests 1152 bytes for
UDP if nothing is
known about the
target.

Message Reliant on TCP No (RFC


fragmentation 7959 proposes an
extension for this)

Content type Any (binary) Any


MQTT CoAP

Pub/sub topic Level separator: No pub/sub


matching /Wildcards: + # (but extensions have
been proposed)

Message One to many, one to One to many, one to


distribution one one

Reliability Three qualities of Confirmable or not


service: 0 - fire and (roughly equivalent to
forget 1 - at least QoS 1 and 0)
once 2 - once and
only once

MQTT Performance vs CoAP Performance

These are the approximate packet sizes measured with Wireshark for MQTT and CoAP
including TCP and UDP overheads for a payload of 256 bytes. The CoAP scenario is a
synchronous GET plus the response with the data - there is no connection to establish or
subscription to be sent. The MQTT messages are sent at QoS 0 and the CoAP messages
without confirmation.

MQTT (TCP) CoAP (UDP)


Bytes Bytes

Establish connection 166 None

Subscribe 159 None

For each message 388 312+75 (request +


published response)

Sum for 1 message 1101 387


MQTT (TCP) CoAP (UDP)
Bytes Bytes

Sum for 10 8085 3870


messages

Sum for 100 77925 38700


messages

As you would expect, the overheads for UDP are less than TCP in this simple scenario.
However, to reach a similar level of capability as the MQTT TCP connection (larger packets,
reliable transmission), more complex CoAP behavior is needed.

The following are figures for an MQTT application using a local HiveMQ broker. The
application sends and receives a message before the next round trip starts. A similar approach
is taken with CoAP using the Eclipse Californium Java implementation of client and server
– it issues a GET to receive the data from the server before the subsequent GET. In each case,
the messages are 256 bytes.

Avg
Msgs
No. round
Protocol per
messages trip time
second
(ms)

10000 MQTT - 1,234 0.81


QoS 1

10000 MQTT - 18,416 0.054


QoS 0

10000 CoAP 8,729 0.1145

These are simple scenarios. In both cases, one can expand throughput by tailoring the
applications and servers to the solution needed. These figures are just for illustration.
The Advantages and Disadvantages of MQTT

Having a centralized MQTT broker means that IoT services are available in a ready-made
and portable form, so:

 applications can track messages and communicate the availability of services


 retained messages provide the last known good value for data streams

One of the greatest attributes of MQTT is the flexibility with which you can create solutions.
The publish-subscribe paradigm allows many clients to produce and share information with
each other as well as back end systems. Brokers can be chained together, and MQTT
gateways connected to cloud services just by mapping the flow of messages through topics.
Over-the-air updates of firmware and configurations can be broadcast to devices.

The inbuilt queueing of MQTT brokers can deal with connection interruptions and provide
buffering for constrained devices that don’t have the capacity to do it themselves.

The Advantages and Disadvantages of CoAP

CoAP behaves similarly to HTTP, which is familiar to many developers and users. Whether
that suits more or less than MQTT’s publish/subscribe can be a matter of taste.

With CoAP using UDP it has smaller overheads than TCP, but a smaller maximum payload
and less reliability. It can use the multicast ability of UDP for certain efficient functions.
CoAP was designed for IoT, so it does a good job of supporting low-powered devices.

Some downsides of CoAP using UDP are:

 It can be more difficult to traverse firewalls, unlike MQTT that uses WebSockets.
 It can be used in DDoS attacks if internet-facing.

CoAP solutions are very flexible since the definition of client and server are dynamic. In
MQTT, these elements are fixed. That also means no CoAP standard broker exists with
general services that can be relied on.

Conclusion: MQTT Vs. CoAP – Which is better for IoT?

The out-of-the-box features MQTT brokers provide must be implemented by CoAP


applications. Since some MQTT brokers have standard behavior and cloud services available,
IoT solutions can be built rapidly.

CoAP could be well-suited to an edge, on-premises, or mesh network that connects to a


gateway, which then uses MQTT to the cloud for IoT services. You can also use MQTT for
this purpose. There is also an MQTT-like competitor for CoAP, MQTT-SN, which is in the
process of being standardized.
XML BASIC:

Extensible Markup Language (XML) is a markup language and file format for storing,
transmitting, and reconstructing arbitrary data. It defines a set of rules for encoding
documents in a format that is both human-readable and machine-readable. Unlike other
programming languages, XML cannot perform computing operations by itself.

How to Create an XML File


1. Open your text editor of choice.
2. On the first line, write an XML declaration.
3. Set your root element below the declaration.
4. Add your child elements within the root element.
5. Review your file for errors.
6. Save your file with the . ...
7. Test your file by opening it in the browser window.

Creating Student data base


8. <?xml version="1.0" encoding="UTF-8"?>
9. <?xml-stylesheet type='text/xsl' href='cgpa.xsl'?>
10. <class>
11. <student>
12. <name> ABC </name>
13. <id> 001 </id>
14. <branch> IT </branch>
15. <cgpa> 9 </cgpa>
16. </student>
17. <student>
18. <name> PQR </name>
19. <id> 004 </id>
20. <branch> Computer </branch>
21. <cgpa> 7 </cgpa>
22. </student>
23. <student>
24. <name> XYZ </name>
25. <id> 006 </id>
26. <branch> IT </branch>
27. <cgpa> 10 </cgpa>
28. </student>
29. </class>
30. <?xml version="1.0" encoding="UTF-8"?>
31. <xsl:stylesheet version="1.0" xmlns:xsl="https://www.w3.org/1999/XSL/Transf
orm">
32. <xsl:template match="/">
33. <html>
34. <body>
35. <h2>Student list in descending order of their CGPA.</h2>
36. <table border="1">
37. <tr bgcolor="lightblue">
38. <th>ID</th>
39. <th>Name</th>
40. <th>Branch</th>
41. <th>CGPA</th>
42. </tr>
43. <xsl:for-each select="class/student">
44. <xsl:sort select="cgpa" order="descending" data-type="number"/>
45. <tr>
46. <td><xsl:value-of select="id"/></td>
47. <td><xsl:value-of select="name"/></td>
48. <td><xsl:value-of select="branch"/></td>
49. <td><xsl:value-of select="cgpa"/></td>
50. </tr>
51. </xsl:for-each>
52. </table>
53. </body>
54. </html>
55. </xsl:template>
56. </xsl:stylesheet>

JSON(Java Script Object Notation)

JSON (JavaScript Object Notation) is defined as a file format used in object-oriented


programming that uses human-readable language, text, and syntax to store and communicate
data objects between applications.

A JSON file contains structured text in JSON data format. JSON (JavaScript Object
Notation) is a widely used data format that enables simple communication between systems
(more about JSON). More specifically, the format is used to structure and transfer
information between web-based systems.

The two primary parts that make up JSON are keys and values. Together they make a
key/value pair. Key: A key is always a string enclosed in quotation marks. Value: A value
can be a string, number, boolean expression, array, or object.
JSON or JavaScript Object Notation is a standard text-based format developed from the
JavaScript object syntax and used to portray structured data. JSON, pronounced as ‘jason’, is
an open standard format for creating and storing files or exchanging data that uses
comprehensible and human-readable text made up of attributes and serializable values.
JSON is a data format that is not dependent on any language. It is a data format used by
several modern programming languages. JSON is used in electronic data exchange, such as
transmitting data in web applications. Websites are made of web pages. These web pages
display pre-stored information in a server and interact with the server using data formats such
as JSON.
To understand JavaScript Object Notation, you must have a basic understanding of computer
programming Hyper Text Markup Language and be familiar with JavaScript and the basics of
CSS. Although it was developed following the JavaScript Object Syntax format and shares
many similarities (since both are elements of Object Oriented Programming or OOP), it
remains an independent data format from the syntax mentioned above. Now, different
programming languages can read and generate JSON codes.

JSON format has a syntax nearly identical to the code for JavaScript objects. This similarity
makes it very easy for programs written in JavaScript to be converted to a JSON data format.
Even though JSON is derived from JavaScript object notation syntax, JSON is a text-only
subset of JavaScript syntax.
In JSON, data is represented in name/value pairs separated by a comma. The curly bracket
contains the object and is separated from the name by a colon. Square brackets hold arrays,
and a comma separates the array from values.
Here is an example:
“movie”: [
{
“season”: “01”,
“language”: “english”,
“episode”: “second”,
“director”: “Robert Anderson”
}
]
JSON is built on two structures which are

 An ordered list of values, which translates to arrays, vectors, lists, or

sequences.

 A collection of name/value pairs; can be an object, record, hash table, etc.

Uses of JSON
JSON is widely used all over the world, and this reflects how important it has become in
today’s world. It gained so much popularity because of its ease of use and simplicity. The
JSON data format replaced XML, which was formally in use but was very heavy and difficult
to learn due to several modifications. On the other hand, JSON makes data transfer a walk in
the park. The syntax is straightforward to learn, lightweight, and compatible with human and
machine languages.
The most common uses of JSON include:

 It is used in writing JavaScript-based applications that have websites and

browser extensions as part of their features.

 It is essential in the transfer of structured data across network connections.

 It is used to draw up data from a server by web applications.

 JSON data format is used to publish public data by web services.

 It is used in migrating from one database to another.

Types of JSON Data


JavaScript Object Notation is currently a widely used data format for any data exchange on
the World Wide Web. This data format is easy to understand, with seven different data types.
They are;

1. Number

2. String

3. Boolean

4. Array

5. Object
6. Whitespace

7. Null
Let us now explore each one of these JSON data types in more detail:

1. Number
A number in JSON is a data type that is used in JSON to represent figures in the base 10
system of counting. A number represents integers, negative integers, simple floating point
numbers, and exponential notations.
However, numbers cannot be represented as strings in the JSON format; only the base 10
(decimal) is used, making JSON’s octal or hexadecimal system obsolete. Infinity and NaN
are also not used.

 Integer: Includes the digit 0 and positive or negative figures from 1-9

 Fraction: This represents figures with decimal points like .5, .8

 Exponent: A number in JSON can have an exponent of 10 and should be

prefixed by the exponent sign; e+ e- E+ E-


An example of this JSON data type is { “length”: 150.35 }.

2. String
A string is a sequence of zero or more doubled spaced Unicode characters written with
particular rules in mind. Strings in JSON are always written using double quotation marks (“
“), similar to the C programming language. Strings enclosed in single quotes (‘ ‘) become
invalid. You can also include backslash-escaped characters like forward slash (\/), backward
slash (\\) backspace (\b), newline (\n), carriage return (\r) horizontal tab (\t) etc. A character is
a string with just a single element.
An example of this type of JSON data is { “name”:”Jade” } or { “city”:”Accra\/Ghana” }.

3. Boolean
Boolean is a data type in JavaScript Object Notation that can exist as one of only two options.
Boolean values can only be true or false. When using Boolean data type, quotation marks are
not used. Examples of Boolean data type are { “transparency” : false } and { “green” : true }.
4. Array
An array is an arranged set of values that are enclosed within a set of square brackets
consisting of the left square bracket ( [ ) and the right square bracket ( ] ). An array consists
of values separated using a comma (,). Arrays consist of related values, that is, items under a
collective group. An array should be used when the key names are made of sequential
integers. You can start Indexing in arrays from either 0 or 1.
An example of this type of JSON data is: { “colors” : [“red”, “orange”, “yellow”, “blue”] } or
{ “serial numbers” : [“302”, “303” “305” “306” “307”] }

5. Object
An object is a data type in JavaScript Object Notation that is made up of unordered or non-
structured sets of data represented as name/value pairs and placed in between a pair of curly
brackets (left and right curly brackets {} ).
An object can contain any number of name/value pairs ranging from zero or more. The keys
must be of the strings data type and should be unique. When there is more than one
name/value pair, the name is followed by a colon, and then the key/value pairs are separated
using a comma (,).
An example of this type of JSON data is:
{
“participant” : { “name” : “rose”, “age” : “17”, “status” : “disqualified” }
}

6. Whitespace
Whitespace is simply a space added to a code to make it easier for humans to read and
understand. Whitespace does not actually affect the code or JSON data format as a whole. It
can be reduced to the bare minimum without corrupting the data. Whitespace can be a single
or multiple space, Horta tap tab, new line, or carriage return.
An example of this type of JSON data is { “name” : “ Praise Johnson” } (whitespace is used)
or { “name” : “PraiseJohnson” } (whitespace is not used).
7. Null
Technically, null is not a data value type. It is, however, classified as a special value in
JavaScript Object Notation. Null describes the absence of value; that is, when there is no
value assigned to a key, it is described as null. When using the null title, there is no need to
use quotation marks. An example of this type of JSON data is:
{
“friendly” : true
“receptive” : true
“intelligent” : null
}

Functions of JSON
Functions in programming refer to structured blocks of code that are used to carry out
specific operations over and over again. Functions are created as a standard operation and
mean the same thing for a particular programming language. Functions provide better
modularity for applications and make it easy to reuse codes. Although the concept remains,
the same, different languages may refer to these unique codes as methods, subroutines, or
procedures. Some functions are built into the programming language, while a programmer
can write others.

You might also like