/*
* ============================================================
* Integrated Smart Home Automation & Security Ecosystem
* ============================================================
* Microcontroller : NodeMCU ESP8266
* Modules : Climate Control, Smart Lighting,
* Intrusion Detection, Door Access,
* Security Mode, Core Logic Engine
* Platform : Wokwi Simulator / Arduino IDE
* Author : [pramod BR]
* Date : [19/04/2026]
* ============================================================
*/
// ---- Libraries ----
#include <Servo.h> // For Servo Motor control
// ============================================================
// SECTION 1: PIN CONFIGURATION
// ============================================================
// --- Input Pins ---
#define THERMISTOR_PIN A0 // Thermistor analog input (NodeMCU has 1 analog pin)
#define LDR_PIN A0 // LDR shares analog pin (read separately in loop)
#define IR_PIN D5 // IR Motion sensor digital input
#define DOORBELL_PIN D6 // Doorbell push button
#define MODE_BTN_PIN D7 // Security mode toggle button
// --- Output Pins ---
#define FAN_LED_PIN D1 // Fan/Cooling LED (Climate module)
#define INDOOR_LED_PIN D2 // Indoor light LED (Smart lighting module)
#define RED_LED_PIN D3 // Red LED (Security/Intrusion alert)
#define GREEN_LED_PIN D4 // Green LED (Normal mode / Door welcome)
#define BUZZER_PIN D8 // Piezo Buzzer (alerts)
#define SERVO_PIN D0 // Servo Motor (door mechanism)
// ============================================================
// SECTION 2: THRESHOLD CALIBRATION VALUES
// ============================================================
#define TEMP_THRESHOLD 30.0 // Temperature in °C — above this triggers cooling
#define LIGHT_THRESHOLD 500 // LDR raw value — below this means it's dark
#define ANALOG_READ_TEMP 0 // Mode flag: read thermistor
#define ANALOG_READ_LDR 1 // Mode flag: read LDR
// ============================================================
// SECTION 3: GLOBAL STATE VARIABLES
// ============================================================
bool securityMode = false; // false = Normal Mode, true = Security Mode
bool doorOpen = false; // Tracks if door is currently open
bool coolingActive = false; // Tracks if cooling/fan is ON
bool intrusionDetected = false; // Tracks if intrusion is active
// ============================================================
// SECTION 4: NON-BLOCKING TIMING (millis-based)
// ============================================================
unsigned long lastTempCheck = 0;
unsigned long lastLightCheck = 0;
unsigned long lastIRCheck = 0;
unsigned long doorOpenTime = 0;
unsigned long lastBuzzerToggle = 0;
unsigned long lastModeBtnCheck = 0;
#define TEMP_CHECK_INTERVAL 2000 // Check temperature every 2 seconds
#define LIGHT_CHECK_INTERVAL 1000 // Check light every 1 second
#define IR_CHECK_INTERVAL 500 // Check IR every 0.5 seconds
#define DOOR_AUTO_CLOSE 3000 // Auto-close door after 3 seconds
#define BUZZER_BEEP_INTERVAL 300 // Buzzer beep interval
#define MODE_BTN_DEBOUNCE 200 // Button debounce time
Servo doorServo; // Servo object for door
// ============================================================
// SECTION 5: SETUP — Runs once at startup
// ============================================================
void setup() {
Serial.begin(115200);
delay(500);
Serial.println("============================================");
Serial.println(" Smart Home Automation & Security System ");
Serial.println("============================================");
// Set input pins
pinMode(IR_PIN, INPUT);
pinMode(DOORBELL_PIN, INPUT_PULLUP); // Pull-up: LOW when pressed
pinMode(MODE_BTN_PIN, INPUT_PULLUP); // Pull-up: LOW when pressed
// Set output pins
pinMode(FAN_LED_PIN, OUTPUT);
pinMode(INDOOR_LED_PIN, OUTPUT);
pinMode(RED_LED_PIN, OUTPUT);
pinMode(GREEN_LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
// Attach servo
doorServo.attach(SERVO_PIN);
doorServo.write(0); // Start at closed position
// Initial state — Normal Mode (Green LED ON)
digitalWrite(GREEN_LED_PIN, HIGH);
digitalWrite(RED_LED_PIN, LOW);
Serial.println("System initialized in NORMAL MODE");
Serial.println("All modules active. Monitoring started.\n");
}
// ============================================================
// SECTION 6: MODULE 1 — ADAPTIVE CLIMATE CONTROL
// Reads thermistor, triggers fan LED + buzzer if too hot
// ============================================================
float readTemperature() {
/*
* Thermistor voltage divider formula:
* Raw ADC (0-1023) → Voltage → Resistance → Temperature (Steinhart-Hart)
* Simplified for simulation: map raw value to temperature range
*/
int rawValue = analogRead(THERMISTOR_PIN);
// Simplified mapping for Wokwi simulation (0-1023 → 10°C to 50°C)
float temperature = map(rawValue, 0, 1023, 10, 50);
return temperature;
}
void climateControlModule() {
unsigned long now = millis();
if (now - lastTempCheck >= TEMP_CHECK_INTERVAL) {
lastTempCheck = now;
float temp = readTemperature();
Serial.print("[CLIMATE] Temperature: ");
Serial.print(temp);
Serial.println(" °C");
if (temp > TEMP_THRESHOLD) {
// Too hot — activate cooling response
if (!coolingActive) {
coolingActive = true;
digitalWrite(FAN_LED_PIN, HIGH); // Turn ON fan/cooling LED
Serial.println("[CLIMATE] HIGH TEMP! Cooling activated.");
}
// Auditory alert — short beep
tone(BUZZER_PIN, 1000, 200);
} else {
// Temperature normal — shutdown cooling
if (coolingActive) {
coolingActive = false;
digitalWrite(FAN_LED_PIN, LOW); // Turn OFF fan LED
noTone(BUZZER_PIN);
Serial.println("[CLIMATE] Temperature normal. Cooling deactivated.");
}
}
}
}
// ============================================================
// SECTION 7: MODULE 2 — INTELLIGENT AMBIENT ILLUMINATION
// Reads LDR, auto-controls indoor lighting
// ============================================================
void smartLightingModule() {
unsigned long now = millis();
if (now - lastLightCheck >= LIGHT_CHECK_INTERVAL) {
lastLightCheck = now;
int lightValue = analogRead(LDR_PIN);
Serial.print("[LIGHTING] LDR Value: ");
Serial.println(lightValue);
if (lightValue < LIGHT_THRESHOLD) {
// Low light — dark outside → turn ON indoor light
digitalWrite(INDOOR_LED_PIN, HIGH);
Serial.println("[LIGHTING] Low light detected. Indoor light ON.");
} else {
// Bright light — daytime → turn OFF indoor light
digitalWrite(INDOOR_LED_PIN, LOW);
Serial.println("[LIGHTING] Daylight detected. Indoor light OFF.");
}
}
}
// ============================================================
// SECTION 8: MODULE 3 — PROACTIVE INTRUSION DETECTION
// IR sensor detects motion, triggers tiered alarm response
// ============================================================
void intrusionDetectionModule() {
unsigned long now = millis();
if (now - lastIRCheck >= IR_CHECK_INTERVAL) {
lastIRCheck = now;
int irValue = digitalRead(IR_PIN);
if (irValue == HIGH) {
// Motion detected!
intrusionDetected = true;
Serial.println("[SECURITY] MOTION DETECTED!");
// Flash Red LED
digitalWrite(RED_LED_PIN, !digitalRead(RED_LED_PIN));
if (securityMode) {
// Security Mode — aggressive response: loud long alarm
tone(BUZZER_PIN, 2000, 500);
Serial.println("[SECURITY] ARMED MODE — High alert alarm triggered!");
} else {
// Normal Mode — mild response: short beep
tone(BUZZER_PIN, 1500, 150);
Serial.println("[SECURITY] Normal mode — Mild alert triggered.");
}
} else {
// No motion
if (intrusionDetected) {
intrusionDetected = false;
noTone(BUZZER_PIN);
// Restore LED state based on mode
if (!securityMode) {
digitalWrite(RED_LED_PIN, LOW);
}
Serial.println("[SECURITY] No motion. Alert cleared.");
}
}
}
}
// ============================================================
// SECTION 9: MODULE 4 — AUTOMATED ACCESS & ENTRY MANAGEMENT
// Doorbell button triggers servo door open + auto-close
// ============================================================
void doorAccessModule() {
int doorbellPressed = digitalRead(DOORBELL_PIN); // LOW when pressed (pull-up)
if (doorbellPressed == LOW && !doorOpen) {
// Doorbell pressed — open door
doorOpen = true;
doorOpenTime = millis();
doorServo.write(90); // Rotate servo to 90° — door opens
digitalWrite(GREEN_LED_PIN, HIGH); // Welcome indicator ON
Serial.println("[DOOR] Doorbell pressed! Door OPENED. Auto-close in 3s.");
}
// Auto-close after DOOR_AUTO_CLOSE milliseconds
if (doorOpen && (millis() - doorOpenTime >= DOOR_AUTO_CLOSE)) {
doorOpen = false;
doorServo.write(0); // Rotate servo back to 0° — door closes
// Restore Green LED based on current mode
if (!securityMode) {
digitalWrite(GREEN_LED_PIN, HIGH); // Normal mode keeps green ON
} else {
digitalWrite(GREEN_LED_PIN, LOW);
}
Serial.println("[DOOR] Door AUTO-CLOSED after 3 seconds.");
}
}
// ============================================================
// SECTION 10: MODULE 5 — GLOBAL STATE & SECURITY GOVERNANCE
// Toggle button switches between Normal and Security Mode
// ============================================================
void securityGovernanceModule() {
unsigned long now = millis();
// Debounce button check
if (now - lastModeBtnCheck >= MODE_BTN_DEBOUNCE) {
lastModeBtnCheck = now;
int btnState = digitalRead(MODE_BTN_PIN); // LOW when pressed
if (btnState == LOW) {
// Toggle security mode
securityMode = !securityMode;
if (securityMode) {
// Switch to Security Mode (Red)
digitalWrite(RED_LED_PIN, HIGH);
digitalWrite(GREEN_LED_PIN, LOW);
Serial.println("[MODE] === SECURITY MODE ACTIVATED (Red) ===");
} else {
// Switch to Normal Mode (Green)
digitalWrite(GREEN_LED_PIN, HIGH);
digitalWrite(RED_LED_PIN, LOW);
Serial.println("[MODE] === NORMAL MODE ACTIVATED (Green) ===");
}
delay(300); // Short delay to prevent double-toggle
}
}
}
// ============================================================
// SECTION 11: MODULE 6 — CORE EMBEDDED LOGIC & INTEGRATION
// Non-blocking loop engine — runs all modules simultaneously
// ============================================================
void printSystemStatus() {
Serial.println("-------- System Status --------");
Serial.print("Mode : ");
Serial.println(securityMode ? "SECURITY (Red)" : "NORMAL (Green)");
Serial.print("Cooling : ");
Serial.println(coolingActive ? "ON" : "OFF");
Serial.print("Door : ");
Serial.println(doorOpen ? "OPEN" : "CLOSED");
Serial.print("Intrusion : ");
Serial.println(intrusionDetected ? "DETECTED" : "Clear");
Serial.println("-------------------------------\n");
}
// ============================================================
// SECTION 12: MAIN LOOP — All modules run concurrently
// ============================================================
unsigned long lastStatusPrint = 0;
void loop() {
// --- Run all 5 modules using non-blocking millis() timing ---
climateControlModule(); // Module 1: Temperature monitoring
smartLightingModule(); // Module 2: Auto light control
intrusionDetectionModule(); // Module 3: IR motion security
doorAccessModule(); // Module 4: Servo door management
securityGovernanceModule(); // Module 5: Mode toggle
// Print full system status every 5 seconds
if (millis() - lastStatusPrint >= 5000) {
lastStatusPrint = millis();
printSystemStatus();
}
// Small yield to keep ESP8266 WiFi stack stable
yield();
}