#define BLYNK_TEMPLATE_ID "TMPL6KQ9Y3lnz"
#define BLYNK_TEMPLATE_NAME "pt2"
#define BLYNK_AUTH_TOKEN "vIOU37H6FiLkJ9OZli_S20GcZhHiQWdt"
#define BLYNK_PRINT Serial
#include <WiFi.h> // Include the WiFi library
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h> // Include the Blynk library for ESP32
// WiFi credentials
char ssid[] = "Wokwi-GUEST"; // Replace with your WiFi network name
char pass[] = ""; // Leave empty for open networks
bool isBlinking = false; // Variable to check if LED should blink
unsigned long lastTime = 0; // Stores the last time LED was toggled
int ledState = LOW; // Keeps track of LED state (ON or OFF)
// This function runs when the button in the Blynk app is pressed
BLYNK_WRITE(V0) {
int buttonState = param.asInt(); // Get the button value (1 for ON, 0 for OFF)
if (buttonState == 1) {
isBlinking = true; // Start blinking when button is ON
Serial.println("Blinking started");
} else {
isBlinking = false; // Stop blinking when button is OFF
digitalWrite(14, LOW); // Turn off the LED immediately
Serial.println("Blinking stopped");
}
}
void setup() {
pinMode(14, OUTPUT); // Set pin as an OUTPUT for the LED
Serial.begin(9600); // Start serial communication at 9600 baud
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass); // Connect to Blynk using WiFi credentials
Serial.println("Setup complete!");
}
void loop() {
Blynk.run(); // Keep Blynk running
// Blink the LED if isBlinking is true
if (isBlinking) {
unsigned long currentTime = millis(); // Get the current time in milliseconds
// Check if 1 second has passed
if (currentTime - lastTime >= 500) {
lastTime = currentTime; // Update the last time
// Toggle the LED state
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
digitalWrite(14, ledState); // Apply the new LED state
Serial.println(ledState == HIGH ? "LED ON" : "LED OFF");
}
}
}