Practical IoT (Internet of Things)
Lab Report
Lab 04
Instructor: Dr. Naveed Akhtar
Name Registration No.
Eshal Ali 03-3-1-014-2022
Hadiqa Nadeem 03-3-1-017-2022
Hanana Asif 03-3-1-021-2022
Muhammad Ahsan Ali 03-3-1-029-2022
March 27, 2025
2
Lab 04: Using MQTT Protocol in IoT
Objective
• Understand the fundamentals of MQTT—a lightweight messaging protocol designed for IoT.
• Learn the publish/subscribe communication pattern.
• Configure ESP32 / WeMos D1 Mini (ESP8266) to connect to a public MQTT broker (e.g.,
test.mosquitto.org / broker.hivemq.com / iot.intellihouse.com.pk).
• Publish sensor/button data to a topic (Collaborate with another group)
• Subscribe to a topic and control actuators based on received data (Collaborate with another
group).
• Implement code that publishes messages to a topic and subscribes to another topic to receive
messages (single device as publisher and subscriber).
• Gain hands-on experience with IoT messaging, debugging, and integrating sensor/actuator control
through MQTT.
Task 1: Setting up the environment
Step 1: Install Required Libraries
Step 2: Configure Your WiFi Credentials
These credentials were used for the lab
• SSID: "Ahsan Ali";
• Password: "ahsan2030";
3
Step 3: Install Client Software for visualization (Optional)
MQTT explorer was used for visualization with the server test.mosquito.org.
Task 2: Configure the MQTT Publisher (Button)
4
Arduino Sketch
5
Code Explanation
• This is the code for MQTT publisher. It publishes the code on public MQTT Broker (MQTT Explorer)
through test.mosquito.org server on port 1833
• The ESP 8266 tries to connect to the WiFi Network; Ahsan Ali in this case. When connected, “WiFi
Connected” is printed on Serial Monitor
• The code ensures that we are always connected to the MQTT Server.
• Initially, the button state is low. When the button is pressed, it turns HIGH. The code checks if the
button state changes. When it changes, it prints the respective message to the MQTT Broker.
Fritzing Schematic
6
Breadboard setup
Connections
Push Button
1. One Side to 3V3 through a Pull-Down Resistor
2. Other Side to D2 of ESP8266.
Pull-Down Resistor
Connected between D2 and GND. The pin reads LOW when the button is not pressed
Working
• When button is pressed, D2 gets HIGH.
• When the button is not pressed, D2 remains LOW.
Serial Monitor
7
Message Published on MQTT broker
In Additional Settings, we added “pieas/#” in the topics to filter the messages shown to show only
messages published with this topic
Task 3: Configure the MQTT Subscriber (LED)
Arduino Sketch
8
9
Code Explanation
• Similar to the publisher setup,it connects to the WiFi and MQTT broker.
• After connecting to the broker, it subscribes to the topic “pieas/iotlab/4/group/1/button”.
• It checks when a message for the respective topic is published and turns the LED on or off
depending on the msg published
Breadboard Setup
Working
• As a functional LED was not available, we use the built in LED on ESP8266 using GPIO D4
• When the button is pressed, the LED turns off, the rest of the time it remains ON.
10
Task 4: Both Publisher and Subscriber on Single Device
Arduino Sketch
11
Code Explanation
• This code combines the functionaly of the publisher and subscriber codes.
• It checks for the button state and publishes its On/OFF message based on its state.
• The same code checks for any published messages after subscribing to the topic.
• It turns the LED ON?OFF based on the published message.
12
Serial Monitor
Message Published
In Additional Settings, we added “pieas/#” in the topics to filter the messages shown to show only
messages published with this topic
13
Breadboard Setup
Working
The same ESP 8266 acts as the publisher, as well as the subscriber. Depending on the state of button
pin, it publishes the respective message. After subscribing to the topic “pieas/iotlab/4/group/1/button” ,
it turns the LED ON/OFF, depending on the message published on this topic.