Zbotic Logo Zbotic Logo
  • Home
  • Shop
  • Sale
  • 3D Print Service
  • PCB Service
  • B2B
  • Blogs
  • Contact Us
0 0

View Wishlist Add all to cart

0 0
0 Shopping Cart
Shopping cart (0)
Subtotal: ₹0.00

View cartCheckout

  • Shop
  • About Us
  • Contact Us
  • Reseller
  • Blogs
020 69134444
1800 209 0998
[email protected]
Help Desk
Facebook Twitter Instagram Linkedin YouTube
Zbotic Logo Zbotic Logo
0 0

View Wishlist Add all to cart

0 0
0 Shopping Cart
Shopping cart (0)
Subtotal: ₹0.00

View cartCheckout

All departments
  • 3D Print Service
  • 3D Printer
  • Batteries & Chargers
  • Development Boards
  • Drone Parts
  • EBike parts
  • Sensor Modules
  • Electronic Components
  • Electronic Modules
  • IoT and Wireless
  • Mechanical Parts and Workbench Tools
  • Motors & Drivers & Pumps & Actuators
  • DIY and Robot Kits
  • Show more
  • Home
  • Shop
  • Sale
  • 3D Print Service
  • PCB Service
  • B2B
  • Blogs
  • Contact Us
Return to previous page
Home Home Automation & Smart Devices

MQTT for Home Automation: ESP32 + Mosquitto + Home Assistant

MQTT for Home Automation: ESP32 + Mosquitto + Home Assistant

April 1, 2026 /Posted by / 0

MQTT is the backbone protocol of professional home automation systems. If you are building a smart home with multiple ESP32 devices — switches, sensors, cameras, and controllers — MQTT provides the reliable, efficient communication layer that ties everything together. Combined with Mosquitto as the message broker and Home Assistant as the automation platform, MQTT gives you a local-first, privacy-respecting smart home that works even when the internet is down.

This comprehensive guide takes you from zero to a fully functional MQTT-based home automation system, with practical ESP32 code examples and Home Assistant configuration.

Table of Contents

  • What Is MQTT and Why Use It?
  • System Architecture
  • Setting Up Mosquitto Broker
  • ESP32 Publishing Sensor Data
  • ESP32 Subscribing to Commands
  • Home Assistant Integration
  • Creating Powerful Automations
  • Frequently Asked Questions
  • Conclusion

What Is MQTT and Why Use It?

MQTT (Message Queuing Telemetry Transport) is a lightweight publish/subscribe messaging protocol designed for IoT devices. Instead of each device talking directly to every other device (which becomes unmanageable with more than a few devices), all devices communicate through a central broker.

How It Works

  • Publish: An ESP32 temperature sensor publishes data to a topic like home/bedroom/temperature
  • Subscribe: Home Assistant subscribes to home/bedroom/temperature and receives the data
  • Broker: Mosquitto sits in the middle, receiving published messages and forwarding them to all subscribers

Why MQTT Over HTTP/WebSockets?

  • Minimal overhead: MQTT messages are tiny (2 bytes header minimum) compared to HTTP
  • Persistent connections: Devices maintain a single connection to the broker instead of opening new connections for each message
  • QoS levels: Guaranteed message delivery with QoS 1 and 2
  • Last Will: Automatic notification when a device goes offline
  • Retained messages: New subscribers immediately get the last known state
  • Scales effortlessly: Adding a new device requires zero changes to existing devices

System Architecture

A typical MQTT-based smart home in India looks like this:

┌─────────────┐    ┌──────────────┐    ┌─────────────┐
│  ESP32 #1   │    │   Mosquitto  │    │    Home     │
│ Bedroom     │───→│    Broker    │←───│  Assistant  │
│ Light+Temp  │←───│  (RPi/PC)   │───→│  Dashboard  │
└─────────────┘    └──────────────┘    └─────────────┘
                          ↕
┌─────────────┐    ┌──────────────┐
│  ESP32 #2   │    │  ESP32 #3    │
│ Living Room │    │  Water Tank  │
│ Fan+Humid   │    │  Level Sensor│
└─────────────┘    └──────────────┘

All ESP32 devices connect to the Mosquitto broker. Home Assistant also connects to the same broker. Commands flow from Home Assistant → Broker → ESP32, and sensor data flows from ESP32 → Broker → Home Assistant.

🛒 Recommended: ESP32 Development Board (38 Pin) — With built-in WiFi and plenty of GPIO pins, the ESP32 is the ideal MQTT client device for home automation nodes.

Setting Up Mosquitto Broker

Mosquitto is the most popular MQTT broker for home automation. Install it on a Raspberry Pi, old laptop, or any always-on computer on your home network.

Installation on Raspberry Pi / Ubuntu

sudo apt update
sudo apt install mosquitto mosquitto-clients

# Create a password file for authentication
sudo mosquitto_passwd -c /etc/mosquitto/passwd homeuser
# Enter password when prompted

# Configure Mosquitto
sudo nano /etc/mosquitto/conf.d/default.conf

Add to the configuration file:

listener 1883
allow_anonymous false
password_file /etc/mosquitto/passwd
# Restart Mosquitto
sudo systemctl restart mosquitto
sudo systemctl enable mosquitto

# Test with command-line client
# Terminal 1 (subscribe):
mosquitto_sub -h localhost -u homeuser -P yourpassword -t "test/topic"

# Terminal 2 (publish):
mosquitto_pub -h localhost -u homeuser -P yourpassword -t "test/topic" -m "Hello MQTT!"

ESP32 Publishing Sensor Data

This ESP32 reads a DHT22 sensor and publishes temperature and humidity to MQTT topics every 30 seconds:

#include <WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>
#include <ArduinoJson.h>

// WiFi
const char* ssid = "Your_WiFi";
const char* password = "Your_Password";

// MQTT Broker
const char* mqtt_server = "192.168.1.100"; // Your broker IP
const int mqtt_port = 1883;
const char* mqtt_user = "homeuser";
const char* mqtt_password = "yourpassword";

// Topics
const char* topic_temp = "home/bedroom/temperature";
const char* topic_humid = "home/bedroom/humidity";
const char* topic_status = "home/bedroom/status";
const char* topic_cmd = "home/bedroom/relay/set";
const char* topic_relay_state = "home/bedroom/relay/state";

// Hardware
#define DHT_PIN 4
#define RELAY_PIN 26

DHT dht(DHT_PIN, DHT22);
WiFiClient espClient;
PubSubClient mqtt(espClient);

unsigned long lastPublish = 0;
#define PUBLISH_INTERVAL 30000 // 30 seconds

void connectWiFi() {
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("
WiFi connected: " + WiFi.localIP().toString());
}

void connectMQTT() {
  while (!mqtt.connected()) {
    Serial.print("Connecting MQTT...");
    
    // Client ID should be unique
    String clientId = "ESP32-Bedroom-" + String(random(0xffff), HEX);
    
    // Last Will message - sent automatically if device disconnects
    if (mqtt.connect(clientId.c_str(), mqtt_user, mqtt_password,
                     topic_status, 1, true, "offline")) {
      Serial.println("connected!");
      
      // Publish online status (retained)
      mqtt.publish(topic_status, "online", true);
      
      // Subscribe to command topic
      mqtt.subscribe(topic_cmd);
      
    } else {
      Serial.print("failed, rc=");
      Serial.println(mqtt.state());
      delay(5000);
    }
  }
}

// Called when a message arrives on subscribed topics
void mqttCallback(char* topic, byte* payload, unsigned int length) {
  String message;
  for (int i = 0; i  PUBLISH_INTERVAL) {
    publishSensorData();
    lastPublish = millis();
  }
}
🛒 Recommended: DHT22 Temperature and Humidity Sensor — Reliable environmental sensor for publishing temperature and humidity data via MQTT to your Home Assistant dashboard.

ESP32 Subscribing to Commands

The code above already includes subscription handling in the mqttCallback function. When Home Assistant publishes “ON” or “OFF” to home/bedroom/relay/set, the ESP32 receives the message and controls the relay accordingly.

MQTT Topic Structure Best Practices

home/
├── bedroom/
│   ├── temperature    (sensor → HA)
│   ├── humidity       (sensor → HA)
│   ├── relay/
│   │   ├── set        (HA → device, command)
│   │   └── state      (device → HA, current state)
│   └── status         (online/offline)
├── living_room/
│   ├── temperature
│   ├── fan/
│   │   ├── set
│   │   └── state
│   └── status
└── water_tank/
    ├── level
    ├── pump/
    │   ├── set
    │   └── state
    └── status

Home Assistant Integration

Home Assistant natively supports MQTT. Add your ESP32 devices to the configuration.yaml file:

# configuration.yaml

mqtt:
  sensor:
    - name: "Bedroom Temperature"
      state_topic: "home/bedroom/temperature"
      unit_of_measurement: "°C"
      device_class: temperature
      
    - name: "Bedroom Humidity"
      state_topic: "home/bedroom/humidity"
      unit_of_measurement: "%"
      device_class: humidity
  
  switch:
    - name: "Bedroom Light"
      state_topic: "home/bedroom/relay/state"
      command_topic: "home/bedroom/relay/set"
      payload_on: "ON"
      payload_off: "OFF"
      state_on: "ON"
      state_off: "OFF"
      
  binary_sensor:
    - name: "Bedroom Controller"
      state_topic: "home/bedroom/status"
      payload_on: "online"
      payload_off: "offline"
      device_class: connectivity

After restarting Home Assistant, your ESP32 devices appear on the dashboard with real-time data and switch controls.

🛒 Recommended: 4 Channel 5V Relay Module — Control 4 appliances per room via MQTT commands from Home Assistant. Each channel gets its own MQTT topic for independent control.

Creating Powerful Automations

Home Assistant automations use MQTT sensor data to trigger actions across your entire home:

Example 1: Temperature-Based Fan Control

automation:
  - alias: "Turn on fan when hot"
    trigger:
      platform: numeric_state
      entity_id: sensor.bedroom_temperature
      above: 30
    action:
      service: switch.turn_on
      entity_id: switch.bedroom_fan

  - alias: "Turn off fan when cool"
    trigger:
      platform: numeric_state
      entity_id: sensor.bedroom_temperature
      below: 25
    action:
      service: switch.turn_off
      entity_id: switch.bedroom_fan

Example 2: Sunset-Based Lighting

  - alias: "Porch light at sunset"
    trigger:
      platform: sun
      event: sunset
      offset: "+00:15:00"
    action:
      service: switch.turn_on
      entity_id: switch.porch_light

  - alias: "Porch light off at night"
    trigger:
      platform: time
      at: "23:00:00"
    action:
      service: switch.turn_off
      entity_id: switch.porch_light

Example 3: Presence-Based All-Off

  - alias: "All off when leaving"
    trigger:
      platform: state
      entity_id: device_tracker.jayesh_phone
      to: "not_home"
      for: "00:05:00"
    action:
      - service: switch.turn_off
        entity_id:
          - switch.bedroom_light
          - switch.living_room_light
          - switch.kitchen_light
      - service: notify.mobile_app
        data:
          message: "All lights turned off. You left home."
🛒 Recommended: BME280 Environmental Sensor — Measures temperature, humidity, and barometric pressure in one sensor. Publish all three values via MQTT for comprehensive room monitoring.

Frequently Asked Questions

What happens if the Mosquitto broker goes down?

ESP32 devices lose their MQTT connection and cannot receive commands from Home Assistant. However, physical buttons (if implemented) continue to work. The PubSubClient library automatically attempts to reconnect. When the broker comes back, all devices reconnect within seconds and the Last Will messages update their status.

How many devices can Mosquitto handle?

A Raspberry Pi 4 running Mosquitto can easily handle 100+ connected devices with thousands of messages per minute. For a typical home with 10–20 ESP32 nodes, a Raspberry Pi 3 is more than sufficient.

Is MQTT secure?

Basic MQTT uses plaintext. For security: (1) Use username/password authentication (as shown above), (2) Restrict Mosquitto to listen only on the local network interface, (3) For internet access, use MQTT over TLS (port 8883). For a home network behind a router, username/password is usually sufficient.

Can I use MQTT without Home Assistant?

Absolutely. You can build a complete MQTT system with just Mosquitto and ESP32 devices — each device can subscribe to commands from other devices directly. Home Assistant adds a nice dashboard and automation engine, but is not required.

What is the difference between QoS 0, 1, and 2?

QoS 0: Fire and forget (fastest, may lose messages). QoS 1: At least once delivery (recommended for most home automation). QoS 2: Exactly once delivery (slowest, rarely needed). Use QoS 1 for switch commands and QoS 0 for frequent sensor readings.

Conclusion

MQTT is the protocol that transforms a collection of individual smart devices into a cohesive, responsive smart home system. Its lightweight design is perfect for ESP32 microcontrollers, and its publish/subscribe model makes adding new devices trivially easy. Combined with Mosquitto and Home Assistant, you get a professional-grade automation platform that runs entirely on your local network — no cloud, no subscription fees, no privacy concerns.

Build your MQTT-powered smart home with components from Zbotic.in. Get ESP32 boards, relay modules, and sensors — everything you need for a complete home automation ecosystem, delivered across India.

Tags: ESP32, home assistant, iot, MQTT, smart home
Share Post
  • Facebook
  • Linkedin
  • Whatsapp
Inverter Basics: Modified Sine...
blog inverter basics modified sine wave vs pure sine wave india 612683
blog pcb holder and third hand best options for soldering work 612689
PCB Holder and Third Hand: Bes...

Related posts

Svg%3E
Read more

Curtain and Blind Automation: Stepper Motor Controller

April 1, 2026 0
Curtain automation is one of the most satisfying smart home upgrades you can build. Imagine your curtains opening automatically at... Continue reading
Svg%3E
Read more

Smart Doorbell with Camera: ESP32-CAM Video Intercom

April 1, 2026 0
A smart doorbell with a camera lets you see who is at your door from your phone, even when you... Continue reading
Svg%3E
Read more

Blynk IoT Platform: Control Arduino and ESP32 from Mobile

April 1, 2026 0
The Blynk IoT platform is the fastest way to control your Arduino and ESP32 projects from your mobile phone. Instead... Continue reading
Svg%3E
Read more

PIR Sensor Automatic Light: Save Electricity with Motion Detection

April 1, 2026 0
PIR sensor automatic lights are the simplest and most effective way to save electricity in Indian homes. By automatically turning... Continue reading
Svg%3E
Read more

4-Channel Relay Module Guide: Wiring, Safety, and Projects

April 1, 2026 0
The 4-channel relay module is the single most important component in home automation projects. It is the bridge that allows... Continue reading

Add comment Cancel reply

Your email address will not be published. Required fields are marked

Facebook Twitter Instagram Pinterest Linkedin Youtube

Get the latest deals and more.

Download on Google Play Download on the App Store

Call us: 020 69134444 / 1800 209 0998

Monday - Saturday 09:30 AM - 06:00 PM
For Technical Supports Email: [email protected]
For Sales / Enquiries Email: [email protected]

  • My Account

    • Cart

    • Wishlist

    • Checkout

    • My Orders

    • Track Order

    • My Account

  • Information

    • FAQs

    • Blogs

    • Career

    • About Us

    • Contact Us

    • Payment Options

  • Policies

    • Privacy Policy

    • Terms & Conditions

    • GST Input Tax Credit

    • Shipping Return Policy

    • E-Waste Collection Points

    • Our Sitemap

© Zbotic.in is registered trademark of Moxie Supply Pvt Ltd – All Rights Reserved
Login
Use Phone Number
Use Email Address
Not a member yet? Register Now
Reset Password
Use Phone Number
Use Email Address
Register
Already a member? Login Now