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

Name: Charmi Shah

Roll Number: 20BCP299


Sem: 7
Division: 4(Group-8)
Subject: IOT Lab
IOT Lab Experiment-3

Aim: Aim: Designing the Publish Subscribe system using MQTT and Paho on
Mosquitto Broker

Prerequisite: Basics of Microprocessor and Operating System

Outcome: To impart knowledge of Internet of Things Technology

Theory:
MQTT:

MQTT (Message Queuing Telemetry Transport) is a lightweight messaging protocol that is


widely used for IoT applications. It is publisher subscriber model. It uses TCP protocol at
transport layer. It is designed to be efficient in low-bandwidth, high-latency networks and is
ideal for use cases where devices need to communicate with a central server or with each
other.

MOSQUITTO BROKER:

Mosquitto is an open-source Message Broker, also known as a Message Queue or Messaging


System, that allows you to pass messages between different devices or software components.
It is based on the MQTT (Message Queuing Telemetry Transport) protocol, which is a
lightweight, machine-to-machine (M2M) and IoT communication protocol.

PAHO:

Paho is an open-source implementation of the MQTT (Message Queuing Telemetry


Transport) protocol, which is a lightweight, machine-to-machine (M2M) and Internet of
Things (IoT) communication protocol. Paho provides a set of APIs and tools for developing
MQTT-compliant applications and devices.

Steps:

1. Open terminal and install paho mqtt client with this command
• pip3 install paho-mqtt
2. To run mosquitto broker run this command in terminal
• Mosquito -v
3. To test if the mosquitto runs or not type this command in terminal
1
• Mosquitto_sub -t test/status
4. Then navigate to location where the python file is present in terminal and
run the python file
• Python3 pythonfilename.py

Python code

import paho.mqtt.client as paho

import sys

def onMessage(client, userdata, msg):

print(msg.topic + ": "+msg.payload.decode())

client = paho.Client()

client.on_message = onMessage

if client.connect("localhost", 1883, 60)!=0:

print("Could not connect to MQTT Broker")

sys.exit(-1)

client.subscribe("test/status")

try:

print("Sweta,Siwani,Tarang,Ckewyn,Chaitanya")

print("Press CTRL+C to exit")

client.loop_forever()

except:

2
print("Disconnectedfrom Broker")

client.disconnect()

Output:

3
Observation and Learning:

Observation:
1. Efficient Communication: MQTT (Message Queuing Telemetry Transport) proved
to be a robust and efficient protocol for implementing the Publish-Subscribe
architecture. It enabled seamless communication between devices while minimizing
network overhead.

2. Paho Library Ease: The Paho MQTT library, which provides client implementations
in various programming languages, demonstrated user-friendly APIs. This simplified
the process of connecting devices to the MQTT broker and handling publish-
subscribe interactions.

3. Mosquitto Broker Reliability: The Mosquitto MQTT broker displayed reliable


message routing and delivery. It managed multiple client connections effectively and
ensured that published messages were reliably delivered to subscribing clients.

4. Raspberry Pi Resource Utilization: The lightweight nature of the MQTT protocol


and the Paho library allowed the Raspberry Pi to handle numerous MQTT
connections without excessive resource consumption. This makes it suitable for IoT
applications.

Learning:
1. Topic Design Significance: Thoughtful topic design is crucial. Clear and organized
topic structures ensure that messages are appropriately categorized and reach their
intended audience. It's essential to plan topics that reflect the system's architecture and
the types of data being exchanged.

2. QoS Levels Impact: Quality of Service (QoS) levels affect message delivery
guarantees. QoS 0 offers no assurance of delivery, QoS 1 ensures at least one
delivery, and QoS 2 guarantees exactly-once delivery. Selecting the appropriate QoS
level depends on the application's needs and potential network constraints.

3. Client Lifecycle Management: Proper management of client connections is essential


to prevent memory leaks and ensure efficient resource utilization. Connecting and
disconnecting clients should be handled meticulously to maintain system stability.

4. Security Considerations: MQTT supports username-password authentication, but


additional security mechanisms like TLS/SSL should be implemented, especially in
sensitive applications. Security configurations play a vital role in safeguarding data
during transmission.

4
Conclusion:
In conclusion combination of MQTT's efficiency, Paho's user-friendly interfaces, and the
reliability of the Mosquitto Broker showcased the strength of this architecture for IoT and
other distributed applications. Thoughtful topic design, understanding QoS levels, managing
client lifecycles, and implementing appropriate security measures are all essential aspects for
creating a robust and successful system.

You might also like