/*************************************************************
ESP32 Smart Home with Blynk (Adaptive Light + Security System)
Widgets used in Blynk:
- V0 : LDR Value (Value Display)
- V1 : LED Brightness (Gauge)
- V2 : PIR Status (LED Widget)
- V3 : Alarm State (Label Display)
- V4 : Manual Light Switch (Switch)
- V5 : Manual Alarm Switch (Switch)
*************************************************************/
#define BLYNK_TEMPLATE_ID "TMPL31oDcQagr"
#define BLYNK_TEMPLATE_NAME "automaticLightSensor"
#define BLYNK_AUTH_TOKEN "dBPlO-50m8XbvH9yjDjHQAd_aQl9zOKi"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
// WiFi credentials (Wokwi uses built-in guest WiFi)
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// Hardware pins
#define LDR_PIN 34 // LDR AO pin
#define LED_PIN 26 // Room light
#define PIR_SECURITY 27 // PIR sensor
#define BUZZER_PIN 14 // Buzzer
// Variables
int ldrValue = 0;
int pirSecurityValue = 0;
int brightness = 0;
bool manualLight = false; // Controlled by V4
bool manualAlarm = true; // Controlled by V5 (default = enabled)
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
pinMode(PIR_SECURITY, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
Serial.println("ESP32 Smart Home with Blynk Started");
}
// --- Manual Light Control (V4) ---
BLYNK_WRITE(V4) {
manualLight = param.asInt(); // 1 = ON, 0 = OFF
Serial.print("Manual Light Override: ");
Serial.println(manualLight);
}
// --- Manual Alarm Control (V5) ---
BLYNK_WRITE(V5) {
manualAlarm = param.asInt(); // 1 = enabled, 0 = disabled
Serial.print("Manual Alarm Arm/Disarm: ");
Serial.println(manualAlarm);
}
void loop() {
Blynk.run();
// ---------------- ROOM ADAPTIVE LIGHT ----------------
ldrValue = analogRead(LDR_PIN); // LDR gives 0 - 4095
if (manualLight) {
brightness = 255; // Force light ON
} else {
// Map inversely → darker room = brighter LED
brightness = map(ldrValue, 0, 4095, 255, 0);
}
analogWrite(LED_PIN, brightness);
// ---------------- SECURITY SYSTEM ----------------
pirSecurityValue = digitalRead(PIR_SECURITY);
String alarmState;
if (manualAlarm) {
if (ldrValue < 2500) { // Arm only when dark
if (pirSecurityValue == HIGH) {
digitalWrite(BUZZER_PIN, HIGH); // Motion → buzzer ON
alarmState = "ALARM ON";
} else {
digitalWrite(BUZZER_PIN, LOW); // No motion
alarmState = "ALARM OFF";
}
} else {
digitalWrite(BUZZER_PIN, LOW); // Daytime → idle
alarmState = "DISABLED";
}
} else {
digitalWrite(BUZZER_PIN, LOW); // Manually disarmed
alarmState = "DISABLED";
}
// ---------------- SEND TO BLYNK ----------------
Blynk.virtualWrite(V0, ldrValue);
Blynk.virtualWrite(V1, brightness);
Blynk.virtualWrite(V2, pirSecurityValue); // LED widget: ON if motion
Blynk.virtualWrite(V3, alarmState);
// ---------------- DEBUG SERIAL ----------------
Serial.print("LDR: ");
Serial.print(ldrValue);
Serial.print(" | LED Brightness: ");
Serial.print(brightness);
Serial.print(" | PIR: ");
Serial.print(pirSecurityValue);
Serial.print(" | Alarm: ");
Serial.println(alarmState);
delay(500);
}
/*
// SOLUTION 1: Simple analogWrite() approach (Most Compatible)
#define LDR_PIN 34 // LDR AO pin connected to GPIO 34
#define LED_PIN 26 // Room light (PWM-capable pin)
#define PIR_SECURITY 27 // PIR at main door
#define BUZZER_PIN 14 // Buzzer or alert LED
int ldrValue = 0;
int pirSecurityValue = 0;
void setup() {
// Simple setup without LEDC functions
pinMode(LED_PIN, OUTPUT);
pinMode(PIR_SECURITY, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
Serial.begin(115200);
Serial.println("ESP32 Smart Home Started");
}
void loop() {
// ---------------- ROOM ADAPTIVE LIGHT ----------------
ldrValue = analogRead(LDR_PIN); // LDR gives 0 - 4095
Serial.print("LDR: ");
Serial.print(ldrValue);
// Map LDR value inversely → darker room = brighter LED
// (0-4095 mapped to 255-0)
int brightness = map(ldrValue, 0, 4095, 255, 0);
analogWrite(LED_PIN, brightness); // Use analogWrite instead of ledcWrite
Serial.print(" | LED Brightness: ");
Serial.print(brightness);
// ---------------- SECURITY SYSTEM ----------------
pirSecurityValue = digitalRead(PIR_SECURITY);
Serial.print(" | PIR Security: ");
Serial.print(pirSecurityValue);
// Arm security only when dark
if (ldrValue < 2500) {
if (pirSecurityValue == HIGH) {
digitalWrite(BUZZER_PIN, HIGH); // Motion → buzzer ON
Serial.println(" | ALARM: ON");
} else {
digitalWrite(BUZZER_PIN, LOW); // No motion → buzzer OFF
Serial.println(" | ALARM: OFF");
}
} else {
digitalWrite(BUZZER_PIN, LOW); // Daytime → system idle
Serial.println(" | ALARM: DISABLED");
}
delay(200);
}
*/