#include <LiquidCrystal_I2C.h>
#define BLYNK_PRINT Serial
#include "EmonLib.h"
#include <WiFi.h>
#include <WiFiClient.h>
#define BLYNK_TEMPLATE_ID "TMPL3LjHj4tSY"
#define BLYNK_TEMPLATE_NAME "Meter2"
#define BLYNK_AUTH_TOKEN "gHgf-RPZ-ML7L1RCnY4dWgKaFQ8BRTox"
#include <BlynkSimpleEsp32.h>
char auth[] = "gHgf-RPZ-ML7L1RCnY4dWgKaFQ8BRTox";
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// Initialize LCD
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address to 0x27 for a 16 chars and 2-line display
// Pin definitions
#define VOLTAGE_PIN 34 // ADC pin for voltage measurement
#define CURRENT_PIN 35 // ADC pin for current measurement
// Resistor values for the voltage divider
const float realVoltage = 220.0; // Real-world voltage (220V)
const float scaledVoltage = 3.3; // Scaled down voltage across the voltage divider (around 3.17V)
// Shunt resistor value
const float shuntResistance = 1.0; // 1Ω shunt resistor for current measurement
// Energy variables
float energyConsumed = 0.0; // Total energy consumption in watt-hours
unsigned long lastMillis = 0; // For tracking time
void setup() {
// Initialize LCD
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Group No.15");
delay(2000);
// Start Serial
Serial.begin(115200);
// Setup ADC resolution and attenuation
analogReadResolution(12); // 12-bit resolution (values from 0 to 4095)
analogSetAttenuation(ADC_11db); // For reading up to 3.3V
// Display startup message
lcd.setCursor(0, 0);
lcd.print("Voltage Current");
lcd.setCursor(0, 1);
lcd.print("Power kWH");
delay(2000);
// Connect to WiFi
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
// Initialize Blynk
Blynk.begin(auth, ssid, pass);
}
void loop() {
// Read voltage and current
float voltage = readVoltage();
float current = readCurrent();
// Calculate power and energy
float power = voltage * current;
calculateEnergy(power);
// Display values on LCD
lcd.setCursor(0, 0);
lcd.print("V: ");
lcd.print(voltage, 1);
lcd.print("V I: ");
lcd.print(current, 1);
lcd.print("A");
lcd.setCursor(0, 1);
lcd.print("P: ");
lcd.print(power, 1);
lcd.print("W E: ");
lcd.print(energyConsumed, 1);
lcd.print("Wh");
// Send data to Blynk
Blynk.virtualWrite(V0, voltage); // Send voltage to Blynk
Blynk.virtualWrite(V1, current); // Send current to Blynk
Blynk.virtualWrite(V2, power); // Send power to Blynk
Blynk.virtualWrite(V3, energyConsumed); // Send energy consumption to Blynk
// Run Blynk
Blynk.run();
delay(1000); // Update every 1 second
}
// Function to read the scaled voltage from the ADC
float readVoltage() {
int adcValue = analogRead(VOLTAGE_PIN);
float measuredVoltage = (adcValue / 4095.0) * 3.3; // Convert ADC value to voltage (0-3.3V range)
float actualVoltage = measuredVoltage * (realVoltage / scaledVoltage); // Scale it to 220V range
return actualVoltage;
}
// Function to read current based on shunt resistor
float readCurrent() {
int adcValue = analogRead(CURRENT_PIN);
float measuredVoltage = (adcValue / 4095.0) * 3.3; // Convert ADC value to voltage
float current = measuredVoltage / shuntResistance; // Calculate current using Ohm's Law (V = IR)
return current;
}
// Function to calculate energy consumption over time
void calculateEnergy(float power) {
unsigned long currentMillis = millis();
float timeElapsed = (currentMillis - lastMillis) / 3600000.0; // Convert time from ms to hours
energyConsumed += power * timeElapsed; // Energy in watt-hours (Wh)
lastMillis = currentMillis; // Update lastMillis to current time
}