Tech Gyan Set
Published © MIT

Smart Electricity Bill Saver + Real-Time Power Monitor

An IoT system that tracks real-time power usage and helps reduce your electricity bill. ⚡

BeginnerFull instructions provided8 hours892
Smart Electricity Bill Saver + Real-Time Power Monitor

Things used in this project

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

1️⃣ Required Libraries

C/C++
🔹 Purpose:
Enable WiFi connection, MQTT communication and energy monitoring.
/*
====================================================
1️⃣ LIBRARIES
WiFi, MQTT and PZEM Energy Monitor
====================================================
*/

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

🔟 Main Loop Logic

C/C++
🔹 Purpose:
Main intelligence of the system:
void loop() {

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

  client.loop();

  float voltage = pzem.voltage();
  float current = pzem.current();
  float power   = pzem.power();
  float energy  = pzem.energy();   // kWh

  if (isnan(voltage) || isnan(current)) {
    Serial.println("Error reading PZEM");
    return;
  }

  // Bill Calculation
  float estimatedBill = energy * electricityRate;

  // Publish to MQTT
  client.publish(topic_voltage, String(voltage).c_str());
  client.publish(topic_current, String(current).c_str());
  client.publish(topic_power, String(power).c_str());
  client.publish(topic_energy, String(energy).c_str());
  client.publish(topic_bill, String(estimatedBill).c_str());

  // Overload Protection
  if (current > overloadCurrent) {
    digitalWrite(RELAY_PIN, HIGH);
    relayState = false;
    client.publish(topic_alert, "Overload Detected - Power Cut");
  }

  delay(2000);
}

2️⃣ WiFi Configuration

C/C++
🔹 Purpose:
Connect ESP32 to the internet.
/*
====================================================
2️⃣ WIFI SETTINGS
Enter your WiFi credentials
====================================================
*/

const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASSWORD";

3️⃣ MQTT Configuration

C/C++
🔹 Purpose:
Define MQTT topics for real-time monitoring.
/*
====================================================
3️⃣ MQTT SETTINGS
Broker and topics
====================================================
*/

const char* mqtt_server = "broker.hivemq.com";
const int mqtt_port = 1883;

const char* topic_voltage = "power/voltage";
const char* topic_current = "power/current";
const char* topic_power   = "power/watt";
const char* topic_energy  = "power/energy";
const char* topic_bill    = "power/bill";
const char* topic_alert   = "power/alert";

4️⃣ Pin Configuration

C/C++
🔹 Purpose:
Define GPIO pins for hardware connection.
/*
====================================================
4️⃣ PIN DEFINITIONS
Relay and PZEM UART pins
====================================================
*/

#define RELAY_PIN 4

#define PZEM_RX 16
#define PZEM_TX 17

5️⃣ Object Initialization

C/C++
🔹 Purpose:
Initialize WiFi, MQTT and energy monitor objects.
WiFiClient espClient;
PubSubClient client(espClient);
HardwareSerial pzemSerial(2);
PZEM004Tv30 pzem(pzemSerial, PZEM_RX, PZEM_TX);

6️⃣ Protection & Billing Settings

C/C++
🔹 Purpose:
Define overload protection limit and electricity cost.
/*
====================================================
6️⃣ SYSTEM SETTINGS
Overload and billing rate
====================================================
*/

float overloadCurrent = 10.0;      // Overload limit in Ampere
float electricityRate = 6.0;       // ₹ per unit (kWh)

bool relayState = true;

7️⃣ WiFi Connect Function

C/C++
🔹 Purpose:
Connect ESP32 to WiFi network.
void setup_wifi() {

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
}

8️⃣ MQTT Reconnect Function

C/C++
🔹 Purpose:
Ensure stable MQTT connection.
void reconnect() {

  while (!client.connected()) {

    if (client.connect("PowerMonitorClient")) {
      Serial.println("MQTT Connected");
    } else {
      delay(2000);
    }
  }
}

9️⃣ Setup Function

C/C++
🔹 Purpose:
Initialize system components.
void setup() {

  Serial.begin(115200);

  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW);

  setup_wifi();

  client.setServer(mqtt_server, mqtt_port);
}

Credits

Tech Gyan Set
23 projects • 6 followers
Tech Gyan Set | IoT & Embedded Systems Creator | Arduino, ESP32, GSM & NodeMCU Projects | Smart Home & Real-Life Automation Tutorials
Thanks to Tech Gyan Set .

Comments