Mechatronics LAB
Published © LGPL

ESP32 Smart Home Motion-Activated Lighting System

ESP32 Smart Home Motion-Activated Lighting System

BeginnerProtip1 hour376
ESP32 Smart Home Motion-Activated Lighting System

Things used in this project

Hardware components

ESP32S
Espressif ESP32S
×1

Story

Read more

Schematics

ESP32 Smart Home Motion-Activated Lighting System

ESP32 Smart Home Motion-Activated Lighting System

Code

ESP32 Smart Home Motion-Activated Lighting System

Arduino
ESP32 Smart Home Motion-Activated Lighting System
#define BLYNK_TEMPLATE_ID "YourTemplateID"
#define BLYNK_DEVICE_NAME "MotionLightingSystem"
#define BLYNK_AUTH_TOKEN "YourAuthToken"

#include <WiFi.h>
#include <BlynkSimpleEsp32.h>

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "YourWiFiSSID";
char pass[] = "YourWiFiPassword";

const int PIR_PIN = 14; // GPIO D14 for PIR sensor
const int LED_PIN = 4;  // GPIO D4 for LED

void setup() {
    pinMode(PIR_PIN, INPUT);
    pinMode(LED_PIN, OUTPUT);
    Blynk.begin(auth, ssid, pass);
}

void loop() {
    int motion = digitalRead(PIR_PIN);
    if (motion == HIGH) {
        digitalWrite(LED_PIN, HIGH);
        Blynk.virtualWrite(V1, "Motion Detected");
    } else {
        digitalWrite(LED_PIN, LOW);
        Blynk.virtualWrite(V1, "No Motion");
    }
    Blynk.run();
}

Design the Web Interface

HTML
Design the Web Interface
<!DOCTYPE html>
<html>
<head>
    <title>Smart Lighting System</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f9;
            text-align: center;
            padding: 20px;
        }
        h1 {
            color: #007BFF;
        }
        p {
            font-size: 1.2rem;
        }
        button {
            padding: 10px 20px;
            font-size: 1rem;
            background-color: #007BFF;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }
        button:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
    <h1>Motion-Activated Lighting</h1>
    <p id="motion-status">No Motion</p>
    <button id="toggle-led">Toggle LED</button>
    <script>
        const motionStatus = document.getElementById('motion-status');
        const toggleButton = document.getElementById('toggle-led');

        // Function to fetch motion status from Blynk
        const fetchMotionStatus = () => {
            fetch('http://blynk.cloud/external/api/get?token=YourBlynkAuthToken&V1')
                .then(response => response.text())
                .then(data => {
                    motionStatus.textContent = data === "1" ? "Motion Detected" : "No Motion";
                })
                .catch(error => console.error('Error fetching motion status:', error));
        };

        // Function to toggle the LED state
        const toggleLED = () => {
            fetch('http://blynk.cloud/external/api/update?token=YourBlynkAuthToken&V2=1')
                .then(() => setTimeout(() => {
                    fetch('http://blynk.cloud/external/api/update?token=YourBlynkAuthToken&V2=0');
                }, 500))
                .catch(error => console.error('Error toggling LED:', error));
        };

        toggleButton.addEventListener('click', toggleLED);
        setInterval(fetchMotionStatus, 1000); // Update motion status every second
    </script>
</body>
</html>

Credits

Mechatronics LAB
75 projects • 47 followers
I am Sarful , I am a Mechatronics Engineer & also a teacher I am Interested in the evolution of technology in the automation industry .
Contact

Comments

Please log in or sign up to comment.