#define BLYNK_TEMPLATE_ID "TMPLOCRXh0T1"
#define BLYNK_TEMPLATE_NAME "TESTING"
#define BLYNK_AUTH_TOKEN "4SNtY61KC-K8G3IZfZyWWKpG3zy_3Z-l"
#define BLYNK_AUTH "4SNtY61KC-K8G3IZfZyWWKpG3zy_3Z-l"
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
// Pin for the LED
int ledPin = 2;
// Pin for the physical button
int buttonPin = 4;
// Variable to store LED state
int ledState = LOW;
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to Wi-Fi");
// Configure Blynk
Blynk.config(BLYNK_AUTH_TOKEN);
// Set LED pin as OUTPUT
pinMode(ledPin, OUTPUT);
// Set button pin as INPUT
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
// Run Blynk client
Blynk.run();
Connection();
}
// Blynk function to control LED state
BLYNK_WRITE(V1) {
ledState = param.asInt();
digitalWrite(ledPin, ledState);
}
void Connection(){
// Check if connected to Blynk server
if (Blynk.connected()) {
// Control LED using Blynk app when online
Blynk.virtualWrite(V1, ledState);
}
else {
// Control LED using physical button when offline
int buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
ledState = !ledState;
digitalWrite(ledPin, ledState);
delay(250); // Button debouncing delay
}
}
}