Lesson Smart Cities and the MQTT Protocol - Internet of Things - ثاني ثانوي

2012 7. IoT Messaging In this unit, you will learn about smart city applications and the (Message Queuing Telemetry Transport) MQTT communication protocol. You will also build an IoT application with an Arduino microcontroller and the MQTT protocol. Finally, you will perform data analysis operations on the built application. Learning Objectives In this unit, you will learn to > List the layers of a Smart City Architecture. > Cite examples of a Smart City. > Describe the functionality of the MQTT protocol. > Classify the Quality of Service (QoS) of the MQTT protocol. > Use Python script to publish messages to MQTT X Client. > Create a JSON Data file to store reports. > Use a Jupyter Notebook to perform data analysis operations on the JSON data file. Tools >Arduino IDE > JetBrains PyCharm > Autodesk Tinkercad Circuits > MCFI X Client

Lesson 1 Smart Cities and the MQTT Protocol

Lesson 1 Smart Cities and the MQTT Protocol Link to don www.lem.edu.x Smart Cities The majority of cities began as modest urban centers. They were not originally planned to support a rapidly rising population. Typically, rapid expansion affects city infrastructure and services. Roads, bridges, and sewage systems frequently exceed their maximum capacity, making daily living difficult. Supplying essentials such as water and electricity while simultaneously lowering the carbon footprint is an immediate challenge. As the global population increases, so do emissions and consumption. Population concentration in confined areas limits the ecosystem's ability to absorb pollutants. Increased emissions and waste contribute to the acceleration of climate change. Today, cities are accountable for 60-80 percent of the world's energy and greenhouse gas emissions and consume 60 percent of all drinkable water, losing as much as 20 percent to leakage. Optimizing resources, waste, and emissions with loT technologies is a major priority for city authorities worldwide. Service layer Dat cumber layer A Smart City loT Architecture The main challenge of smart loT solutions is connecting multiple complex systems into one consolidated solution. There are many proposed smart city architectures. One of the most prominent divides a smart city loT network into four layers. These are the Street, City, Data Center, and Services layers. Data goes from devices at the street layer to the city network layer, where it is consolidated, normalized, and stored. The data center layer feeds information to the services layer, comprising city-service provider apps. وزارة التعليم 1173-165 Figure 71 Asman city lot Architecture Street layer City layer

Lesson 1 Smart Cities and the MQTT Protocol

Street Layer The street layer consists of devices and sensors that gather data and operate depending on the overall solution's requirements and the networking components needed to collect and aggregate such data. At the street layer, a range of devices is employed for various smart city use cases, such as: Table 7.1: Street layer devices and sensors Туре Description Magnetic sensor A magnetic sensor may detect a parking event by monitoring changes in the magnetic field when a heavy metal object, such as a vehicle or a truck approaches it. Eljering controller Alighting controller can dim and brighten a light based on environmental variables and time Video cameras Video cameras paired with video analytics can recognize cars, faces and traffic conditions for various traffic and security applications, Air quality sensor An al quality sensor can detect and quantify quantides of pases and parnculate matter to provide a hyper-localized view of pollubon in a specific location وزارة التعليم 173-19 264 Counters In order to provide traffic analytics, counters record the number of vehicles moving in the street or parked in a public parking area

Lesson 1 Smart Cities and the MQTT Protocol

City Layer The city layer can appear to be a straightforward transport layer between edge devices and the data center or Internet. Network routers and switches must be deployed at the city layer above the street layer to support the transfer of big data. The city layer must transmit data through many types of protocols for various loT applications. However, some applications are sensitive to delays or packet losses. A lost packet may trigger an alert or create an incorrect status report. Therefore, the city layer must be resilient to ensure that a data packet sent from a sensor or gateway will always reach its destination. Data Center Layer The gathered sensor data is sent to a data center for processing and storage. Based on this data processing, important information and patterns will be identified and insights will be generated. For instance, a data center can give a city-wide perspective of the traffic and assist authorities in determining the demand for additional or fewer mass transport vehicles. The same traffic data may be used to automatically manage and synchronize the city's traffic light durations to reduce traffic congestion. Cloud and data storage services are crucial for developing any comprehensive loT solution. This data can be stored in data centers owned by city authorities or private companies depending on the local legislation. Services Layer Ultimately, the actual value of loT systems is determined by the services delivered to authorities and citizens. The processed data should be displayed following the particular demands of each data consumer, the distinct user experience requirements, and the different use cases. Buses and other public transit systems can be redirected if needed to avoid known congestion locations. The number of subway trains can be dynamically increased in response to an increase in traffic congestion, anticipating the decisions of commuters to choose public transit instead of their automobiles on days with heavy traffic. Riyadh Figure 72 Tofu update incaltime Example The Ministry of Municipal and Rural Affairs and Housing plans to implement more than 50 loT smart city projects by 2030, including smart traffic management and parking, environmental preservation and ultrash disposal systems, smart housing, and land management systems. Improving citizens' quality of life, financial sustainability, and service quality are the main goals. - כקו 265

Lesson 1 Smart Cities and the MQTT Protocol

266 Smart City Applications Connected Street Lighting Street lighting is one of the most expensive metropolitan utilities, accounting for up to 40 percent of the overall utility bill. Commonly, cities search for ways to cut lighting costs while simultaneously improving operating efficiencies and lowering initial expenditure. Installing a smart street lighting system can result in substantial energy savings and can be leveraged to deliver new services. Light-Emitting Diode (LED) technology is at the forefront of the move from conventional to intelligent street lighting. LEDs with low power consumption are ideally suited for smart solution applications. For instance, LED color or light intensity can be modified according to the conditions. Smart Traffic Control Vigure 7.3 Connected Street Lighting Traffic is one of the most well-known problems in every city. It is a major contributor to global pollution and loss of productivity. A smart city traffic solution would incorporate population counts, transit information, and vehicle counts on the road and forward the necessary data to traffic planners so they can take action. It is possible to enable traffic apps in cooperation with loT sensors to control traffic and decrease congestion. Using historical data, urban planners may create more effective strategies to minimize traffic congestion. Consequences of heavy traffic waves include a rise in local accidents, which are often minor but increase general congestion. A common solution for stop-and-go traffic is regulating the standard flow speed based on vehicle density. An application that detects traffic density in real time can regulate the length of the traffic light cycle to restrict or eliminate the wave effect by Figure 7 Smart Traffic Control controlling the number of vehicles added to the flow on major roads. Connected Environment The majority of large cities monitor air quality. Costly and decades-old air quality monitoring stations are frequently used to collect data. These stations are quite precise in their readings, but their range is extremely limited. Thus, a metropolis is likely to have several blind spots without enough data to properly identify air-quality patterns. Considering the cost and size of air quality monitoring stations, communities cannot afford to acquire the necessary number of stations to provide reliable information on a localized level and track pollution flows as they move through the city over time. دراية التعليم Age Roadside Unit (RSU) 2173-1465 Smart Safety Alerts Figure 7.5 Smart Air quality.Station On the side of the roads, there is a Dedicated short-range communications (DSRC) communication unit which serves as a gateway between vehicle on-board unit OBUS and the communications infrastructure. A Roadside Unit (RSU) is a special wireless communicating device located on the roadside that provides connectivity and information support to passing vehicles, including safety warnings and traffic information.

Lesson 1 Smart Cities and the MQTT Protocol

A smart city always requires localized, real-time, distributed knowledge about air quality. For this data, smart cities require the following: • Open-data systems that receive measurements of air quality from existing monitoring stations. • IoT sensors that give the same level of precision as the air quality stations but are far less expensive. • Environmental data visualization for authorities and citizens and storage of previous air quality data records to trace emissions through time and identify trends. Example The Line in the NEOM megacity in the KSA aims to incorporate the latest breakthrough smart city technologies to become the most technologically advanced urban environment. NEOM will rely heavily on smart city lot solutions to reach its goal of becoming a zero-emission city with no cars or traffic. Message Queuing Telemetry Transport (MQTT) Introduction to MQTT During the end of the 1990s, engineers from IBM and Arcom searched for a dependable, lightweight, and cost-effective protocol to monitor and manage many sensors and their data from a central server location, as was customary in the oil and gas sectors. The outcome of their study was the development of the Message Queuing Telemetry Transport (MQTT) protocol that is now standardized by the Organization for the Advancement of Structured Information Standards (OASIS). The MQTT protocol is more commonly used in loT applications than the HTTP protocol because it is easier to create complex architectures with devices that publish and receive data packets. MQTT Basics An MQTT client can be a "publisher" to send data to an MQTT server operating as a message server (message broker). The MQTT server receives the publishers' network connection and application messages. Additionally, it manages the subscription and unsubscription processes and delivers application data to MQTT clients serving as subscribers. Clients can subscribe to all or particular data from a publisher's information pool using MQTT. In this case, the MQTT client is called a "subscriber" The inclusion of a message broker in MQTT decouples the data transfer between publishers and subscribers. Publishers and subscribers are unaware of each other. The MQTT message broker guarantees that information may be delayed and stored in the event of network failure, which is an advantage of this decoupling. Due to this, publishers and subscribers are not required to be online simultaneously. Each client and server MQTT session consists of four phases: session establishment, authentication, data exchange, and session termination. Each client that connects to a server has a unique client ID that identifies the MQTT session between the two parties. The server treats each client individually when sending an application message to many clients. The drawbacks of the MQTT protocol are slower transmit cycles than HTTP, resource discovery and backup services must be implemented by the user, there is a lack of default security encryption and it is generally difficult to scale as the number of devices and brokers increases. 2173-1445 MOTT Client MOTT Clint (Subscriber) [Subscriber) Massage Broker MQTT server MQTT Client (Publisher) Temperature/ Relative Humidity Sensor Figure 7.7: MQTT functionality 267

Lesson 1 Smart Cities and the MQTT Protocol

268 Quality of Service (QoS) The MQTT protocol provides three degrees of service quality (QoS). QoS for MQTT is applied while exchanging application messages with publishers or subscribers. The delivery protocol primarily concerns application message delivery from a single sender to a single recipient. The following table presents the three MQTT QoS levels: Table 7.2: Quality of Service Levels Level Description (040) QoS 0: at most once ■ Doesn't survive failures. ■ Never duplicated This is an unacknowledged and best-effort data service known as "at most once" delivery. The publisher delivers a single message to a server, which relays it to each subscriber. The recipient receives no answer, and the sender does not retry to send the data. The recipient receives the message either once or not at all. QoS 1: at least once ■ Survives connection loss • Can be duplicated This Qos level assures messages are sent at least once between the publisher and server, then between the server and subscribers. This level guarantees at least one delivery QoS 2: exactly once Survives connection loss - Never duplicated This is the highest QoS level and is used when neither message loss nor duplication is acceptable. This QoS level has an extra cost since each packet includes an optional variable header with a packet Identification. This level provides a "guaranteed service" named "exactly once" delivery. The number of retries Is Irrelevant as long as the message is sent precisely once. Example Smart cities, objects can be attacked due to their centralized architecture. Using traditional security methods may not be adequate for the evolving loT environment. In the KSA, blockchain foT technologies will be developed in large cities to minimize central points of network fallure with a distributed architecture. The NEON megaproject will base its network on blockchain loT technologies to provide secure and accessible network infrastructure for its citizens. 21771-155

Lesson 1 Smart Cities and the MQTT Protocol

Exercises 1 Read the sentences and tick True or False. 1. Smart city technologies are being developed only to optimize traffic flow. 2. City layer network routers must be resilient against potential packet losses 3. Data from the street layer is sent directly to the Data Center layer. 4. Data stored in the Data Center layer can be stored on the servers of private companies. 5. The services layer contains the applications that the residents of the city use. 6. Connected street lighting systems require LED lights exclusively. 7. Historical data cannot be used to forecast future traffic. True False 4 8. Connected environment solutions can be used to reduce city emissions. B 9. The MQTT protocol was created to connect many sensors through a single service point. 10. On a basic connection with the MQTT protocol, the publisher and the subscriber acknowledge each other's presence B 2 What is the primary driver behind smart city advancements? Present your ideas below. pulohg 269

Lesson 1 Smart Cities and the MQTT Protocol

220 3 Create a diagram showing how data flows in a smart city lot architecture. 4 Provide examples of how sensors are used in a smart city street layer. حرارة الصليم

Lesson 1 Smart Cities and the MQTT Protocol

5 Describe how identical systems in the Data Center layer can be used in multiple applications Present your ideas below. 6 Provide two examples of smart city applications and briefly describe them. Present your ideas below. 7 Describe briefly how the MQTT protocol works. وزارة التعليم Б

Lesson 1 Smart Cities and the MQTT Protocol

772 8 Classify the three degrees of Quality Of Service for the MQTT protocol. 9 Create a diagram with an example of three devices connected by the MQTT protocol, one publisher, and two subscribers. مرارة التعليم

Lesson 1 Smart Cities and the MQTT Protocol