An MQTT dashboard for smart home using HiveMQ and Grafana transforms raw sensor data into beautiful real-time visualisations. Whether you are monitoring room temperatures across your Bengaluru apartment or tracking energy consumption in your Delhi bungalow, this tutorial walks you through building a professional IoT monitoring stack entirely free using open-source tools and Indian-available hardware.
Table of Contents
- What is MQTT and Why Use It?
- Setting Up HiveMQ Cloud Broker (Free Tier)
- ESP8266/ESP32 Firmware for MQTT Publishing
- Telegraf and InfluxDB for Data Storage
- Building Your Grafana Dashboard
- Panels, Alerts, and Notifications
- India-Specific Tips
- Frequently Asked Questions
What is MQTT and Why Use It?
MQTT (Message Queuing Telemetry Transport) is a lightweight publish-subscribe protocol designed for constrained IoT devices. For Indian homes with intermittent power and occasional WiFi drops, MQTT’s QoS levels and retained messages ensure data is never permanently lost. Key advantages: low bandwidth (a temperature reading costs ~50 bytes, negligible on Jio), persistent sessions, fan-out to 50 subscribers, and native support in Home Assistant, Node-RED, Tasmota, and ESPHome.
Recommended: UNO WiFi R3 (ATmega328P + ESP8266)
The UNO WiFi R3 combines ATmega328P with ESP8266 in one board, perfect for publishing MQTT sensor data over WiFi without separate shields. Ideal for first-time MQTT projects.
Setting Up HiveMQ Cloud Broker (Free Tier)
HiveMQ Cloud offers a free tier supporting up to 100 connected devices and 10 GB monthly data. Register at console.hivemq.cloud, create a cluster in AWS ap-south-1 (Mumbai) for lowest latency from India, and note your Cluster URL. Add credentials under Access Management.
# Test from Linux
mosquitto_sub -h YOUR_CLUSTER.s1.eu.hivemq.cloud -p 8883
--capath /etc/ssl/certs/ -u zbotic_home -P yourpassword
-t "home/#"
mosquitto_pub -h YOUR_CLUSTER.s1.eu.hivemq.cloud -p 8883
--capath /etc/ssl/certs/ -u zbotic_home -P yourpassword
-t "home/bedroom/temperature" -m "28.5"
ESP8266/ESP32 Firmware for MQTT Publishing
Install PubSubClient library in Arduino IDE. Use WiFiClientSecure for TLS connections to HiveMQ.
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
#include <DHT.h>
const char* mqttHost = "YOUR_CLUSTER.s1.eu.hivemq.cloud";
const int mqttPort = 8883;
DHT dht(D4, DHT22);
WiFiClientSecure espClient;
PubSubClient client(espClient);
void loop() {
if (!client.connected()) reconnect();
client.loop();
static unsigned long lastMsg = 0;
if (millis() - lastMsg > 30000) {
lastMsg = millis();
float t = dht.readTemperature();
char buf[8]; dtostrf(t, 4, 1, buf);
client.publish("home/bedroom/temperature", buf, true);
}
}
Recommended: Mega WiFi R3 (ATmega2560 + ESP8266)
The Mega WiFi R3 with 54 I/O pins handles complex homes with many sensors. Publish temperature, humidity, PIR, and power data simultaneously over MQTT.
Telegraf and InfluxDB for Data Storage
Grafana visualises data but does not store it. Use InfluxDB 2.x with Telegraf’s MQTT consumer plugin:
sudo apt update && sudo apt install influxdb2 telegraf
sudo systemctl enable --now influxdb
Telegraf config (/etc/telegraf/telegraf.conf):
[[inputs.mqtt_consumer]]
servers = ["ssl://YOUR_CLUSTER.s1.eu.hivemq.cloud:8883"]
topics = ["home/#"]
username = "zbotic_home"
password = "yourpassword"
data_format = "value"
data_type = "float"
[[outputs.influxdb_v2]]
urls = ["http://localhost:8086"]
token = "YOUR_INFLUXDB_TOKEN"
org = "zbotic"
bucket = "smarthome"
Building Your Grafana Dashboard
sudo apt install grafana
sudo systemctl enable --now grafana-server
Access at http://localhost:3000. Add InfluxDB as a data source (Flux query language). Create panels with Flux queries:
from(bucket: "smarthome")
|> range(start: -24h)
|> filter(fn: (r) => r.topic == "home/bedroom/temperature")
|> aggregateWindow(every: 5m, fn: mean)
Panels, Alerts, and Notifications
Recommended panels for an Indian home:
- Gauge: Current room temperature (alert if >35 degrees C during Indian summer)
- Time series: 24h humidity trend for monsoon season (June-September)
- Stat panel: Uptime of each ESP node
- Bar gauge: Daily energy consumption in kWh (from PZEM-004T power meter)
Set up Telegram or Email alerts when a sensor goes offline or temperature spikes.
Recommended: 12V 1-Channel Relay Module (RS485/Modbus)
Integrate this RS485/Modbus relay module with your MQTT setup to control appliances based on Grafana thresholds, such as auto-switching on a cooler when temperature exceeds 35 degrees C.
India-Specific Tips
- Power cuts: Use QoS 1 on all MQTT publishes so messages are re-sent after reconnect.
- IST timezone: Set Grafana timezone to Asia/Kolkata under Dashboard Settings.
- Starter kit cost (2025): ESP8266 NodeMCU Rs 180, DHT22 Rs 120, Raspberry Pi 4 2GB Rs 4,500 – total under Rs 5,000.
- Cloudflare Tunnel: Use for secure remote Grafana access without port-forwarding your home router.
Frequently Asked Questions
- Can I use a local Mosquitto broker instead of HiveMQ Cloud?
- Yes. Install Mosquitto on your Raspberry Pi with
sudo apt install mosquittoand point your ESP firmware to the Pi’s local IP. Works offline without internet. - Is HiveMQ free tier enough for a full home?
- For most Indian homes (5-10 sensors, 1-minute intervals), yes. A single ESP8266 publishing 4 topics every 30 seconds uses roughly 5MB/month, well within the 10GB free limit.
- How do I secure my MQTT broker?
- Always use TLS (port 8883), strong passwords, and per-device credentials. Never expose MQTT port 1883 (plaintext) to the internet.
- Can I trigger relays based on Grafana alerts?
- Not directly. Use Node-RED as the automation bridge: subscribe to MQTT, evaluate sensor conditions, and publish relay commands back to the ESP.
- What database is best for long-term storage?
- InfluxDB for time-series data (sensor readings). For event logs (door open/close history), use SQLite or PostgreSQL alongside InfluxDB.
Add comment