Smart home automation system using ESP 32 Wowki simulator

 


 Features Include:

  • Web-based control panel with responsive design
  • DHT22 for temperature & humidity monitoring
  • PIR sensor for motion detection
  • OLED display showing real-time status
  • Auto/Manual modes for smart automation
  • LED indicators for light and fan (simulate actual devices)
  • Buzzer alerts for high temperature warnings

 Quick Start:

  1. Go to wokwi.com and create a new ESP32 project
  2. Copy the smart_home_esp32.ino code
  3. Replace the diagram.json with the provided file
  4. Click "Start Simulation"
  5. Open the Serial Monitor to get the IP address
  6. Click the IP to access the web control panel!
CODE:

/*
 * ESP32 Smart Home Automation System
 * Features:
 * - DHT22 Temperature & Humidity Sensor
 * - Motion Detection (PIR Sensor)
 * - Light Control (LED simulating smart bulb)
 * - Fan Control (Motor simulating fan)
 * - OLED Display for status
 * - Web Server for remote control
 */

#include <WiFi.h>
#include <WebServer.h>
#include <DHT.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

// WiFi Credentials (for Wokwi simulation)
const char* ssid = "Wokwi-GUEST";
const char* password = "";

// Pin Definitions
#define DHT_PIN 15
#define PIR_PIN 14
#define LIGHT_PIN 26
#define FAN_PIN 27
#define BUZZER_PIN 25

// DHT Sensor Setup
#define DHT_TYPE DHT22
DHT dht(DHT_PIN, DHT_TYPE);

// OLED Display Setup
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// Web Server
WebServer server(80);

// Global Variables
float temperature = 0;
float humidity = 0;
bool lightStatus = false;
bool fanStatus = false;
bool motionDetected = false;
bool autoMode = true;

// Timing variables
unsigned long lastSensorRead = 0;
const long sensorInterval = 2000; // Read sensor every 2 seconds

void setup() {
  Serial.begin(115200);
 
  // Initialize pins
  pinMode(PIR_PIN, INPUT);
  pinMode(LIGHT_PIN, OUTPUT);
  pinMode(FAN_PIN, OUTPUT);
  pinMode(BUZZER_PIN, OUTPUT);
 
  // Initialize DHT sensor
  dht.begin();
 
  // Initialize OLED display
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);
  display.println("Smart Home");
  display.println("Initializing...");
  display.display();
  delay(2000);
 
  // Connect to WiFi
  WiFi.begin(ssid, password);
  Serial.print("Connecting to WiFi");
  display.clearDisplay();
  display.setCursor(0, 0);
  display.println("Connecting WiFi...");
  display.display();
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
 
  Serial.println("\nWiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
 
  // Setup web server routes
  server.on("/", handleRoot);
  server.on("/light/on", handleLightOn);
  server.on("/light/off", handleLightOff);
  server.on("/fan/on", handleFanOn);
  server.on("/fan/off", handleFanOff);
  server.on("/mode/auto", handleAutoMode);
  server.on("/mode/manual", handleManualMode);
  server.on("/status", handleStatus);
 
  server.begin();
  Serial.println("HTTP server started");
 
  updateDisplay();
}

void loop() {
  server.handleClient();
 
  // Read sensors periodically
  unsigned long currentMillis = millis();
  if (currentMillis - lastSensorRead >= sensorInterval) {
    lastSensorRead = currentMillis;
    readSensors();
   
    // Auto mode logic
    if (autoMode) {
      autoControl();
    }
   
    updateDisplay();
  }
}

void readSensors() {
  // Read DHT22
  temperature = dht.readTemperature();
  humidity = dht.readHumidity();
 
  // Read PIR
  motionDetected = digitalRead(PIR_PIN);
 
  // Check for read errors
  if (isnan(temperature) || isnan(humidity)) {
    Serial.println("Failed to read from DHT sensor!");
    temperature = 0;
    humidity = 0;
  }
 
  // Print sensor data
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.print("°C, Humidity: ");
  Serial.print(humidity);
  Serial.print("%, Motion: ");
  Serial.println(motionDetected ? "Detected" : "None");
}

void autoControl() {
  // Auto light control based on motion
  if (motionDetected && !lightStatus) {
    lightStatus = true;
    digitalWrite(LIGHT_PIN, HIGH);
    Serial.println("Auto: Light ON (Motion detected)");
  } else if (!motionDetected && lightStatus) {
    lightStatus = false;
    digitalWrite(LIGHT_PIN, LOW);
    Serial.println("Auto: Light OFF (No motion)");
  }
 
  // Auto fan control based on temperature
  if (temperature > 28 && !fanStatus) {
    fanStatus = true;
    digitalWrite(FAN_PIN, HIGH);
    Serial.println("Auto: Fan ON (High temperature)");
    // Alert beep
    tone(BUZZER_PIN, 1000, 200);
  } else if (temperature <= 26 && fanStatus) {
    fanStatus = false;
    digitalWrite(FAN_PIN, LOW);
    Serial.println("Auto: Fan OFF (Normal temperature)");
  }
}

void updateDisplay() {
  display.clearDisplay();
  display.setCursor(0, 0);
 
  // Title
  display.setTextSize(1);
  display.println("SMART HOME");
  display.drawLine(0, 10, 128, 10, SSD1306_WHITE);
 
  // Temperature and Humidity
  display.setCursor(0, 14);
  display.print("Temp: ");
  display.print(temperature, 1);
  display.println(" C");
 
  display.setCursor(0, 24);
  display.print("Humid: ");
  display.print(humidity, 1);
  display.println(" %");
 
  // Status
  display.setCursor(0, 34);
  display.print("Light: ");
  display.println(lightStatus ? "ON" : "OFF");
 
  display.setCursor(0, 44);
  display.print("Fan: ");
  display.println(fanStatus ? "ON" : "OFF");
 
  display.setCursor(0, 54);
  display.print("Mode: ");
  display.println(autoMode ? "AUTO" : "MANUAL");
 
  display.display();
}

// Web Server Handlers
void handleRoot() {
  String html = "<!DOCTYPE html><html><head>";
  html += "<meta name='viewport' content='width=device-width, initial-scale=1.0'>";
  html += "<style>";
  html += "body { font-family: Arial; text-align: center; background: #f0f0f0; padding: 20px; }";
  html += ".container { max-width: 500px; margin: 0 auto; background: white; padding: 20px; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }";
  html += "h1 { color: #333; }";
  html += ".sensor { background: #e3f2fd; padding: 15px; margin: 10px 0; border-radius: 5px; }";
  html += ".button { display: inline-block; padding: 15px 30px; margin: 5px; font-size: 16px; cursor: pointer; border: none; border-radius: 5px; color: white; text-decoration: none; }";
  html += ".on { background: #4CAF50; }";
  html += ".off { background: #f44336; }";
  html += ".mode { background: #2196F3; }";
  html += ".status { font-size: 14px; color: #666; margin-top: 5px; }";
  html += "</style></head><body>";
  html += "<div class='container'>";
  html += "<h1>🏠 Smart Home Control</h1>";
 
  // Sensor Data
  html += "<div class='sensor'>";
  html += "<h3>📊 Sensors</h3>";
  html += "<p>🌡️ Temperature: <strong>" + String(temperature, 1) + " °C</strong></p>";
  html += "<p>💧 Humidity: <strong>" + String(humidity, 1) + " %</strong></p>";
  html += "<p>🚶 Motion: <strong>" + String(motionDetected ? "Detected" : "None") + "</strong></p>";
  html += "</div>";
 
  // Light Control
  html += "<div class='sensor'>";
  html += "<h3>💡 Light Control</h3>";
  html += "<p class='status'>Status: " + String(lightStatus ? "ON" : "OFF") + "</p>";
  html += "<a href='/light/on' class='button on'>Turn ON</a>";
  html += "<a href='/light/off' class='button off'>Turn OFF</a>";
  html += "</div>";
 
  // Fan Control
  html += "<div class='sensor'>";
  html += "<h3>🌀 Fan Control</h3>";
  html += "<p class='status'>Status: " + String(fanStatus ? "ON" : "OFF") + "</p>";
  html += "<a href='/fan/on' class='button on'>Turn ON</a>";
  html += "<a href='/fan/off' class='button off'>Turn OFF</a>";
  html += "</div>";
 
  // Mode Control
  html += "<div class='sensor'>";
  html += "<h3>⚙️ Control Mode</h3>";
  html += "<p class='status'>Current: " + String(autoMode ? "AUTO" : "MANUAL") + "</p>";
  html += "<a href='/mode/auto' class='button mode'>AUTO Mode</a>";
  html += "<a href='/mode/manual' class='button mode'>MANUAL Mode</a>";
  html += "</div>";
 
  html += "<p style='margin-top: 20px; font-size: 12px; color: #999;'>IP: " + WiFi.localIP().toString() + "</p>";
  html += "</div></body></html>";
 
  server.send(200, "text/html", html);
}

void handleLightOn() {
  lightStatus = true;
  digitalWrite(LIGHT_PIN, HIGH);
  Serial.println("Light turned ON via web");
  server.sendHeader("Location", "/");
  server.send(303);
}

void handleLightOff() {
  lightStatus = false;
  digitalWrite(LIGHT_PIN, LOW);
  Serial.println("Light turned OFF via web");
  server.sendHeader("Location", "/");
  server.send(303);
}

void handleFanOn() {
  fanStatus = true;
  digitalWrite(FAN_PIN, HIGH);
  Serial.println("Fan turned ON via web");
  server.sendHeader("Location", "/");
  server.send(303);
}

void handleFanOff() {
  fanStatus = false;
  digitalWrite(FAN_PIN, LOW);
  Serial.println("Fan turned OFF via web");
  server.sendHeader("Location", "/");
  server.send(303);
}

void handleAutoMode() {
  autoMode = true;
  Serial.println("Switched to AUTO mode");
  server.sendHeader("Location", "/");
  server.send(303);
}

void handleManualMode() {
  autoMode = false;
  Serial.println("Switched to MANUAL mode");
  server.sendHeader("Location", "/");
  server.send(303);
}

void handleStatus() {
  String json = "{";
  json += "\"temperature\":" + String(temperature, 1) + ",";
  json += "\"humidity\":" + String(humidity, 1) + ",";
  json += "\"motion\":" + String(motionDetected ? "true" : "false") + ",";
  json += "\"light\":" + String(lightStatus ? "true" : "false") + ",";
  json += "\"fan\":" + String(fanStatus ? "true" : "false") + ",";
  json += "\"mode\":\"" + String(autoMode ? "auto" : "manual") + "\"";
  json += "}";
 
  server.send(200, "application/json", json);
}

 Interactive Features:

  • Click the PIR sensor to simulate motion
  • Adjust DHT22 temperature/humidity sliders
  • Control lights and fan via web interface
  • Switch between AUTO and MANUAL modes

The system automatically turns on lights when motion is detected and activates the fan when temperature exceeds 28°C!

Diagram .json:

{
  "version": 1,
  "author": "ESP32 Smart Home Automation",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-esp32-devkit-v1", "id": "esp", "top": 0, "left": 0, "attrs": {} },
    {
      "type": "wokwi-dht22",
      "id": "dht1",
      "top": -67.2,
      "left": 105.6,
      "attrs": { "temperature": "8.7", "humidity": "62.5" }
    },
    {
      "type": "wokwi-pir-motion-sensor",
      "id": "pir1",
      "top": -105.49,
      "left": 230.11,
      "attrs": {}
    },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": -180,
      "left": 340,
      "attrs": { "color": "yellow", "lightColor": "yellow" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r1",
      "top": -120,
      "left": 345,
      "rotate": 90,
      "attrs": { "value": "220" }
    },
    {
      "type": "wokwi-led",
      "id": "led2",
      "top": -205.2,
      "left": 378.2,
      "attrs": { "color": "blue", "lightColor": "blue" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r2",
      "top": -120,
      "left": 405,
      "rotate": 90,
      "attrs": { "value": "220" }
    },
    {
      "type": "wokwi-buzzer",
      "id": "bz1",
      "top": -120,
      "left": 480,
      "attrs": { "volume": "0.5" }
    },
    {
      "type": "wokwi-ssd1306",
      "id": "oled1",
      "top": 121.9,
      "left": 241.9,
      "attrs": { "i2cAddress": "0x3C" }
    }
  ],
  "connections": [
    [ "esp:TX0", "$serialMonitor:RX", "", [] ],
    [ "esp:RX0", "$serialMonitor:TX", "", [] ],
    [ "dht1:VCC", "esp:3V3", "red", [ "v0" ] ],
    [ "dht1:GND", "esp:GND.1", "black", [ "v0" ] ],
    [ "dht1:SDA", "esp:D15", "green", [ "v0" ] ],
    [ "pir1:VCC", "esp:3V3", "red", [ "v0" ] ],
    [ "pir1:GND", "esp:GND.1", "black", [ "v0" ] ],
    [ "pir1:OUT", "esp:D14", "orange", [ "v0" ] ],
    [ "led1:A", "esp:D26", "yellow", [ "v-9.6", "h-48" ] ],
    [ "led1:C", "r1:1", "yellow", [ "v0" ] ],
    [ "r1:2", "esp:GND.2", "black", [ "h0" ] ],
    [ "led2:A", "esp:D27", "blue", [ "v-9.6", "h-96" ] ],
    [ "led2:C", "r2:1", "blue", [ "v0" ] ],
    [ "r2:2", "esp:GND.2", "black", [ "h0" ] ],
    [ "bz1:1", "esp:D25", "green", [ "v0" ] ],
    [ "bz1:2", "esp:GND.2", "black", [ "v0" ] ],
    [ "oled1:VCC", "esp:3V3", "red", [ "h0" ] ],
    [ "oled1:GND", "esp:GND.2", "black", [ "h0" ] ],
    [ "oled1:SDA", "esp:D21", "blue", [ "h0" ] ],
    [ "oled1:SCL", "esp:D22", "yellow", [ "h0" ] ]
  ],
  "dependencies": {}
}


Comments