Professional Documents
Culture Documents
CH-4
CH-4
Specialized device
It requires Generic commodity devices.
solutions.
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?
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.
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.
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 CoAP
(SASL support
possible)
Retained Yes No
messages
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.
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)
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:
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.
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.
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.
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.
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
sequences.
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:
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
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.