29. Building a Home Intrusion Detection System with ESP32-CAM

Introduction:

    Home security is a top priority for many homeowners. In this blog, we will explore how to create a simple yet effective home intrusion detection system using an ESP32-CAM module, a PIR sensor for motion detection, an IR sensor for flash control, and a buzzer for alarm notification. Additionally, we will develop a web application to store and view images captured during an intrusion, along with timestamps and intrusion types.



Components Needed :

  • ESP32-CAM module
  • PIR sensor
  • IR sensor
  • Buzzer
  • Jumper wires
  • USB cable for programming and power

Setting Up the Hardware :

    Connect the components as follows:

  • PIR sensor :
    • VCC to ESP32-CAM 5V pin,
    • OUT to ESP32-CAM GPIO pin (e.g., GPIO 13), 
    • GND to ESP32-CAM GND pin
  • IR sensor :
    • VCC to ESP32-CAM 5V pin,
    • OUT to ESP32-CAM GPIO pin (e.g., GPIO 12), 
    • GND to ESP32-CAM GND pin
  • Buzzer :
    • VCC to ESP32-CAM 5V pin,
    • GND to ESP32-CAM GND pin, 
    • Signal to ESP32-CAM GPIO pin (e.g., GPIO 14)

Setting Up the Software :

  1. Install the Arduino IDE and the ESP32 board manager.
  2. Install the required libraries for the ESP32-CAM module, PIR sensor, IR sensor, SPIFFS, and WebServer (if not already installed).
Circuit Diagram :

Code :

C++
#include "esp_camera.h" #include <WiFi.h> #include <WebServer.h> #include "FS.h" // WiFi credentials const char* ssid = "YourWiFiSSID"; const char* password = "YourWiFiPassword"; // Camera configuration const int camera_pins[] = {CAMERA_PIN_PWDN, CAMERA_PIN_RESET, CAMERA_PIN_XCLK, CAMERA_PIN_SIOD, CAMERA_PIN_SIOC, CAMERA_PIN_Y9, CAMERA_PIN_Y8, CAMERA_PIN_Y7, CAMERA_PIN_Y6, CAMERA_PIN_Y5, CAMERA_PIN_Y4, CAMERA_PIN_Y3, CAMERA_PIN_Y2, CAMERA_PIN_VSYNC, CAMERA_PIN_HREF, CAMERA_PIN_PCLK}; const int PWDN = -1; // Not used const int RESET = -1; // Not used const int LED_FLASH = 4; // Flash LED pin const int BUZZER_PIN = 14; // Buzzer pin const int PIR_PIN = 13; // PIR sensor pin const int IR_PIN = 12; // IR sensor pin WebServer server(80); bool motionDetected = false; bool flashNeeded = false; void setup() { Serial.begin(115200); // Configure PIR sensor pinMode(PIR_PIN, INPUT); // Configure IR sensor pinMode(IR_PIN, INPUT); // Configure Buzzer pinMode(BUZZER_PIN, OUTPUT); digitalWrite(BUZZER_PIN, LOW); // Configure camera camera_config_t config; config.ledc_channel = LEDC_CHANNEL_0; config.ledc_timer = LEDC_TIMER_0; config.pin_d0 = Y2_GPIO_NUM; config.pin_d1 = Y3_GPIO_NUM; config.pin_d2 = Y4_GPIO_NUM; config.pin_d3 = Y5_GPIO_NUM; config.pin_d4 = Y6_GPIO_NUM; config.pin_d5 = Y7_GPIO_NUM; config.pin_d6 = Y8_GPIO_NUM; config.pin_d7 = Y9_GPIO_NUM; config.pin_xclk = XCLK_GPIO_NUM; config.pin_pclk = PCLK_GPIO_NUM; config.pin_vsync = VSYNC_GPIO_NUM; config.pin_href = HREF_GPIO_NUM; config.pin_sscb_sda = SIOD_GPIO_NUM; config.pin_sscb_scl = SIOC_GPIO_NUM; config.pin_pwdn = PWDN_GPIO_NUM; config.pin_reset = RESET_GPIO_NUM; config.xclk_freq_hz = 20000000; config.pixel_format = PIXFORMAT_JPEG; if (psramFound()) { config.frame_size = FRAMESIZE_UXGA; config.jpeg_quality = 10; config.fb_count = 2; } else { config.frame_size = FRAMESIZE_SVGA; config.jpeg_quality = 12; config.fb_count = 1; } // Initialize the camera esp_err_t err = esp_camera_init(&config); if (err != ESP_OK) { Serial.printf("Camera init failed with error 0x%x", err); return; } // Connect to WiFi WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } Serial.println("Connected to WiFi"); // Initialize SPIFFS if (!SPIFFS.begin(true)) { Serial.println("An error occurred while mounting SPIFFS"); return; } // Setup routes for HTTP server server.on("/", HTTP_GET, handleRoot); server.on("/upload", HTTP_POST, handleUpload); server.on("/images", HTTP_GET, handleImages); server.on("/image", HTTP_GET, handleImage); server.begin(); } void loop() { server.handleClient(); motionDetected = digitalRead(PIR_PIN) == HIGH; flashNeeded = digitalRead(IR_PIN) == HIGH; if (motionDetected) { // Turn on buzzer digitalWrite(BUZZER_PIN, HIGH); // Capture an image camera_fb_t * fb = NULL; fb = esp_camera_fb_get(); if (!fb) { Serial.println("Camera capture failed"); return; } // Turn on flash if needed if (flashNeeded) { ledcAttachPin(LED_FLASH, 0); ledcWrite(0, 255); } // Save the image String filename = "/image_" + String(millis()) + ".jpg"; File file = SPIFFS.open(filename, FILE_WRITE); if (!file) { Serial.println("Failed to open file for writing"); return; } file.write(fb->buf, fb->len); file.close(); esp_camera_fb_return(fb); // Turn off flash if (flashNeeded) { ledcDetachPin(LED_FLASH); } // Turn off buzzer digitalWrite(BUZZER_PIN, LOW); } } void handleRoot() { server.send(200, "text/html", "<h1>Home Intrusion Detection System</h1><p>Secure your home with our system!</p>"); } void handleUpload() { HTTPUpload& upload = server.upload(); if (upload.status == UPLOAD_FILE_START) { String filename = "/image_" + String(millis()) + ".jpg"; File file = SPIFFS.open(filename, FILE_WRITE); if (!file) { Serial.println("Failed to open file for writing"); return; } Serial.print("Writing file: "); Serial.println(filename); // Save timestamp and intrusion type file.println("Time: " + String(millis())); file.println("Intrusion Type: " + (motionDetected ? "Motion" : "Unknown")); file.close(); } else if (upload.status == UPLOAD_FILE_WRITE) { file.write(upload.buf, upload.currentSize); } else if (upload.status == UPLOAD_FILE_END) { file.close(); Serial.println("File upload successful"); } } void handleImages() { String imagesList = "<h2>Images:</h2><ul>"; File root = SPIFFS.open("/"); File file = root.openNextFile(); while (file) { if (!file.isDirectory()) { imagesList += "<li><a href='/image?name=" + file.name() + "'>" + file.name() + "</a></li>"; } file = root.openNextFile(); } imagesList += "</ul>"; server.send(200, "text/html", imagesList); } void handleImage() { String imageName = server.arg("name"); if (imageName != "") { File file = SPIFFS.open(imageName, FILE_READ); if (file) { server.streamFile(file, "image/jpeg"); file.close(); return; } } server.send(404, "text/plain", "File Not Found"); }

Code Implementation :

C++
#include "esp_camera.h"
#include <WiFi.h>
#include <WebServer.h>
#include "FS.h"
  • Includes necessary libraries for ESP32, camera, WiFi, and SPIFFS.
C++
const char * ssid = "YourWiFiSSID"; const char* password = "YourWiFiPassword";
  • Defines WiFi credentials.
C++
const int camera_pins[] = {CAMERA_PIN_PWDN, CAMERA_PIN_RESET, CAMERA_PIN_XCLK, CAMERA_PIN_SIOD, CAMERA_PIN_SIOC, CAMERA_PIN_Y9, CAMERA_PIN_Y8, CAMERA_PIN_Y7, CAMERA_PIN_Y6, CAMERA_PIN_Y5, CAMERA_PIN_Y4, CAMERA_PIN_Y3, CAMERA_PIN_Y2, CAMERA_PIN_VSYNC, CAMERA_PIN_HREF, CAMERA_PIN_PCLK};
  •  Defines the pins used by the camera.
C++
const int RESET = -1; // Not used
const int LED_FLASH = 4; // Flash LED pin
const int BUZZER_PIN = 14; // Buzzer pin
const int PIR_PIN = 13; // PIR sensor pin
const int IR_PIN = 12; // IR sensor pin

  • Defines pins for the flash LED, buzzer, PIR sensor, and IR sensor.

Global Variables

C++
WebServer server(80);
bool motionDetected = false;
bool flashNeeded = false;

  • Creates a WebServer instance on port 80 and defines variables to store the status of motion detection and flash requirement.

Setup Function

C++
void setup() {
  // Serial communication setup
  Serial.begin(115200);

  // Configure PIR sensor pin as input
  pinMode(PIR_PIN, INPUT);

  // Configure IR sensor pin as input
  pinMode(IR_PIN, INPUT);

  // Configure buzzer pin as output
  pinMode(BUZZER_PIN, OUTPUT);
  digitalWrite(BUZZER_PIN, LOW);

  // Camera configuration
  // (camera initialization code here)

  // WiFi connection
  // (WiFi connection code here)

  // SPIFFS initialization
  // (SPIFFS initialization code here)

  // Setup HTTP routes
  // (HTTP server setup code here)
}

  • Sets up serial communication, sensor pins, buzzer pin, camera, WiFi connection, SPIFFS initialization, and HTTP server routes.

Loop Function

C++
void loop() {
  // Handle client requests
  server.handleClient();

  // Check PIR sensor for motion detection
  motionDetected = digitalRead(PIR_PIN) == HIGH;

  // Check IR sensor for flash requirement
  flashNeeded = digitalRead(IR_PIN) == HIGH;

  // If motion is detected
  if (motionDetected) {
    // Turn on buzzer
    digitalWrite(BUZZER_PIN, HIGH);

    // Capture an image
    // (image capture code here)

    // Turn on flash if needed
    if (flashNeeded) {
      // (flash control code here)
    }

    // Save the image
    // (image saving code here)

    // Turn off flash
    if (flashNeeded) {
      // (flash control code here)
    }

    // Turn off buzzer
    digitalWrite(BUZZER_PIN, LOW);
  }
}
  • Handles client requests for the HTTP server, checks the PIR sensor for motion detection, and the IR sensor for flash requirement. If motion is detected, it turns on the buzzer, captures an image, saves it, and controls the flash accordingly.

Web Server Handlers

C++
void handleRoot() {
  // Send root page content
  server.send(200, "text/html", "<h1>Home Intrusion Detection System</h1><p>Secure your home with our system!</p>");
}

void handleUpload() {
  // Handle file uploads
  // (file upload handling code here)
}

void handleImages() {
  // List images
  // (image listing code here)
}

void handleImage() {
  // Display individual image
  // (image display code here)
}

  • Defines functions to handle root page, file uploads, image listing, and individual image display for the web server.

Conclusion :

    By following this guide, you can create a cost-effective home intrusion detection system using readily available components. This system provides a basic level of security for your home and can be further expanded and customized to suit your needs.

Post a Comment (0)
Previous Post Next Post