What it shows
MQTT is the light-weight messaging method behind many IoT systems. A sensor node publishes readings under named topics, and any subscriber, such as a laptop or a phone, receives them through a broker.
The science
MQTT works on a publish and subscribe model. The broker is a post office that receives messages sent to a topic such as home/room1/temperature and passes them to every client subscribed to that topic. The ESP32 needs only to connect to the broker and send small text messages, so the sender and the receivers do not need to know each other. The LDR with the 10 kΩ resistor forms a voltage divider read by the ESP32 analog input.
Materials
Cost: under Rs 1,500. Many items come from home or from waste; spare parts such as LEDs, motors and wire are in the store.
- An ESP32 development board
- A DHT11 temperature and humidity sensor
- An LDR and a 10 kΩ resistor
- A laptop running the free Mosquitto broker
- A home Wi-Fi network
- A breadboard and jumper wires
- The free MQTT Explorer app or mosquitto_sub
Safety
For this project: USB power only. Run the broker only on your home or school network and do not expose it to the internet. Do not publish your Wi-Fi password in shared code.
Never use mains electricity: power models with cells, batteries or a low-voltage adapter. Sharp and hot tools, flames and lab chemicals need an adult or your teacher beside you. Keep magnets, button cells and small parts away from young children, and wash your hands after making.
All the safety rulesStep by step
8 steps. Do them in order, and test each part before you go on to the next. Write what you notice in a notebook as you go.
Install Mosquitto on the laptop and allow it through the firewall for the home network only; note the laptop IP address.
Connect the DHT11 data pin to GPIO 4 and the LDR divider midpoint to GPIO 34, with the sensor power from 3V3.
Install the PubSubClient and DHT libraries in the Arduino IDE.
Write the sketch to connect to Wi-Fi, then to the broker at the laptop IP on port 1883.
Every 10 seconds read temperature, humidity and light and publish each under its own topic, for example home/room1/temp.
Open MQTT Explorer on the laptop and subscribe to home/room1/# to see all the values arrive.
Reconnect automatically if the Wi-Fi or broker connection is lost, using a function called in loop.
Cover the LDR and warm the DHT11 with a hand and watch the values change in the explorer.
What you should see
Temperature, humidity and light readings appear every ten seconds on the laptop under their topics and react to changes in the room.
Viva questions
A teacher or a judge will ask how it works. Try to answer each question before you open it, then compare.
1What is a broker?
A server that receives MQTT messages and forwards them to subscribers.
2What does the # sign mean in a topic?
It subscribes to every topic below that level.
3Why is MQTT used in IoT?
Its messages are small and it works well on weak networks and small devices.
4What are the security risks of an open broker?
Anyone on the network could read or publish messages, so use passwords and keep it off the internet.
Take it further
Make a second ESP32 subscribe to the light topic and turn an LED on when the room gets dark.
Change one thing, measure what happens, and add a table or chart of your readings: that is what turns a model into your own investigation.
Free ebooks for this project
Hardware & repair
Computer Networking Study Book
The complete study notes, practice questions and glossary for Computer Networking
