// 1. BLYNK SETUP (Place your credentials here)
#define BLYNK_TEMPLATE_ID "TMPL2epPvChmM"
#define BLYNK_TEMPLATE_NAME "esp assignment mobile"
#define BLYNK_AUTH_TOKEN "I2UR0_WIZiwNjRbLg8UZsYcD7fAcweBo"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
// Your WiFi credentials
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// 3. PIN DEFINITIONS
#define RED_LED 18
#define YELLOW_LED 19
#define GREEN_LED 23
#define BUZZER 16
#define BTN_TOGGLE 13
#define BTN_RESET 32
#define TONE_CRITICAL 2000
// 4. STATE MACHINE
enum State { NORMAL = 0, WARNING = 1, CRITICAL = 2 };
State currentState = NORMAL;
bool lastToggleState = HIGH;
bool lastResetState = HIGH;
// --------------------- BLYNK APP COMMANDS ---------------------
// Remote Reset from App (V1)
BLYNK_WRITE(V1) {
if (param.asInt() == 1) {
Serial.println("Blynk: Remote Reset");
resetToNormal();
}
}
// Remote Toggle from App (V2)
BLYNK_WRITE(V2) {
if (param.asInt() == 1) {
Serial.println("Blynk: Remote Toggle");
changeState();
}
}
// ------------------------- SETUP ---------------------------
void setup() {
Serial.begin(115200);
// Connect to Blynk
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
pinMode(GREEN_LED, OUTPUT);
pinMode(YELLOW_LED, OUTPUT);
pinMode(RED_LED, OUTPUT);
pinMode(BUZZER, OUTPUT);
pinMode(BTN_TOGGLE, INPUT_PULLUP);
pinMode(BTN_RESET, INPUT_PULLUP);
updateSystem();
}
// -------------------------- LOOP ---------------------------
void loop() {
Blynk.run(); // Keep connection alive
// 1. Physical Toggle Button
bool toggleReading = digitalRead(BTN_TOGGLE);
if (toggleReading == LOW && lastToggleState == HIGH) {
delay(50); // debounce
if (digitalRead(BTN_TOGGLE) == LOW) {
changeState();
delay(200);
}
}
lastToggleState = toggleReading;
// 2. Physical Reset Button
bool resetReading = digitalRead(BTN_RESET);
if (resetReading == LOW && lastResetState == HIGH) {
delay(50);
if (digitalRead(BTN_RESET) == LOW) {
resetToNormal();
delay(200);
}
}
lastResetState = resetReading;
// 3. Buzzer Logic
if (currentState == CRITICAL) {
tone(BUZZER, TONE_CRITICAL);
} else {
noTone(BUZZER);
}
}
// ---------------------- HELPER FUNCTIONS ------------------
void changeState() {
if (currentState == NORMAL) currentState = WARNING;
else if (currentState == WARNING) currentState = CRITICAL;
else currentState = NORMAL;
updateSystem();
}
void resetToNormal() {
currentState = NORMAL;
updateSystem();
}
void updateSystem() {
// Update Physical Hardware
digitalWrite(GREEN_LED, (currentState == NORMAL) ? HIGH : LOW);
digitalWrite(YELLOW_LED, (currentState == WARNING) ? HIGH : LOW);
digitalWrite(RED_LED, (currentState == CRITICAL) ? HIGH : LOW);
// Update Blynk App
Blynk.virtualWrite(V0, (int)currentState); // Update state number
Blynk.virtualWrite(V3, (currentState == CRITICAL) ? 255 : 0); // Update App LED brightness
// Serial Monitor Debugging
Serial.print("System State: ");
Serial.println(currentState);
}