IoT/ESP32 B.Tech/Diploma Beginner

Telegram Bot: ESP32 Motion Detection Alerts

Send notifications to your Telegram account when the ESP32 detects motion. You'll receive the notifications from anywhere and program the ESP with Arduino IDE.

  • Suitable forB.Tech/Diploma IoT/ESP32 students
  • Build time1-2 weeks
  • LevelBeginner
  • Available atAjvika Labs, Kakinada

About this project

This tutorial shows how to send notifications to your Telegram account when the ESP32 detects motion. As long as you have access to the internet in your smartphone, you’ll be notified no matter where you are. The ESP board will be programmed using Arduino IDE.
Project Overview
This tutorial shows how to get notifications in your Telegram account when the ESP32 detects motion.
Here’s an overview on how the project works:
You’ll create a Telegram bot for your ESP32.
The ESP32 is connected to a PIR motion sensor.
When the sensor detects motion, the ESP32 sends a warning message to your telegram account.
You’ll be notified in your telegram account whenever motion is detected.
This is a simple project, but shows how you can use Telegram in your IoT and Home Automation projects. The idea is to apply the concepts learned in your own projects.
Introducing Telegram
Messenger is a cloud-based instant messaging and voice over IP service. You can easily install it in your smartphone (Android and iPhone) or computer (PC, Mac and Linux). It is free and without any ads. Telegram allows you to create bots that you can interact with.
“
Bots are third-party applications that run inside Telegram. Users can interact with bots by sending them messages, commands and inline requests. You control your bots using HTTPS requests to Telegram Bot API
“.
The ESP32 will interact with the Telegram bot to send messages to your telegram account. Whenever motion is detected, you’ll receive a notification in your smartphone (as long as you have access to the internet).
Creating a Telegram Bot
Go to Google Play or App Store, download and install
Open Telegram and follow the next steps to create a Telegram Bot. First, search for “
botfather
” and click the BotFather as shown below. Or open this link
in your smartphone.
The following window should open and you’ll be prompted to click the
start
button.
Type
/newbot
and follow the instructions to create your bot. Give it a name and username.
If your bot is successfully created, you’ll receive a message with a link to access the bot and the
bot token
. Save the bot token because you’ll need it so that the ESP32 can interact with the bot.
Get Your Telegram User ID
Anyone that knows your bot username can interact with it. To make sure that we ignore messages that are not from our Telegram account (or any authorized users), you can get your Telegram User ID. Then, when your telegram bot receives a message, the ESP can check whether the sender ID corresponds to your User ID and handle the message or ignore it.
In your Telegram account, search for “IDBot” or open this link
in your smartphone.
Start a conversation with that bot and type
/getid
. You will get a reply back with your user ID. Save that
user ID
, because you’ll need it later in this tutorial.
Preparing Arduino IDE
We’ll program the
ESP32
board using Arduino IDE, so make sure you have them installed in your Arduino IDE.
Installing the ESP32 Board in Arduino IDE (Windows, Mac OS X, Linux)
Universal Telegram Bot Library
To interact with the Telegram bot, we’ll use the
Universal Telegram Bot Library
created by Brian Lough that provides an easy interface for the Telegram Bot API.
Follow the next steps to install the latest release of the library.
Click here to download the Universal Arduino Telegram Bot library
Go to
Sketch
>
Include Library
>
Add.ZIP Library..
Add the library you’ve just downloaded.
Important:
don’t install the library through the Arduino Library Manager because it might install a deprecated version.
For all the details about the library, take a look at the Universal Arduino Telegram Bot Library
GitHub
page.
ArduinoJson Library
You also have to install the
ArduinoJson
library. Follow the next steps to install the library.
Go to
Skech
>
Include Library
>
Manage Libraries
Search for “ArduinoJson”.
Install the library.
We’re using ArduinoJson library version 6.5.12.
Parts Required
For this project, you need the following parts:
ESP32 board
(read
Best ESP32 dev boards
)
Mini PIR motion sensor (AM312)
or
PIR motion sensor (HC-SR501)
Jumper wires
Breadboard
Schematic Diagram
For this project you need to wire a PIR motion sensor to your ESP32 board. Follow the next schematic diagram.
In this example, we’re wiring the PIR motion sensor data pin to
GPIO 27
. You can use any other suitable GPIO. Read
ESP32 GPIO Guide
Telegram Motion Detection with Notifications – ESP32 Sketch
The following code uses your Telegram bot to send a warning message to your telegram account whenever motion is detected. To make this sketch work for you, you need to insert your network credentials (SSID and password), the Telegram Bot token and your Telegram user ID.
/*

Project created using Brian Lough's Universal Telegram Bot Library: https://github.com/witnessmenow/Universal-Arduino-
*/

#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>

// Replace with your network credentials
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

// Initialize Telegram BOT
#define BOTtoken "XXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get from Botfather)

// Use @myidbot to find out the chat ID of an individual or a group
// Also note that you need to click "start" on a bot before it can
// message you
#define CHAT_ID "XXXXXXXXXX"

WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);

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

// Indicates when motion is detected
void IRAM_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);
client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org

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, "Bot started up", "");
}

void loop() {
if(motionDetected){
bot.sendMessage(CHAT_ID, "Motion detected!!", "");
Serial.println("Motion Detected");
motionDetected = false;
}
}
How the Code Works
This sections explain how the code works. Continue reading or skip to the
Demonstration
section.
Start by importing the required libraries.
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
Network Credentials
Insert your network credentials in the following variables.
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";
Telegram Bot Token
Insert your Telegram Bot token you’ve got from Botfather on the
BOTtoken
variable.
#define BOTtoken "XXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get from Botfather)
Telegram User ID
Insert your chat ID. The one you’ve got from the IDBot.
#define CHAT_ID "XXXXXXXXXX"
Create a new WiFi client with
WiFiClientSecure
WiFiClientSecure client;
Create a
bot
with the token and client defined earlier.
UniversalTelegramBot bot(BOTtoken, client);
Motion Sensor
Define the GPIO that the motion sensor is connected to.
const int motion

Technologies used

ESP32ArduinoWiFiBLEPIRAndroidArduino IDE

Hardware components

  • ESP32
  • Arduino
  • PIR

Software & tools

  • Arduino IDE

What you get

  • Working project / hardware kit, tested before delivery
  • Complete source code with explanation
  • Project demo and guidance for your viva
  • Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed. Payment is in two parts: • 50% advance to start the project • Remaining 50% before delivery Note: • Documentation and PPT are not included by default • If you need documentation/PPT, it can be arranged for extra time and cost We'll confirm the exact timeline and cost with you after reviewing your requirements.