#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL3b2-AnzBL"
#define BLYNK_TEMPLATE_NAME "Energy Meter"
#define BLYNK_AUTH_TOKEN "Lau-u8N7lOtZzMzEvPSxvdhSq2SBN_lf"
#include <BlynkSimpleEsp32.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include "EmonLib.h"
#include <LiquidCrystal_I2C.h> // Add LiquidCrystal I2C library for the 20x4 display
#define SCREEN_WIDTH 128 // OLED width, in pixels
#define SCREEN_HEIGHT 64 // OLED height, in pixels
// Create an OLED display object connected to I2C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);
LiquidCrystal_I2C lcd(0x27, 20, 4); // I2C address 0x27 for 20x4 LCD
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const int voltagePin = 35; // Analog input pin for voltage measurement
const int currentPin = 34; // Analog input pin for current measurement
const int redLedPin = 4; // Digital output pin for red LED
const int greenLedPin = 5; // Digital output pin for green LED
// Add temperature sensor pins and objects
#define ONE_WIRE_BUS 15 // Pin connected to the temperature sensor
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature tempSensor(&oneWire);
EnergyMonitor currentSensor;
float voltage, current, power, powerConsumption, temperature, costOfElectricity, totalCost, totalPowerConsumption;
unsigned long previousMillis = 0; // Variable to store the time of the last update
unsigned long totalMillis = 0; // Variable to store the total elapsed time
// Electricity rate per kWh
float electricityRate = 7; // Example rate of ₹30 per kWh
BlynkTimer timer;
void setup() {
Serial.begin(115200);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
delay(500);
display.clearDisplay();
display.setTextColor(WHITE);
lcd.begin(20, 4); // Initialize the 20x4 LCD
lcd.backlight();
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, password);
pinMode(voltagePin, INPUT);
pinMode(currentPin, INPUT);
pinMode(redLedPin, OUTPUT); // Set red LED pin as output
pinMode(greenLedPin, OUTPUT); // Set green LED pin as output
// Initialize temperature sensor
tempSensor.begin();
timer.setInterval(1000L, sendDataToBlynk); // Send data to Blynk every 1 second
}
void sendDataToBlynk() {
// Read raw analog values
int rawVoltage = analogRead(voltagePin);
int rawCurrent = analogRead(currentPin);
// Convert raw values to physical quantities
voltage = (rawVoltage / 4095.0) * 230; // Assuming 230V reference
float currentVoltage = (rawCurrent / 4095.0) * 5; // Assuming 5V reference
const float sensorSensitivity = 0.1; // Sensor sensitivity in V/A (adjust according to datasheet)
current = currentVoltage / sensorSensitivity; // Calculate current in Amperes
power = voltage * current; // Calculate power in Watts
// Calculate elapsed time since last update
unsigned long currentMillis = millis();
unsigned long elapsedTime = currentMillis - previousMillis;
previousMillis = currentMillis;
totalMillis += elapsedTime;
// Calculate power consumption
float elapsedTimeHours = totalMillis / 3600000.0; // Convert milliseconds to hours
powerConsumption = (power / 1000.0); // Convert power to kWh and multiply by elapsed time
costOfElectricity = powerConsumption * electricityRate; // Multiply by electricity rate
// Add current cost to total cost
totalCost += costOfElectricity;
// Add current power consumption to total power consumption
totalPowerConsumption += powerConsumption;
// Read temperature from sensor
tempSensor.requestTemperatures();
temperature = 28; // Assuming only one sensor connected
Serial.print("Voltage: ");
Serial.print(voltage);
Serial.print(" V, Current: ");
Serial.print(current);
Serial.print(" A, Power/H: ");
Serial.print(powerConsumption);
Serial.print(" kWh, Temperature: ");
Serial.print(temperature);
Serial.print(" °C, Cost: ₹");
Serial.println(costOfElectricity, 2);
Serial.print(", totalCost: ₹");
Serial.println(totalCost, 2);
Blynk.virtualWrite(V7, voltage); // Send voltage value to Blynk's virtual pin V0
Blynk.virtualWrite(V8, current); // Send current value to Blynk's virtual pin V1
Blynk.virtualWrite(V4, totalPowerConsumption); // Send total power consumption value to Blynk's virtual pin V2
Blynk.virtualWrite(V3, temperature); // Send temperature value to Blynk's virtual pin V3
Blynk.virtualWrite(V5, costOfElectricity); // Send cost of electricity to Blynk's virtual pin V4
Blynk.virtualWrite(V9, totalCost);
// Light up the appropriate LED based on temperature
if (temperature > 40) {
digitalWrite(redLedPin, HIGH); // Turn on red LED
digitalWrite(greenLedPin, LOW); // Turn off green LED
} else {
digitalWrite(redLedPin, LOW); // Turn off red LED
digitalWrite(greenLedPin, HIGH); // Turn on green LED
}
// Display total cost and total power consumption on the 20x4 LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Total Cost:$");
lcd.print(totalCost, 2);
lcd.setCursor(0, 1);
lcd.print("Total Power: ");
lcd.print(totalPowerConsumption, 2);
lcd.print("kWh");
}
void loop() {
Blynk.run();
timer.run();
// Display values on the OLED
display.clearDisplay(); // Clear display before drawing new values
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.print("Voltage (V): ");
display.println(voltage, 2);
display.setCursor(0, 16);
display.print("Current (A): ");
display.println(current, 2);
display.setCursor(0, 32);
display.print("Power/H: ");
display.println(powerConsumption, 2);
display.setCursor(0, 48);
display.print("Temperature: ");
display.println(temperature, 2);
display.println("°C");
display.display();
}