Esp32 Mqtt Sensor Cloud — this detailed tutorial covers everything from hardware selection to complete working code, designed for Indian makers, students, and engineers.
Table of Contents
- Understanding MQTT Protocol for IoT
- Setting Up MQTT on ESP32
- Sending JSON Sensor Data
- Free Cloud Dashboards for MQTT Data
- QoS Levels and Security
- Setting Up a Local Mosquitto Broker
- Frequently Asked Questions
Understanding MQTT Protocol for IoT
MQTT (Message Queuing Telemetry Transport) is the de facto standard for IoT communication. It uses a publish/subscribe model where devices publish messages to topics, and interested clients subscribe to those topics. An MQTT broker (server) routes messages between publishers and subscribers. MQTT is lightweight — a minimal packet is just 2 bytes — making it perfect for ESP32 and ESP8266 devices with limited bandwidth. Popular free brokers include Mosquitto (self-hosted), HiveMQ Cloud (free tier: 100 connections), and EMQX Cloud.
Setting Up MQTT on ESP32
Install the PubSubClient library from Arduino IDE Library Manager:
#include <WiFi.h>
#include <PubSubClient.h>
const char* ssid = "Your_WiFi";
const char* password = "Your_Password";
const char* mqtt_server = "broker.hivemq.com";
WiFiClient espClient;
PubSubClient client(espClient);
void reconnect() {{
while (!client.connected()) {{
if (client.connect("ESP32_Zbotic")) {{
client.subscribe("zbotic/commands");
}} else {{
delay(5000);
}}
}}
}}
void callback(char* topic, byte* payload, unsigned int length) {{
String message;
for (int i = 0; i < length; i++) message += (char)payload[i];
Serial.printf("Topic: %s Message: %sn", topic, message.c_str());
}}
void setup() {{
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) delay(500);
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}}
void loop() {{
if (!client.connected()) reconnect();
client.loop();
static unsigned long lastMsg = 0;
if (millis() - lastMsg > 5000) {{
lastMsg = millis();
client.publish("zbotic/temperature", "28.5");
}}
}}
Sending JSON Sensor Data
Real-world IoT applications send structured JSON payloads. Use ArduinoJson library to build clean JSON messages with sensor readings, device ID, timestamp, and status fields. Subscribe to command topics to receive instructions from the dashboard — like changing sampling rate or toggling outputs.
Free Cloud Dashboards for MQTT Data
Several free options exist for visualising MQTT data:
- Node-RED Dashboard: Self-hosted, drag-and-drop flow editor with built-in MQTT nodes and dashboard widgets
- ThingsBoard Community Edition: Open-source IoT platform with device management and dashboards
- Grafana + InfluxDB: Store MQTT data in InfluxDB via Telegraf, visualise in Grafana (free self-hosted)
- MQTT Explorer: Desktop app for debugging MQTT topics during development
QoS Levels and Security
MQTT offers three QoS (Quality of Service) levels: QoS 0 (at most once, fire-and-forget), QoS 1 (at least once, may duplicate), and QoS 2 (exactly once, highest overhead). For sensor data, QoS 0 is usually sufficient. For critical commands (like turning off a motor), use QoS 1. Always use TLS encryption (port 8883) for production deployments. Free brokers like HiveMQ Cloud support TLS by default.
Setting Up a Local Mosquitto Broker
For privacy-sensitive applications, run your own MQTT broker on a Raspberry Pi or old laptop. Install Mosquitto on Ubuntu with sudo apt install mosquitto mosquitto-clients. Configure authentication and TLS for security. A Raspberry Pi 4 can handle thousands of connected ESP32 devices on a local network.
Frequently Asked Questions
Is MQTT free to use?
The MQTT protocol itself is open and free. Brokers can be self-hosted (Mosquitto, free) or cloud-hosted (HiveMQ free tier: 100 devices, EMQX free tier: 1M messages/month). For small Indian IoT projects, free tiers are usually sufficient.
Can ESP32 use MQTT over cellular/GSM?
Yes, if you add a SIM800L or SIM7600 module for cellular connectivity. The MQTT library works over any TCP connection, regardless of whether it is WiFi, Ethernet, or cellular.
What is the difference between MQTT and HTTP for IoT?
MQTT is bidirectional, lightweight, and maintains a persistent connection — ideal for real-time control. HTTP is request-response only, heavier, and creates a new connection each time. Use MQTT for live dashboards and control, HTTP for periodic data uploads to APIs.
Conclusion
The ESP32 platform continues to impress with its versatility and value for money. This project demonstrates just one of hundreds of applications possible with affordable microcontrollers. Indian makers have a unique advantage — access to affordable components, a growing community, and endless real-world problems to solve with technology. Whether you are building this for learning, a college project, or a commercial product prototype, the fundamentals covered here will serve you well.
Ready to start building? Browse our ESP32 development boards at Zbotic for the best prices in India with fast shipping!
Add comment