IoT based home security system using Nodemcu ESP8266 module and Telegram app

IoT based home security system using Nodemcu ESP8266 module and Telegram appHello, welcome back to another article from SriTu Hobby. Today we are going to talk about how to make an IoT-based home security system using the Nodemcu ESP8266 module and Telegram app. Also, the PIR motion sensor and the Nodemcu ESP8266 board are mainly used for this tutorial. We have presented this project using Arduino in a previous tutorial. You can read it using this link. But this project belongs to IoT technology. When there is any movement at the point where this security system is connected, it can be viewed through the Telegram app on our mobile phone. We can also easily use this project for home, office, workplace, etc. Keep reading.

How does the PIR security system work?

When powering this system, First, the Nodemcu board connects to the Internet connection via the local IP address. Then, the system goes into activation mode. After, displayed as “System startup” on the LCD and Telegram app. Also, the green LED bulb turns ON. Next, when a movement occurs, it is captured by the PIR sensor and sent to the Nodemcu board. Then, the red LED and the buzzer will activate for a short time. Also, displayed as “Motion detected” in the Telegram app and on the LCD. Then, the system returns to a normal situation. Then, the green LED bulb will turn on, and the LCD will display “NO MOTION”.

OK, let’s do this project step by step. For that, the required components are given below.

Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.

Step 1

Firstly, identify these components.

Nodemcu ESP8266

IoT based home security system using Nodemcu ESP8266 module and Telegram app

PIR motion sensor

LCD display

I2C module

LED bulb

180-ohm resistor

Buzzer

Breadboard

Jumper wires

Step 2

Secondly, connect these components, To do this, use the circuit diagram below.

IoT based home security system using Nodemcu ESP8266 module and Telegram app

Step 3

Thirdly, let’s set up the Telegram app. For that, use the steps below.

  • First, download and install the Telegram app using a mobile phone or computer.IoT based home security system using Nodemcu ESP8266 module and Telegram app
  • Then, run your telegram app and create a new account using your phone number. Now, click on the search bar at the top and enter “Botfather”. After, click the “Botfather” account.IoT based home security system using Nodemcu ESP8266 module and Telegram app
  • Next, click the Start button. Then, send the text as “/ newbot”.IoT based home security system using Nodemcu ESP8266 module and Telegram app
IoT based home security system using Nodemcu ESP8266 module and Telegram app
  • Afterward, enter the suitable name and username. Now, you can see the token. Copy and paste it to another location. This is required for the project program.IoT based home security system using Nodemcu ESP8266 module and Telegram app
  • Now, go to the home page and click on the search bar again. Enter “Idbot” here. After, click the “IDBot” account.
IoT based home security system using Nodemcu ESP8266 module and Telegram app
  • Now, click the start button. After, send the text as “/getid”. OK, now copy and paste this ID to another location. This is also required for the project program.
IoT based home security system using Nodemcu ESP8266 module and Telegram app

Step 4

OK, now let’s set up the program for this project. It is as follows.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>


LiquidCrystal_I2C lcd (0x27, 16, 2);
const char* ssid = "Dialog 4G 025";
const char* password = "b2016ee3";


#define BOTtoken ""
#define CHAT_ID ""

#define Sensor D0
#define LEDR D3
#define LEDG D4
#define Buzzer D5

X509List cert(TELEGRAM_CERTIFICATE_ROOT);
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);


void setup() {
  Serial.begin(115200);
  lcd.init();
  lcd.backlight();
  Wire.begin(D2, D1);
  configTime(0, 0, "pool.ntp.org");      // get UTC time via NTP
  client.setTrustAnchors(&cert); // Add root certificate for api.telegram.org

  pinMode(Sensor, INPUT);
  pinMode(LEDR, OUTPUT);
  pinMode(LEDG, OUTPUT);
  pinMode(Buzzer, OUTPUT);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  int a = 0;
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    lcd.setCursor(a, 0);
    lcd.print(".");
    delay(500);
    a++;
  }

  Serial.println("");
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("WiFi connected");
  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  lcd.setCursor(0, 1);
  lcd.print(WiFi.localIP());
  Serial.println(WiFi.localIP());
  delay(500);

  bot.sendMessage(CHAT_ID, "System started", "");
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("System started");
  delay(1000);
  lcd.clear();
}
void loop() {
  bool value = digitalRead(Sensor);
  Serial.println(value);
  if (value == 1) {
    Serial.println("Motion Detected");
    digitalWrite(LEDR, HIGH);
    digitalWrite(Buzzer, HIGH);
    digitalWrite(LEDG, LOW);
    lcd.setCursor(0, 0);
    lcd.print("Motion Detected");
    bot.sendMessage(CHAT_ID, "Motion detected!!", "");
  } else if (value == 0) {
    digitalWrite(LEDR, LOW);
    digitalWrite(Buzzer, LOW);
    digitalWrite(LEDG, HIGH);
    lcd.setCursor(0, 0);
    lcd.print("No Motion      ");
  }
}

Code explanation

Firstly, libraries are included.

#include <Wire.h>
 #include <LiquidCrystal_I2C.h>
 #include <ESP8266WiFi.h>
 #include <WiFiClientSecure.h>
 #include <UniversalTelegramBot.h>

Secondly, we need to enter WIFI connection details and Telegram bot details. For that, use your WIFI connection details and Telegram bot details.

Enter your WIFI connection details.

 const char* ssid = "Dialog 4G 025";
 const char* password = "b2016ee3";

Enter your Telegram bot token as we obtained earlier.

 #define BOTtoken "*****************************************"

Enter your Telegram chat ID as we obtained earlier.

 #define CHAT_ID "**************"


IoT based home security system using Nodemcu ESP8266 module and Telegram app

Thirdly, the LED, sensor, and buzzer pins are defined.

 #define Sensor D0
 #define LEDR D3
 #define LEDG D4
 #define Buzzer D5

Next, create objects for the I2C library and telegram library.

 LiquidCrystal_I2C lcd (0x27, 16, 2);
 X509List cert(TELEGRAM_CERTIFICATE_ROOT);
 WiFiClientSecure client;
 UniversalTelegramBot bot(BOTtoken, client);

In the setup function, the serial monitor, LCD, wire library, and WIFI library are started.

 Serial.begin(115200);
 lcd.init();
 lcd.backlight();
 Wire.begin(D2, D1);
 WiFi.mode(WIFI_STA);
 WiFi.begin(ssid, password);

Next, the configuration time and root certificate for api.telegram.org are included. Also, the LEDs, buzzer, and sensor PIN are set as output.

 configTime(0, 0, "pool.ntp.org"); 
 client.setTrustAnchors(&cert);
 pinMode(Sensor, INPUT);
 pinMode(LEDR, OUTPUT);
 pinMode(LEDG, OUTPUT);
 pinMode(Buzzer, OUTPUT);

Then, the Nodemcu board connects to the Internet, and “WIFI Connected” and the local IP address are displayed on the LCD.

int a = 0;
 while (WiFi.status() != WL_CONNECTED) {
 Serial.print(".");
 lcd.setCursor(a, 0);
 lcd.print(".");
 delay(500);
 a++;
 }

Serial.println("");
 lcd.clear();
 lcd.setCursor(0, 0);
 lcd.print("WiFi connected");
 Serial.println("WiFi connected");
 Serial.print("IP address: ");
 lcd.setCursor(0, 1);
 lcd.print(WiFi.localIP());
 Serial.println(WiFi.localIP());
 delay(500);

Next, this code sends a message to indicate that the bot has started. Also displayed as “System startup” in the Telegram app and LCD.

bot.sendMessage(CHAT_ID, "System startup", "");
 lcd.clear();
 lcd.setCursor(0, 0);
 lcd.print("System startup");
 delay(1000);
 lcd.clear();

In the loop function, values are obtained from the PIR sensor and entered into the Boolean variable. Also, those values are checked using the IF condition. If the value is 1, prints as “Motion detected” on the serial monitor and LCD. Then, the red LED and buzzer are activated. Also, the message “Motion detected” is sent to the telegram application. If the value is 0, the green LED is activated and prints “No Motion” on the LCD screen

 void loop() {
 bool value = digitalRead(Sensor);
 Serial.println(value);
 if (value == 1) {
 Serial.println("Motion Detected");
 digitalWrite(LEDR, HIGH);
 digitalWrite(Buzzer, HIGH);
 digitalWrite(LEDG, LOW);
 lcd.setCursor(0, 0);
 lcd.print("Motion Detected");
 bot.sendMessage(CHAT_ID, "Motion detected!!", "");
 } else if (value == 0) {
 digitalWrite(LEDR, LOW);
 digitalWrite(Buzzer, LOW);
 digitalWrite(LEDG, HIGH);
 lcd.setCursor(0, 0);
 lcd.print("No Motion       ");
 }
 }

Step 5

OK, now select board and port. Afterward, upload this code to the Nodemcu board.

Step 6

Next, go back to the telegram app and click on the “Botfather” we created earlier. After, click the start button.

Finally, check out this project using motions. OK, enjoy this project. The full video guide is given below. So, we will meet in the next tutorial. Have a good day.

IoT based home security system using Nodemcu ESP8266 module and Telegram app

Similar Posts

Leave a Reply

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