Security system with Telegram notification

Security system with Telegram notification - ESP32 / ESP8266 project with PIR sensor

Reading time: 4 minute

What it is?

Although the security systems currently on the market are quite sophisticated and constantly evolving, there are probably people among us who want to create such a system themselves, or want to have one of backup in case the primary one fails.

In this series of articles we will present you building a DIY security system from modules and development boards available online at a decent price.

In this article we will show you how you can build a motion detection system with Telegram notification, using a PIR sensor (Passive InfraRed sensor) and an ESP 8266 development board (or ESP32).

Materials needed to carry out this project:

  • An ESP 8266 or ESP32 development board;
  • A PIR sensor HC-SR501 (we recommend the large version with potentiometers for adjustment);
  • An AP (Access Point) with internet access;
  • A 5V source;

How does the project work?

The principle of operation of this project is simple and consists in the property of a PIR sensor to detect the change of position of an object. The PIR sensor will send a 3.3V signal whose duration can be adjusted using an existing potentiometer on the sensor.

This signal will reach the development board ESP8266 which will send you a message on the Telegram with the help of a bot. The second potentiometer on the sensor is to adjust its sensitivity.

How do we create the muzzle for Telegram?

A bot (or entire: robot) is a computer program that takes over various automated tasks. First you need to install Telegram application from "Play Store", "Play Store" or "App Store".

After installing the Telegram application, create an account and start a conversation with "Botfather", in which you write "/ start", and after that "/ newbot".

After that, choose a name for your muzzle; if the name is available you will receive a message like the one in the picture below:

After performing this operation, search user "IDBot" and in the conversation write the command "/ getid", and in response you should receive a code.

The software part

In order to compile the code we will present, you must download the following library: https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot, and install it in Arduino IDE.

To install the library in the Arduino IDE you just have to enter the menu Sketch -> Include Library -> Add .zip library, then select the archive previously downloaded from GitHub.

To use the Telegram API you also need the library "arduinosson" which you can easily install by going to the menu Sketch> Include Library> Manage Libraries, where to look "ArduinoJson" and install the latest version available (the version was used for this tutorial 6.5.12).

Now you need to enter a web browser and look for the following address: https://t.me/"replace with bot name ".

The hardware part

To connect PIR sensor (right) to development board (left), you must make the following connections:

  1. 5V - Vcc
  2. GND - GND
  3. D5 (GPIO14) - Out

The code for ESP8266

#include "ESP8266WiFi.h"
#include "WiFiClientSecure.h"
#include "UniversalTelegramBot.h"
#include "ArduinoJson.h"

const char * ssid = "****"; // replace **** with the name of your WiFi network
const char * password = "***"; // replace *** with your WiFi network password

#define BOTtoken "XXXXXXXXXX: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#define CHAT_ID "XXXXXXXXXX" // id number received from IDBot

WiFiClientSecure client;
UniversalTelegramBot bot (BOTtoken, client);

const int motionSensor = 14; // PIR Motion Sensor
bool motionDetected = false;

// Indicates when motion is detected
void ICACHE_RAM_ATTR detectsMovement () {
//Serial.println("MOTION DETECTED !!! ");
motionDetected = true;
}

void setup () {
Serial.begin (115200);
client.setInsecure ();

// PIR Motion Sensor mode INPUT_PULLUP
pinMode (motionSensor, INPUT_PULLUP);
// Set motionSensor pin as interrupt, assign interrupt function and set RISING mode
attachInterrupt (digitalPinToInterrupt (motionSensor), detectsMovement, RISING);

// Attempt to connect to Wifi network:
Serial.print ("Connecting Wifi:");
Serial.println (ssid);

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

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

Serial.println ("");
Serial.println ("WiFi connected");
Serial.print ("IP address:");
Serial.println (WiFi.localIP ());

bot.sendMessage (CHAT_ID, "Pir sensor initialized", "");
}

void loop () {
if (motionDetected) {
bot.sendMessage (CHAT_ID, "Motion detected !!", "");
Serial.println ("Motion Detected");
motionDetected = false;
}
}

Code for ESP32:

#include "WiFi.h"
#include "WiFiClientSecure.h"
#include "UniversalTelegramBot.h"
#include "ArduinoJson.h"

const char * ssid = "****"; // replace **** with the name of your WiFi network
const char * password = "***"; // replace *** with your WiFi network password

#define BOTtoken "XXXXXXXXXX: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

#define CHAT_ID "XXXXXXXXXX" // id number received from IDBot

WiFiClientSecure client;
UniversalTelegramBot bot (BOTtoken, client);

const int motionSensor = 14; // PIR Motion Sensor
bool motionDetected = false;

// Indicates when motion is detected
void ICACHE_RAM_ATTR detectsMovement () {
//Serial.println("MOTION DETECTED !!! ");
motionDetected = true;
}

void setup () {
Serial.begin (115200);

// PIR Motion Sensor mode INPUT_PULLUP
pinMode (motionSensor, INPUT_PULLUP);
// Set motionSensor pin as interrupt, assign interrupt function and set RISING mode
attachInterrupt (digitalPinToInterrupt (motionSensor), detectsMovement, RISING);

// Attempt to connect to Wifi network:
Serial.print ("Connecting Wifi:");
Serial.println (ssid);

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

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

Serial.println ("");
Serial.println ("WiFi connected");
Serial.print ("IP address:");
Serial.println (WiFi.localIP ());

bot.sendMessage (CHAT_ID, "Pir sensor initialized", "");
}

void loop () {
if (motionDetected) {
bot.sendMessage (CHAT_ID, "Motion detected !!", "");
Serial.println ("Motion Detected");
motionDetected = false;
}
}

How does the code work?

#include "WiFi.h"
#include "WiFiClientSecure.h"
#include "UniversalTelegramBot.h"
#include "ArduinoJson.h"

These are the libraries we need to run the code.

attachInterrupt (digitalPinToInterrupt (motionSensor), detectsMovement, RISING);

Here we attribute pin 14 (motion sensor), as an interrupt pin; when the voltage on the pin rises, the detectsMovment () function will be executed.

Serial.print ("Connecting Wifi:");
Serial.println (ssid);

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

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

Serial.println ("");
Serial.println ("WiFi connected");
Serial.print ("IP address:");
Serial.println (WiFi.localIP ());

In this part of the code the development board connects to the WiFi network specified at the beginning of the code; also write in the Serial Monitor the IP address of the development board and the name of the network to which it is connected;

if (motionDetected) {
bot.sendMessage (CHAT_ID, "Motion detected !!", "");
Serial.println ("Motion Detected");
motionDetected = false;
}

In this section of the code the "if" loop checks if the value of the variable "motionDetected" has been modified by the function "detectsMovment ()" in adevarat (true); If it has been modified, the development board will send us an alert message on the Telegram and will restore the value of the variable back to false (false).

Bibliography:

https://www.elektor.com/nodemcu

https://hackster.imgix.net/

https://www.elektor.com/hc-sr501

https://randomnerdtutorials.com/

 1,520 total hits, 3 hits today

4 comments

  1. Wifi means radio, radio means broadcast-reception, reception means noise threshold. Two diodes head to head and a piece of wire connected to a power supply means the receiving pope within a radius of several hundred meters. So radio security? On the other hand you can program some fallback in case of nasty, for example with an older version of espeasy the processor freezes if it can't connect to the mqtt server, so in programming you can first check if you have access to the server.

  2. Hi,
    can someone help me complete the code for the esp8266 so that I can receive notifications from multiple sensors? Notifications should be different, depending on the sensor that triggers.

    Thank you.

Add a comment

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

The maximum upload file size: 2 MB. you can upload: image, audio, video, document, spreadsheet, Interactive, text, archive, queues, Other. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded. Drop files here