DIY Smart Home Energy Monitor with ESP32 and Home Assistant

 

Introduction

Managing energy consumption is a great way to reduce electricity costs and contribute to environmental sustainability. With the help of IoT, you can monitor energy usage in real-time, right from your smartphone. In this guide, we’ll build a Smart Home Energy Monitor using an ESP32 microcontrollercurrent and voltage sensors (like the ACS712 or SCT-013), and the MQTT protocol for data transmission. This data will be accessible on a mobile app via Home Assistant, allowing you to keep track of energy usage and optimize it effectively.

Project Overview

Objectives

  • Monitor power consumption in real-time for home appliances.
  • Display energy usage on a mobile app using MQTT and Home Assistant.
  • Analyze data over time to make informed decisions about energy usage.

Key Components

  • ESP32 (or ESP8266) microcontroller: For reading sensor data and connecting to Wi-Fi.
  • Current Sensor (SCT-013 or ACS712): For measuring current drawn by appliances.
  • Voltage Sensor: Optional, but adds more accuracy if you want to measure precise power usage.
  • MQTT Protocol: To send data to Home Assistant for real-time monitoring.
  • Home Assistant: A home automation platform to display and analyze data.

Part 1: Setting Up the Components

1. ESP32 or ESP8266 Microcontroller

  • Choose either the ESP32 or ESP8266 microcontroller. The ESP32 has more features and is preferred, but either will work.
  • Connect the ESP32 to your computer using a USB cable to program it.

2. Current Sensor (SCT-013 or ACS712)

  • SCT-013 is a non-invasive sensor that clamps around a wire to measure the AC current flowing through it.
  • ACS712 is a current sensor that can measure both AC and DC current but requires direct connection to the wire, so exercise caution with high voltage.

Wiring for SCT-013

  • Connect the SCT-013 sensor’s output to an analog input pin on the ESP32.
  • If you’re using SCT-013 with ESP8266, you’ll need an analog-to-digital converter (ADC) module since the ESP8266 has only one analog input pin with limited resolution.

Wiring for ACS712

  • Connect the VCC pin to the 3.3V or 5V pin on the ESP32.
  • Connect the OUT pin to an analog input pin on the ESP32.
  • Connect the GND pin to the ground (GND) on the ESP32.

3. Voltage Sensor (Optional)

  • A voltage sensor can be added to measure the actual voltage if you want to calculate power more accurately.
  • Connect the sensor’s VCC to the 3.3V on ESP32, GND to ground, and OUT to an analog input.

Part 2: Coding the ESP32 for Data Acquisition and Transmission

1. Install the Required Libraries

Make sure you have the following libraries installed in your Arduino IDE:

  • ESP32 or ESP8266 board support (depending on your microcontroller)
  • PubSubClient library for MQTT communication
  • WiFi library for connecting to your Wi-Fi network

2. Set Up the Code

Here’s the code to:

  1. Read values from the current sensor.
  2. Calculate power consumption (voltage x current if you have a voltage sensor, or assume constant voltage).
  3. Publish data to an MQTT broker.

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

// Wi-Fi and MQTT Broker Settings
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
const char* mqtt_server = "YOUR_MQTT_BROKER_IP";

// MQTT topics
const char* topicPower = "home/energy_monitor/power";

WiFiClient espClient;
PubSubClient client(espClient);

// Sensor parameters
const int sensorPin = 34; // Analog pin for sensor (ESP32)
const float voltageCalibration = 230.0; // Voltage in volts (modify as per your region)
const float sensorCalibration = 0.185; // Calibration constant for ACS712 sensor

void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
}

void setup_wifi() {
delay(10);
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);

Serial.print(".");
}
Serial.println("Connected to WiFi.");
}

void reconnect() {
while (!client.connected()) {
Serial.print("Connecting to MQTT...");
if (client.connect("ESP32Client")) {
Serial.println("Connected.");
} else {
delay(5000);
}
}
}

void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();

// Read sensor data
int rawValue = analogRead(sensorPin);
float current = rawValue * sensorCalibration; // Adjust based on sensor

// Calculate power
float power = voltageCalibration * current; // Simple power calculation (P=VI)

// Publish data to MQTT
String powerStr = String(power);
client.publish(topicPower, powerStr.c_str());

Serial.print("Power: ");
Serial.println(power);

delay(2000); // Send data every 2 seconds
}

3. Upload Code

  1. Connect your ESP32 to your computer.
  2. Select the correct board and port in the Arduino IDE.
  3. Upload the code to the ESP32.

Part 3: Setting Up Home Assistant and MQTT Broker

1. Set Up MQTT Broker

If you don’t have an MQTT broker, you can use Mosquitto on your local network or use an online MQTT service like HiveMQ.

  • Install Mosquitto on a Raspberry Pi or a computer running Home Assistant.
  • Configure Mosquitto by setting a username and password for secure access.

2. Configure Home Assistant

In Home Assistant, you’ll add the MQTT integration to receive data from the ESP32.

  • Go to Settings > Integrations and search for MQTT.
  • Enter your MQTT broker details and save the configuration.

3. Add a Sensor in Home Assistant

In your Home Assistant configuration file (configuration.yaml), add the following entry to display power data:

sensor:
- platform: mqtt
name: "Home Power Consumption"
state_topic: "home/energy_monitor/power"
unit_of_measurement: "W"
value_template: "{{ value | float }}"

  • Restart Home Assistant to apply changes.
  • You should now see a Home Power Consumption sensor in your Home Assistant dashboard.

Part 4: Testing and Using the Energy Monitor

  1. Power on your ESP32 and ensure it’s connected to Wi-Fi.
  2. Open Home Assistant, where you should see the real-time data of power consumption.
  3. Monitor the data over time and optimize energy usage based on the data.

Conclusion

With this DIY Smart Home Energy Monitor, you can track power usage across various appliances in your home. This project introduces core IoT concepts such as data acquisition, MQTT communication, and integration with Home Assistant. You can further expand this setup by adding more sensors or even automating appliances based on usage patterns.

#Tech4bizsolutions #DIYSmartHome #HomeEnergyMonitor #ESP32Projects #HomeAssistantIntegration #IoTProjects
#EnergyEfficiency #SmartHomeAutomation
#TechDIY #OpenSourceElectronics #HomeEnergyManagement #SmartHomeTips #ESP32Development #IoTDevices 


Comments

Popular posts from this blog

Micro-Influencers + AI: The New Power Duo for Affiliate Success in 2025

Unlocking the Power of Xilinx FPGAs: A Comprehensive Guide to Architecture, Series, and Implementation

Redefining Sales: How Generative AI is Empowering SDR Teams to Close Deals Faster