Yarana Iot Guru
Published © MIT

Smart Energy Bill Optimizer & Real-Time Power Monitor

An IoT device that tracks real-time power and reduces electricity bills. ⚡

BeginnerFull instructions provided8 hours1,119
Smart Energy Bill Optimizer & Real-Time Power Monitor

Things used in this project

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Smart_Energy_Monitor_ESP32

C/C++
/*
====================================================
SMART ENERGY MONITORING SYSTEM USING ESP32 + PZEM

This code reads Voltage, Current, Power and Energy
from PZEM-004T sensor using ESP32.

Features:
- Calculates electricity bill (based on unit rate)
- Sends real-time data to MQTT broker
- Provides overload protection using relay
- Automatically cuts power if current exceeds limit
- Publishes system status and alerts via MQTT

Use Case:
Home / Industry Power Monitoring System
IoT Based Energy Meter with Remote Monitoring
====================================================
*/
#include <WiFi.h>
#include <PubSubClient.h>
#include <PZEM004Tv30.h>

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

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

#define RELAY_PIN 4
#define PZEM_RX 16
#define PZEM_TX 17

float overloadCurrent = 10.0;
float electricityRate = 6.0;
bool relayState = true;

WiFiClient espClient;
PubSubClient client(espClient);
HardwareSerial pzemSerial(2);
PZEM004Tv30 pzem(pzemSerial, PZEM_RX, PZEM_TX);

void connectWiFi() {
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) delay(500);
}

void connectMQTT() {
  while (!client.connected()) {
    client.connect("PowerMonitorClient");
  }
}

void setup() {
  Serial.begin(115200);
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW);
  connectWiFi();
  client.setServer(mqtt_server, mqtt_port);
}

void loop() {

  if (!client.connected()) connectMQTT();
  client.loop();

  float v = pzem.voltage();
  float c = pzem.current();
  float p = pzem.power();
  float e = pzem.energy();

  if (isnan(v) || isnan(c)) return;

  float bill = e * electricityRate;

  client.publish("power/voltage", String(v,2).c_str());
  client.publish("power/current", String(c,2).c_str());
  client.publish("power/watt", String(p,2).c_str());
  client.publish("power/energy", String(e,2).c_str());
  client.publish("power/bill", String(bill,2).c_str());

  if (c > overloadCurrent) {
    digitalWrite(RELAY_PIN, HIGH);
    relayState = false;
    client.publish("power/alert", "Overload - Cut");
    client.publish("power/status", "OFF");
  } else if (!relayState) {
    digitalWrite(RELAY_PIN, LOW);
    relayState = true;
    client.publish("power/status", "ON");
  }

  delay(3000);
}

Credits

Yarana Iot Guru
48 projects • 25 followers
Yarana Iot GuruYarana IoT Guru: Arduino,ESP32, GSM, NodeMCU & more.Projects, Tutorials & App Development. Innovate with us!
Thanks to Yarana IoT Guru.

Comments