DIY Room Intruder Alert System Using ESP32, Ultrasonic Sensor, and Telegram

DIY Room Intruder Alert System Using ESP32, Ultrasonic Sensor, and Telegram

Ever wanted to get a notification when someone enters your room? In this DIY project, we’ll show you how to build a smart intruder alert system using an ESP32, ultrasonic sensor, and Telegram—completely free, no third-party paid services!


🧰 Components Needed

  • ESP32 Development Board (e.g., DOIT ESP32 DEVKIT V1)
  • HC-SR04 Ultrasonic Sensor
  • Jumper Wires
  • USB Cable for ESP32
  • WiFi connection
  • Telegram App (for bot notifications)

🔌 Wiring Diagram

HC-SR04 PinESP32 Pin
VCC5V
GNDGND
TrigD5 (GPIO5)
EchoD18 (GPIO18)

Note: While your ESP32 board might have D-labels (D1, D2, etc.), internally we use GPIO numbers in code.


📲 Telegram Bot Setup

  1. Open Telegram and search for @BotFather
  2. Send /start, then /newbot
  3. Name your bot and assign it a username (must end in _bot)
  4. Save the Bot Token provided

Get Your Chat ID

  1. Open this URL in your browser (replace <TOKEN>):
    https://api.telegram.org/bot<TOKEN>/getUpdates
  2. Send any message to your bot
  3. Refresh the page to find your chat ID in the JSON response:
    "chat":{"id":12345678,...}

💻 Code for ESP32

#include &lt;WiFi.h&gt;
#include &lt;WiFiClientSecure.h&gt;

const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";

String botToken = "YOUR_BOT_TOKEN";
String chatID = "YOUR_CHAT_ID";

#define trigPin 5
#define echoPin 18

WiFiClientSecure client;

void setup() {
  Serial.begin(115200);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  WiFi.begin(ssid, password);
  Serial.print("Connecting to WiFi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nConnected!");
  client.setInsecure();  // Skip SSL certificate validation
}

void loop() {
  long duration;
  float distance;

  // Trigger ultrasonic sensor
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2;

  Serial.print("Distance: ");
  Serial.println(distance);

  if (distance &lt; 50) {
    sendTelegramMessage("🚨 Someone just entered in your room!");
    delay(10000);  // Wait 10 sec before next message
  }

  delay(500);
}

void sendTelegramMessage(String message) {
  if (WiFi.status() == WL_CONNECTED) {
    if (client.connect("api.telegram.org", 443)) {
      String url = "/bot" + botToken + "/sendMessage?chat_id=" + chatID + "&text=" + message;
      client.print(String("GET ") + url + " HTTP/1.1\r\n" +
                   "Host: api.telegram.org\r\n" +
                   "Connection: close\r\n\r\n");
      delay(500);
    }
  }
}

🎯 How It Works

  • The ultrasonic sensor continuously checks the distance in front of it.
  • If the distance is less than 50 cm, it assumes someone entered the room.
  • It sends a message via the Telegram Bot API directly to your Telegram app.
  • There’s a 10-second cooldown to prevent spamming.

💡 Real-World Applications

  • Room intrusion alert
  • Baby movement detection
  • Office security
  • Pet activity monitoring
  • Motion-triggered automation

🧪 Tips and Enhancements

  • 📏 Fine-tune the distance threshold (if (distance < 50)) based on your setup
  • ⏱️ Use multiple readings and averaging to reduce false alerts
  • 💡 Add LED or buzzer for local alerts
  • 📷 Integrate with ESP32-CAM to send photos
  • 🕐 Add time-based filters (e.g., only notify at night)

output:

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply

    Your email address will not be published. Required fields are marked *