top of page
Search

Build a Smart Home Security System with ESP8266 and Telegram Notifications

Updated: May 28

Introduction to the Home Security System

In this tutorial, we will build a simple yet effective home security system using an ESP8266, an ultrasonic sensor, and Telegram for real-time notifications. This system detects motion and alerts you via Telegram messages when an object is detected within a certain range. This is a great way to enhance your home's security with minimal components.


Components Required for the Project

To follow along, you will need the following components:


  • ESP8266 NodeMCU

  • Ultrasonic Sensor (HC-SR04)

  • Buzzer

  • LED

  • Jumper Wires


Features of the Security System

This security system includes several features:

  • ✔️ Motion Detection Using an Ultrasonic Sensor

  • ✔️ Alerts Sent to Your Telegram App

  • ✔️ Buzzer and LED Activation on Detection

  • ✔️ Wi-Fi Connectivity for Remote Monitoring


Step 1: Setting Up Your Telegram Bot

To receive alerts on Telegram, you need to create a Telegram bot and obtain a bot token and chat ID. This process is straightforward and can be done in a few simple steps.


Creating the Telegram Bot

  1. Open Telegram and search for BotFather.

  2. Start a chat and type `/newbot`.

  3. Follow the instructions to name your bot.

  4. Copy the Bot Token provided by BotFather.


Getting Your Chat ID

  1. Search for IDBot in Telegram.

  2. Start a chat and type `/getid`.

  3. Copy your Chat ID for later use.


Step 2: Circuit Diagram Setup

Now that you have your bot set up, let's wire the components.


Wiring Instructions

  • Connect the ultrasonic sensor to D1 (TRIG) and D2 (ECHO).

  • Connect the buzzer to D7 and the LED to D6.


ESP8266 Pins

Component

HC-SR04 Pins

D1

Trigger

TRIG

D2

Echo

ECHO

D6

LED

-

D7

Buzzer

-

GND

GND

GND

3.3V

VCC

VCC



Step 3: Code Implementation

Now that the circuit is assembled, you can write the code for the home security system. This code will allow your ESP8266 to communicate with the ultrasonic sensor and send alerts via Telegram.


```cpp

include <ESP8266WiFi.h>

include <WiFiClientSecure.h>

include <UniversalTelegramBot.h>


// Wi-Fi credentials

const char* ssid = "your_SSID";

const char* password = "your_PASSWORD";


// Telegram Bot Token and Chat ID

const String botToken = "your_BOT_TOKEN";

const String chatId = "your_CHAT_ID";


// Define ultrasonic sensor pins

const int TRIG_PIN = D1;

const int ECHO_PIN = D2;


// Other pin definitions

const int LED_PIN = D6;

const int BUZZER_PIN = D7;


// Initialize telegram bot

UniversalTelegramBot bot(botToken, WiFiClientSecure());


void setup() {

Serial.begin(115200);

pinMode(TRIG_PIN, OUTPUT);

pinMode(ECHO_PIN, INPUT);

pinMode(LED_PIN, OUTPUT);

pinMode(BUZZER_PIN, OUTPUT);

connectWiFi();

}


void loop() {

long duration, distance;

// Trigger the sensor

digitalWrite(TRIG_PIN, LOW);

delayMicroseconds(2);

digitalWrite(TRIG_PIN, HIGH);

delayMicroseconds(10);

digitalWrite(TRIG_PIN, LOW);

duration = pulseIn(ECHO_PIN, HIGH);

distance = (duration * 0.034) / 2; // Calculate distance in cm


if (distance < 10) { // Alert if an object is detected within 10 cm

alert();

delay(10000); // Prevent spamming

}

}


void connectWiFi() {

Serial.print("Connecting to WiFi...");

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {

delay(1000);

Serial.print(".");

}

Serial.println(" connected!");

}


void alert() {

digitalWrite(LED_PIN, HIGH);

digitalWrite(BUZZER_PIN, HIGH);

bot.sendMessage(chatId, "ALERT! MOTION DETECTED!!", "");

delay(1000);

digitalWrite(LED_PIN, LOW);

digitalWrite(BUZZER_PIN, LOW);

}

```


Step 4: Explanation of the Code

The code is divided into several sections that allow for seamless integration of the components:


  1. Wi-Fi and Telegram Setup

    • The ESP8266 connects to Wi-Fi.

    • A secure Telegram connection is established using UniversalTelegramBot.


  2. Sensor and Output Pin Configuration

    • `TRIG_PIN` is set as an OUTPUT to send signals.

    • `ECHO_PIN` is set as an INPUT to receive signals.

    • The LED and Buzzer are used for alerts.


  3. Distance Measurement

    • The ultrasonic sensor measures the distance of objects in front of it.

    • If an object is detected within 10 cm, an alert is triggered.


  4. Sending Telegram Notifications

    • If motion is detected, a message is sent to Telegram.

    • To prevent spamming, messages are sent only every 10 seconds.


Step 5: Uploading the Code to ESP8266

Follow these steps to upload the code:


  1. Open Arduino IDE.

  2. Install the following libraries:

    • ESP8266WiFi

    • WiFiClientSecure

    • UniversalTelegramBot


  3. Connect the ESP8266 to your PC.

  4. Select Board: NodeMCU 1.0 and upload the code.

  5. Open the Serial Monitor to check connectivity.


Step 6: Testing the Security System

Once the code is uploaded, it's time for testing!


  1. Power the ESP8266 and wait for a Wi-Fi connection.

  2. Place an object within 10 cm of the sensor.

  3. The buzzer and LED should turn on.

  4. You should receive a Telegram alert saying:

    • "ALERT! MOTION DETECTED!!"


Conclusion

Congratulations! 🎉 You have successfully built a home security system using ESP8266 and Telegram notifications. Now, anytime motion is detected, you will receive an instant alert on your Telegram app.


This project can be expanded with several features:

  • Remote control using Telegram commands

  • Integration with a camera module for snapshots

  • Cloud-based data logging for intrusion tracking


Try it out and make your home smarter! 🏠🔔

 
 
 

Recent Posts

See All
Creative Ideas for Building Your Own Gadgets

Building your own gadgets is an exciting way to explore technology and create something uniquely yours. Whether you are a tech enthusiast or just someone who loves to tinker, DIY gadgets offer endless

 
 
 

Comments


bottom of page