Unit-3-Iot Application Development Part-2 (1)
Unit-3-Iot Application Development Part-2 (1)
MQTT (Message Queuing Telemetry Transport) is an IBM-developed lightweight messaging protocol that
was initially introduced in 1999.
It interprets communications between devices, servers, and applications using the pub/sub pattern.
The MQTT protocol was established with the goal of connecting sensors on oil pipelines to
communications satellites with the least amount of battery loss and bandwidth use possible.
MQTT has been evolving since its creation, with version 5.0 being released in May2018. Version 3.1.1
was accepted as an ISO standard in 2013.
Advantages of MQTT
The following are some of the advantages of MQTT:
Lightweight code footprint -The MQTT protocol requires only a few lines of codeto get up and running
on devices.
Minimized data packets- MQTT is a low-energy protocol. If a device is battery-powered or has a low
CPU, this is ideal.
Speed - Outside of QoS, MQTT functions in real time with no delays.
Ease of Implementation- MQTT includes libraries in a number of programming languages, including
Elixir and Python.
Last will and testament- If a client disconnects abruptly, you can send a message to all subscribers with
information on how to fix the problem.
Retained messages- When a client subscribes to a topic, it will automatically get one retained message for
that subject (like a pinned post on social media).
MQTT architecture
In the MQTT protocol, connected devices are known as "clients," and they communicate with a server known as
the "broker." Data communication between clients is handled by the broker.
When a customer (known as a "publisher") wants to disseminate information, it publishes to a specific topic, which
the broker then distributes to any clients who have subscribed to that topic (known as "subscribers").
The publisher does not require information on the number of subscribers or their locations. As a result, subscribers
do not require any information about the publisher.
Any client can be both a publisher and a subscriber. The clients are usually unaware of each other and are only
aware of the broker who acts as an intermediary. The “pub/sub model” is a popular term for this structure.
MQTT Quality of Service (QoS)
The publisher additionally sends a QoS (Quality of Service) level along with the message. This level specifies the
message's delivery guarantee.
MQTT messaging process
In general, we have two publishing stages that we need to consider during an MQTT message transmission:
1. From a publisher (data sender) to a broker (data receiver), and
2. From the broker (data sender) to a subscriber (data receiver).
In every message transmission, the broker acts as a middleman between the publisher and subscriber,
performing a role of both a receiver and sender of data. To enable messaging, the MQTT broker completes
a number of steps.
When an MQTT client is trying to subscribe to a topic, it sends a SUBSCRIBE packet that, among other
information, also includes a wanted QoS level. Read our ultimate guide to learn what MQTT packet is. The
broker, in turn, answers this request with a SUBACK packet and specifies a corresponding return code to
confirm the subscription (including the QoS level).
When an MQTT client wants to publish a message to a topic, it sends a PUBLISH packet specifying the
desired QoS level.
Now that we better understand MQTT QoS, let‟s learn more about the different QoS levels and what they
mean.
Since the broker does not need any acknowledgement, the publisher discards the message once it is sent.
Simply put, the goal for MQTT QoS 0 is to blast its message out at most once.
Example
An example of MQTT QoS 0 is the sensor at the entrance of a grocery store that counts the number of
customers entering and exiting the store. In this scenario, the publisher sends the current count and does not
need any acknowledgement; that is why it uses QoS 0.
MQTT QoS 1
Now, with MQTT QoS 1, things get a little more complex. The publisher will send a message at least once and
will continue sending it until the broker receives an acknowledgement.
QoS 1 ensures that a message will be delivered and received at least once. After the publisher (data sender)
sends the message as part of the PUBLISH packet to the MQTT broker (data receiver), the broker (data
receiver) responds to the publisher (data sender) with the acknowledgement PUBACK packet, and the sending
process stops.
The publisher will continue sending this message if it does not receive a PUBACK packet.
For instance, a connection drops during message sending. In this case, once it is restored, the publisher will send
the PUBLISH packet again.
We consider MQTT QoS 1 a pesky alarm clock that will not stop! At least it gets you up for work.
Example
One scenario where MQTT QoS 1 is frequently used is in cars. If the driver‟s seat belt is not fastened, the car
sends an alert repeatedly till the driver fastens his seat belt. In this case, the publisher needs a response from the
subscriber before the transmission is considered complete.
MQTT QoS 2
Okay, we saved the best for last. If QoS 0 was the lowest level of service, QoS 2 must be the highest one, right?
Yes, QoS 2 is the highest level of service and is considered the most reliable but also the slowest MQTT quality of
service.
You may wonder why QoS 2 is the slowest. Every message delivery requires a four-part verification between both
tandems: a publisher and a broker, and the broker and a subscriber. This might seem tedious, but this process
ensures that subscribers receive a message exactly once.
As mentioned, every message delivered with MQTT QoS 2 has a four-part verification. The four packets that take
part in the communication are:
1. Publish
The data sender then sends a PUBREL packet to the data receiver to confirm the receipt of the PUBREC packet
the former received from it. In the end, the data receiver sends a PUBCOMP packet with the PacketID of the
initial PUBLISH packet to the data sender, indicating that the message delivery has been successful.
The broker processes packets cyclically, forwarding the PUBLISH packet for each message and then waiting for
the PUBREC response to arrive from all subscribers. Once all subscribers have replied with a PUBREC, the
broker removes the message stored in the inflight message queue. After that, it informs the subscribers of this with
a PUBREL packet.
ZigBee
In a smart home everything connected to everything (literally). So that all the connected devices can share
information with each other.
You can control all of them without the need to visit each thing individually.
All the devices, manufactured by different companies are connected over a single network. Therefore,
requires a common language for its communication by using ZigBee.
ZigBee is a wireless technology standard that provides a set of communication protocols for short-range
communications.
It is an open-source global standard developed by Zigbee Alliance to address the needs of low-cost, low
power wireless IoT networks.
The protocol is used in low data rate, short to medium range wireless networking devices like senors and
control networks.
ZigBee provides flexibility for developers and end-users as an interface due to its features.
ZigBee is a popular technology for smart home in wireless technology with three distinct features.
Firstly, communication is two -way which makes ZigBee devices reliable.
Secondly, it caters to all sectors like lightings, security, appliances and home access.
Thirdly, this technology requires very little power mainly due to its low latency and low duty cycle.
ZigBee uses mesh network and thus reduce chances of failure at nodes.
ZigBee Features
ZigBee offers a wireless range of 70m indoors and 400m outdoors.
It supports multiple networks like point to point, point to multipoint mesh- networks.
It uses AES 128 encryption for protecting your information while on air transfers.
Its easy installation and maintenance makes it more affordable.
Zigbee uses physical and MAC layers of the IEEE 802.15.4 specification.
It operates in an unlicensed 2.4 GHz ISM band is most commonly used worldwide..
It can transfers data at a rate of 250 kbps.
Operating Frequency Bands (Only one channel will be selected for use in a network):
1. Channel 0: 868 MHz (Europe)
2. Channel 1-10: 915 MHz (the US and Australia)
3. Channel 11-26: 2.4 GHz (Across the World)
Advantages of Zigbee:
1. Designed for low power consumption.
2. Provides network security and application support services operating on the top of IEEE.
3. Zigbee makes possible completely networks homes where all devices are able to communicate and be
4. Use in smart home
5. Easy implementation
6. Adequate security features.
7. Low cost: Zigbee chips and modules are relatively inexpensive, so it a cost-effective solution for IoT
applications.
8. Mesh networking: Zigbee uses a mesh network topology, which allows for devices to communicate with
each other without the need for a central hub or router.
9. Reliability: Zigbee protocol is designed to be highly reliable, with robust mechanisms in place to ensure that
data is delivered reliably even in adverse conditions.
Disadvantages of Zigbee :
1. Limited range: Zigbee has a relatively short range compared to other wireless communications protocols,
which can make it less suitable for certain types of applications or for use in large buildings.
2. Limited data rate: Zigbee is designed for low-data-rate applications, which can make it less suitable for
applications that require high-speed data transfer.
3. Interoperability: Zigbee is not as widely adopted as other IoT protocols, which can make it difficult to find
devices that are compatible with each other.
4. Security: Zigbee‟s security features are not as robust as other IoT protocols, making it more vulnerable to
hacking and other security threats.
Channel Access:
1. Contention Based Method (Carrier-Sense Multiple Access With Collision Avoidance Mechanism)
2. Contention Free Method (Coordinator dedicates a specific time slot to each device (Guaranteed Time Slot
(GTS)))
Zigbee Applications:
1. Home Automation
2. Medical Data Collection
3. Industrial Control Systems
4. meter reading system
5. light control system
6. Commercial
7. Government Markets Worldwide
8. Home Networking
Bluetooth
Bluetooth is universal for short-range wireless voice and data communication.
Bluetooth works on the principle of transmitting and receiving data using radio waves.
Bluetooth is a wireless technology standard IEEE 802.15.
It is a Wireless Personal Area Network (WPAN) technology used for communication over smaller
distances.
This technology was invented by Ericson in 1994.
It operates in the unlicensed (ISM) band from 2.4 GHz to 2.485 GHz.
Maximum devices that can be connected at the same time are 7.
Bluetooth ranges up to 10 meters.
It provides data rates up to 1 Mbps or 3 Mbps depending upon the version.
The spreading technique that it uses is FHSS (Frequency-hopping spread spectrum).
A Bluetooth network is called a piconet and a collection of interconnected piconets is called scatternet.
Bluetooth Transmission capacity is 720 kbps.
Bluetooth is a Low-cost short-distance radio communications standard.
Bluetooth is robust and flexible.
Bluetooth is cable replacement technology that can be used to connect almost any device to any other
device.
It is in a star configuration, with a single primary device that controls several secondary devices.
Bluetooth Architecture:
The architecture of Bluetooth defines two types of networks:1. Piconet and 2. Scatternet
Piconet:
Piconet is a type of Bluetooth network that contains one primary node called the master node
and seven active secondary nodes called slave nodes.
Thus, there are a total of 8 active nodes which are present at a distance of 10 meters.
The communication between the primary and secondary nodes can be one-to-one or one-to-many.
Possible communication is only between the master and slave;
Slave-slave communication is not possible.
It also has 255 parked nodes, these are secondary nodes and cannot take participation in communication
unless it gets converted to the active state.
Scatternet:
It is formed by using various piconets.
A slave that is present in one piconet can act as master or we can say primary in another piconet.
This kind of node can receive a message from a master in one piconet and deliver the message to its
slave in the other piconet where it is acting as a master. This type of node is referred to as a bridge node.
A station cannot be mastered in two piconets.
Bluetooth protocols stack:
Radio Layer (RF): responsible for radio frequency communication between devices. It defines two types
of physical links: connection-less and connection-oriented.
Baseband Link Layer: performs data transfer and link management between devices. It performs the
connection establishment within a piconet, addressing, packet format, timing and power control.
Link Manager Protocol (LMP): manages the link between devices, including establishing and
maintaining connections. It is responsible for creating the links, monitoring their health, and terminating
them gracefully upon command or failure.
Logical Link Control and Adaptation Protocol (L2CAP): provides a multiplexing protocol for data
packets and supports higher-level protocols. It is also known as the heart of the Bluetooth protocol stack. It
allows the communication between upper and lower layers of the Bluetooth protocol stack. It also performs
segmentation and multiplexing.
Service Discovery Protocol (SDP): enables devices to discover services provided by other devices.
WAP (Wireless Access Protocol): It is used for internet access.
RF comm layer: It is a cabal replacement protocol. It is short for Radio Frontend Component. It
provides a serial interface with WAP and OBEX. It also provides emulation of serial ports over the
logical link control and adaption protocol(L2CAP). The protocol is based on the ETSI standard TS
07.10.
OBEX (Object Exchange): a set of protocols for exchanging objects such as vCards, vCalendar
appointments, etc.
Telephony Control Protocol (TCS): handles telephony-related control signals for cordless telephones.
Types of Bluetooth
Various types of Bluetooth are available in the market such as.
In-Car Headset: One can make calls from the car speaker system without the use of mobile phones.
Stereo Headset: To listen to music in car or in music players at home.
Webcam: One can link the camera with the help of Bluetooth with their laptop or phone.
Bluetooth-equipped Printer: The printer can be used when connected via Bluetooth with mobile phone or
laptop.
Bluetooth Global Positioning System (GPS): To use GPS in cars, one can connect their phone with car
system via Bluetooth to fetch the directions of the address.
Advantage:
It is a low-cost and easy-to-use device.
It can also penetrate through walls.
It creates an Ad-hoc connection immediately without any wires.
It is used for voice and data transfer.
Disadvantages:
It can be hacked and hence, less secure.
It has a slow data transfer rate: of 3 Mbps.
It has a small range: 10 meters.
Bluetooth communication does not support routing.
The issues of handoffs have not been addressed.
Applications:
It can be used in laptops, and in wireless PCs, printers.
It can be used in wireless headsets, wireless PANs, and LANs.
It can connect a digital camera wirelessly to a mobile phone.
It can transfer data in terms of videos, songs or files from one cell phone to another cell phone or computer.
It is used in the sectors of Medical health care, sports and fitness, Military.
The device mode should not be confused with its Generic Attribute Profile (GATT), where a device can either
act as a server or a client. Once two devices establish a connection, the one that sends out the data is the server,
and the one that receives it is the client. So if a smart band sends, let's say, heart rate monitor readings to the
smartphone, then it acts as a server. But if the smartphone sends software update to the smart band, then the
smartphone is the server.
BLE Vs Bluetooth
BLE is an independent standard that is incompatible with the "classic" Bluetooth.
SIG introduced Bluetooth Low Energy in its 2010 Bluetooth 4.0 specification
Its main focus was on health- and fitness-related devices and smart home and indoor location.
Bluetooth Classic is designed for continuous, two-way communication, whereas BLE transfers smaller
packets of data over short periods of time in one way;
BLE has much smaller energy consumption (as much as 100 times smaller than Bluetooth Classic);
Bluetooth Classic is not so limited, it has a longer range and higher data throughput.
Applications of BLE
1. BLE technology in Medicine: Blood glucose or pressure monitor could report its status over BLE.
2. BLE technology in Retail: Allows consumers and retailers to communicate with each other quickly and
easily. Beacons track the route of people inside a shop, gather customer data, and help to offer a
personalized experience.
3. BLE technology for location tracking: Passengers in an airport can use BLE apps to find personalized
directions within the airport. It can also show them the way to restaurants, services, and baggage carousels.
4. BLE technology for Communicators: It helps users to take calls by doing the flip action and serve as
wireless speakers when linked to music streaming devices. Depending upon the mobile platform, it is
available for both BLE iOS and BLE Android.
5. BLE technology for tracking pets: Tracking your pet‟s location and even its behavior. Specific
applications like PitPatPet use Bluetooth technology to track the dog‟s activity and fitness.
6. BLE technology in Agriculture: Also, beacons could collect and analyze data such as environmental
and sub-tidal water temperature, chlorophyll values, and more. Combined with satellite imaging data and
properly analyzed, this could bring about efficient and sustainable food production.
7. BLE technology for Smart Kitchen: The smart fork helps to monitor and track the eating habits to
support weight loss. A smart frying pan is a gadget that checks the temperature inside your food and lets
you know when it‟s done without needing to stick a fork in it.
8. Smart Tags: Smart tags are a type of Bluetooth LE gadget that shows the technology‟s potential. The
most interesting example of smart tags is Tile. You can attach this small tag to anything you like, and
it‟ll communicate with your phone over Bluetooth LE, allowing your phone to keep track of its location.
9. BLE technology for sports: Augmented reality glasses featuring a tiny heads-up display and smart
wearable that show cyclists critical data during training are examples. It helps athletes improve their
performance theoretically and improve results.
10. Home Automation Systems: Reminders if some devices (like stoves or ovens) are turned off, Remote
control (360-camera), Smart Locks, Sensors (water, fire, motion, light). It can be controlled and operated
by Bluetooth smart ready gadgets that can receive and transmit Bluetooth signals.
PSOC4 BLE architecture and components overview
PSoC 4 is a programmable embedded system controller with an ARM® Cortex®-M0 CPU.
It combines programmable analog, programmable interconnect, user-programmable digital logic, and
commonly used fixed-function peripherals with a high performance ARM Cortex-M0 subsystem.
The PSoC 4xxx-BL family is based on the PSoC 4 architecture which supports Bluetooth.
This is upward-compatible with larger members of PSoC 4.
Features
The PSoC 4xxx-BL family has these major components:
BLE radio and subsystem
32-bit Cortex-M0 CPU with single-cycle multiply, delivering up to 43 DMIPS (Dhrystone MIPS (Million
Instructions per Second))at 48 MHz
Up to 256 KB flash and 32 KB SRAM
Direct memory access (DMA)
Four independent center-aligned pulse-width modulators (PWMs) with complementary, dead-band
programmable outputs
Twelve-bit SAR ADC (with a sampling rate of 1 Msps in PSoC 42xx-BL and 806 ksps in PSoC 41xx-BL)
with hardware sequencing for multiple channels
Up to four opamps that can be used for analog signal conditioning and as a comparator
Two low-power comparators
Two serial communication blocks (SCB) that can work as SPI, UART, I2C, and local interconnect network
(LIN) slave serial communication channels
Up to four programmable logic blocks, known as universal digital blocks (UDBs)
CapSense
Segment LCD direct drive
Low-power operating modes: Sleep, Deep-Sleep, Hibernate,and Stop
Programming and debugging system through serial wiredebug (SWD)
fully supported by PSoC Creator™ IDE tool
Processor
The heart of the PSoC 4 is a 32-bit Cortex-M0 CPU core running up to 48 MHz.
It uses 16-bit instructions and executes a subset of the Thumb-2 instruction set.
This instruction set enables fully compatible binary upward migration of the code to higher performance
processors such as Cortex M3 and M4.
The CPU has a hardware multiplier that provides a 32-bit result in one cycle.
Interrupt Controller
The CPU subsystem includes a nested vectored interrupt controller (NVIC) with 32 interrupt inputs
a wakeup interrupt controller (WIC), which can wake the processor from Deep-Sleep mode.
The Cortex-M0 CPU of PSoC 4 implements a non-maskable interrupt (NMI) input,
Memory
The PSoC 4 memory subsystem consists of flash and SRAM.
A supervisory ROM, containing boot and configuration routines, is also present.
Flash
The PSoC 4 has a flash module, with a flash accelerator tightly coupled to the CPU, to improve average
access times from the flash block.
SRAM
The PSoC 4 provides SRAM, which is retained during Hibernate mode.
Power System
The PSoC 4 operates with a single external supply in the range 1.71 V to 5.5 V.
PSoC 4 has four low-power modes – Sleep, Deep-Sleep, Hibernate, and Stop
Default mode is Active mode. In Active mode, the CPU runs with all the logic powered.
In Sleep mode, the CPU is powered off with all other peripherals functional.
In Deep-Sleep mode, the CPU, SRAM, and high-speed logic are in retention; and the low-frequency
peripherals are in operation.
In Hibernate mode low-frequency peripherals stop operating.
Multiple internal regulators are available in the system to support power supply schemes in different power
modes.