#define BLYNK_TEMPLATE_ID "TMPL6r-sgG6Jr"
#define BLYNK_TEMPLATE_NAME "ENERGY METER"
#define BLYNK_AUTH_TOKEN "d4TkH2lr6kHvIgNEfmsxpFPIoqymWpw9"
#include <BlynkSimpleEsp32.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "EmonLib.h"  // Include Emon Library

#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);

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

EnergyMonitor currentSensor;

float voltage, current, power;

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);

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, password);

  pinMode(voltagePin, INPUT);
  pinMode(currentPin, INPUT);

  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) * 3.3;  // Assuming 3.3V reference

// Change these values based on your specific SCT sensor model and calibration
float currentVoltage = (rawCurrent / 4095.0) * 3.3; // Assuming 3.3V reference
const float sensorSensitivity = 0.1; // Sensor sensitivity in V/A (adjust according to datasheet)
current = currentVoltage / sensorSensitivity; // Calculate current in Amperes
  // Calculate power using P = VI
  power = voltage * current;

  Serial.print("Voltage: ");
  Serial.print(voltage);
  Serial.print(" V, Current: ");
  Serial.print(current);
  Serial.print(" A, Power: ");
  Serial.print(power);
  Serial.println(" W");
  
  Blynk.virtualWrite(V0, voltage); // Send voltage value to Blynk's virtual pin V0
  Blynk.virtualWrite(V1, current); // Send current value to Blynk's virtual pin V1
  Blynk.virtualWrite(V2, power);   // Send power value to Blynk's virtual pin V2
}

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

  // Display values on the OLED
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.print("Voltage: ");
  display.println(voltage, 2);
  display.println(" V");

  display.setCursor(0, 16);
  display.print("Current: ");
  display.println(current, 2);
  display.println("A");

  display.display();
  delay(1000); // Add a short delay to ensure the OLED has time to refresh
  display.clearDisplay();
}
esp:VIN
esp:GND.2
esp:D13
esp:D12
esp:D14
esp:D27
esp:D26
esp:D25
esp:D33
esp:D32
esp:D35
esp:D34
esp:VN
esp:VP
esp:EN
esp:3V3
esp:GND.1
esp:D15
esp:D2
esp:D4
esp:RX2
esp:TX2
esp:D5
esp:D18
esp:D19
esp:D21
esp:RX0
esp:TX0
esp:D22
esp:D23
r1:1
r1:2
r2:1
r2:2
oled1:GND
oled1:VCC
oled1:SCL
oled1:SDA
r3:1
r3:2
pot1:GND
pot1:SIG
pot1:VCC
pot2:GND
pot2:SIG
pot2:VCC