#define BLYNK_TEMPLATE_ID "TMPL2qCqta-zY"
#define BLYNK_TEMPLATE_NAME "Power Consumption Monitoring"
#define BLYNK_AUTH_TOKEN "mbsjZqwe4Mlionk9RkQ2WpC8ee2UIpvu" 

#include <WiFi.h>
#include <BlynkSimpleEsp32.h>

// Wi-Fi credentials
char ssid[] = "Wokwi-GUEST";  
char pass[] = "";             

// Pin definitions for LEDs
int ledPin1 = 13;    // Pin connected to the first LED
int ledPin2 = 12;    // Pin connected to the second LED
int threshold = 50;  // Power threshold
float power;         // Variable to store power value

BlynkTimer timer;    // Timer for periodic updates

// Function to simulate power values and update Blynk
void sendPowerToBlynk() {
  int simulatedPower = random(0, 100);  // Simulate power values from 0 to 100
  power = simulatedPower;               // Update the power variable

  // Print the simulated power value to the serial monitor
  Serial.print("Simulated Power: ");
  Serial.println(power);

  // Send the power value to Blynk (V0)
  Blynk.virtualWrite(V0, power);

  // Control the LEDs based on the power threshold
  if (power > threshold) {
    digitalWrite(ledPin1, HIGH);  // Turn on LED 1
    digitalWrite(ledPin2, HIGH);  // Turn on LED 2
  } else {
    digitalWrite(ledPin1, LOW);   // Turn off LED 1
    digitalWrite(ledPin2, LOW);   // Turn off LED 2
  }
}

void setup() {
  // Start serial communication
  Serial.begin(9600);

  // Initialize LED pins as outputs
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);

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

  // Setup timer to send power values every second
  timer.setInterval(1000L, sendPowerToBlynk);
}

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