gaxil
Published

IoT-Based Energy Meter with Extended Functionalities

A smart energy meter with real-time monitoring, IoT control, and extra features like auto lighting, LCD, and USB output.

IntermediateFull instructions providedOver 1 day156
IoT-Based Energy Meter with Extended Functionalities

Things used in this project

Story

Read more

Custom parts and enclosures

schematics

Schematics

schematics

.

Code

code

C/C++
full code
#define BLYNK_TEMPLATE_ID "given from blynk"
#define BLYNK_TEMPLATE_NAME "given to blynk"
#define BLYNK_AUTH_TOKEN "given from blynk"

#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <EmonLib.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// WiFi credentials
char ssid[] = "name of your wifi";
char pass[] = "password of your wifi";

// Pin configuration
const int ledPin = 27;     // LED 1
const int ledPin2 = 13;    // LED 2
const int voltagePin = 34; // ZMPT101B
const int currentPin = 35; // ACS712

// Calibration values
const float voltageCalibration = 75.0;
const float currentCalibration = 20.0;

// Virtual pin assignments
#define VPIN_LED     V1
#define VPIN_LED2    V4
#define VPIN_CURRENT V2
#define VPIN_VOLTAGE V3

// LCD setup (change I2C address if needed)
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Create instances
EnergyMonitor emonVoltage;
EnergyMonitor emonCurrent;
BlynkTimer timer;

void setup() {
  Serial.begin(115200);
  
  // Set LED pins as output
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);

  // Connect to WiFi + Blynk
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);

  // Sensor initialization
  emonVoltage.voltage(voltagePin, voltageCalibration, 1.7);
  emonCurrent.current(currentPin, currentCalibration);

  // LCD initialization
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Initializing...");

  // Read sensors every second
  timer.setInterval(1000L, readSensors);
}

// Handle LED 1 (GPIO 27) from Blynk V1
BLYNK_WRITE(VPIN_LED) {
  int state = param.asInt();
  digitalWrite(ledPin, state);
  Serial.print("LED 1 set to: ");
  Serial.println(state);
}

// Handle LED 2 (GPIO 13) from Blynk V4
BLYNK_WRITE(VPIN_LED2) {
  int state = param.asInt();
  digitalWrite(ledPin2, state);
  Serial.print("LED 2 set to: ");
  Serial.println(state);
}

// Sensor reading + LCD + Blynk update
void readSensors() {
  emonVoltage.calcVI(1000, 2000);
  float Vrms = emonVoltage.Vrms;
  float Irms = emonCurrent.Irms;

  // Filter noise
  if (Vrms < 10) Vrms = 0;
  if (Irms < 0.2) Irms = 0;

  // Debug
  Serial.print("Voltage: ");
  Serial.print(Vrms, 1);
  Serial.print(" V\tCurrent: ");
  Serial.print(Irms, 2);
  Serial.println(" A");

  // Send to Blynk
  Blynk.virtualWrite(VPIN_VOLTAGE, Vrms);
  Blynk.virtualWrite(VPIN_CURRENT, Irms);

  // Display on LCD
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("V: ");
  lcd.print(Vrms, 1);
  lcd.print(" V");

  lcd.setCursor(0, 1);
  lcd.print("I: ");
  lcd.print(Irms, 2);
  lcd.print(" A");
}

void loop() {
  Blynk.run();
  timer.run();
}

Credits

gaxil
7 projects • 5 followers
wameedh scientific club

Comments