Tech Gyan Set
Published © MIT

IoT-Based Smart Door Security System

An IoT-based smart door security system that detects intrusions instantly and sends real-time alerts for enhanced home safety. πŸ”πŸ“‘

BeginnerFull instructions provided8 hours618
IoT-Based Smart Door Security System

Things used in this project

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

πŸ” IoT-Based Smart Door Security System βœ… Complete ESP32 Code

C/C++
#include <WiFi.h>
#include <PubSubClient.h>

// ----------------------
// Pin Configuration
// ----------------------
#define DOOR_SENSOR 4     // Magnetic reed switch
#define PIR_SENSOR 5      // Motion sensor
#define BUZZER 18         // Alarm buzzer

// ----------------------
// WiFi Credentials
// ----------------------
const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASSWORD";

// ----------------------
// MQTT Configuration
// ----------------------
const char* mqtt_server = "broker.hivemq.com";
const char* topic_alert = "smartdoor/alert";
const char* topic_status = "smartdoor/status";

WiFiClient espClient;
PubSubClient client(espClient);

// ----------------------
// Variables
// ----------------------
bool lastDoorState = HIGH;
bool alarmTriggered = false;

// ----------------------
// WiFi Setup
// ----------------------
void setup_wifi() {
  Serial.println("Connecting to WiFi...");
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("\nWiFi Connected");
}

// ----------------------
// MQTT Reconnect
// ----------------------
void reconnect() {
  while (!client.connected()) {
    Serial.println("Connecting to MQTT...");
    if (client.connect("SmartDoorClient")) {
      Serial.println("MQTT Connected");
    } else {
      delay(2000);
    }
  }
}

// ----------------------
// Setup Function
// ----------------------
void setup() {

  Serial.begin(115200);

  pinMode(DOOR_SENSOR, INPUT_PULLUP);
  pinMode(PIR_SENSOR, INPUT);
  pinMode(BUZZER, OUTPUT);

  digitalWrite(BUZZER, LOW);

  setup_wifi();
  client.setServer(mqtt_server, 1883);
}

// ----------------------
// Main Loop
// ----------------------
void loop() {

  if (!client.connected()) {
    reconnect();
  }

  client.loop();

  bool doorState = digitalRead(DOOR_SENSOR);
  bool motionState = digitalRead(PIR_SENSOR);

  // Door Open Detection
  if (doorState == LOW && lastDoorState == HIGH) {
    Serial.println("πŸšͺ Door Opened!");
    client.publish(topic_alert, "Door Opened");
    digitalWrite(BUZZER, HIGH);
    alarmTriggered = true;
  }

  // Motion Detection
  if (motionState == HIGH) {
    Serial.println("🚨 Motion Detected!");
    client.publish(topic_alert, "Motion Detected");
    digitalWrite(BUZZER, HIGH);
    alarmTriggered = true;
  }

  // Reset Alarm after 5 seconds
  if (alarmTriggered) {
    delay(5000);
    digitalWrite(BUZZER, LOW);
    alarmTriggered = false;
  }

  lastDoorState = doorState;

  delay(500);
}

Credits

Tech Gyan Set
28 projects β€’ 12 followers
Tech Gyan Set | IoT & Embedded Systems Creator | Arduino, ESP32, GSM & NodeMCU Projects | Smart Home & Real-Life Automation Tutorials
Thanks to Tech Gyan Set .

Comments