/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
/* Fill in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID "TMPL6l33Cd8jb"
#define BLYNK_TEMPLATE_NAME "HW2"
#define BLYNK_AUTH_TOKEN "RZsbvPOuk169Z5wYugpxaaD1KjGDuEPQ"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
// VIRTUAL PINS (Matches Blynk Dashboard)
#define V_LED V0 // LED Widget
#define V_SW1 V1 // SW1 Status Widget
#define V_SW2 V2 // SW2 Status Widget
#define V_MOISTURE V3 // Potentiometer Gauge
// HARDWARE PINS (Matches Wokwi)
#define LED_PIN 15
#define SW1_PIN 2
#define SW2_PIN 0
#define POT_PIN 34
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
BlynkTimer timer;
// This function controls the LED
BLYNK_WRITE(V_LED)
{
int pinValue = param.asInt();
digitalWrite(LED_PIN, pinValue);
}
// This function sends the Potentiometer data every 500ms
void sendSensor()
{
int potValue = analogRead(POT_PIN);
int moisturePercent = map(potValue, 0, 4095, 0, 100);
Blynk.virtualWrite(V_MOISTURE, moisturePercent);
}
void setup()
{
Serial.begin(9600);
// Hardware Setup
pinMode(LED_PIN, OUTPUT);
pinMode(SW1_PIN, INPUT_PULLUP); // LOW when pressed
pinMode(SW2_PIN, INPUT_PULLUP); // LOW when pressed
pinMode(POT_PIN, INPUT);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
timer.setInterval(500L, sendSensor);
}
void loop()
{
Blynk.run();
timer.run();
// If Pressed, Turn LED ON ("Open")
if (digitalRead(SW1_PIN) == LOW) {
// Only turn ON if it is currently OFF
if (digitalRead(LED_PIN) == LOW) {
digitalWrite(LED_PIN, HIGH);
Blynk.virtualWrite(V_LED, 1); // Update the App Button to "ON"
Serial.println("SW1 Pressed: LED ON");
}
}
// If Pressed, Turn LED OFF ("Closed")
if (digitalRead(SW2_PIN) == LOW) {
// Only turn OFF if it is currently ON
if (digitalRead(LED_PIN) == HIGH) {
digitalWrite(LED_PIN, LOW);
Blynk.virtualWrite(V_LED, 0); // Update the App Button to "OFF"
Serial.println("SW2 Pressed: LED OFF");
}
}
}