ESP32 Door Status Monitor with Telegram Notifications
Build a monitor status door using an ESP32 board and a magnetic reed switch. You'll receive a message in your Telegram account whenever the door changes state: opened or closed.
- Suitable forB.Tech/Diploma IoT/ESP32 students
- Build time1-2 weeks
- LevelBeginner
- Available atAjvika Labs, Kakinada
About this project
In this project, you’re going to monitor the status of a door using an ESP32 board and a magnetic reed switch. You’ll receive a message in your Telegram account whenever the door changes state: opened or closed. As long as you have access to the internet on your smartphone, you’ll be notified no matter where you are. The ESP32 board will be programmed using Arduino IDE.
We have a similar tutorial that sends emails instead of Telegram messages:
ESP32 Door Status Monitor with Email Notifications (IFTTT)
Read the ESP8266 Guide:
Door Status Monitor with Telegram Notifications
Project Overview
In this project, we’ll create a Telegram Bot that will send messages to your Telegram account whenever a door changes state. To detect the change, we’ll use a magnetic contact switch.
A magnetic contact switch is basically a reed switch encased in a plastic shell so that you can easily apply it to a door, a window, or a drawer to detect if it is open or closed.
The electrical circuit is closed when a magnet is near the switch—door closed. When the magnet is far away from the switch—door open—the circuit is open. See the figure below.
We can connect the reed switch to an ESP32 GPIO to detect changes in its state.
Introducing Telegram
Messenger is a cloud-based instant messaging and voice over IP service. You can easily install it on 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 the door changes state, you’ll receive a notification on 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
on 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. Mine is called
Door Sensor
, and the username is
ESPDoorSensorBot
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.
Sending a Message to the Bot
This step is very important. Don’t miss it. Otherwise, the project will not work.
You must send a message to your Telegram Bot from your Telegram account before it can send you messages.
Go back to the chats tab, and in the search field, type the username of your bot.
Select your bot to start a conversation.
Click on the Start link.
And that’s it! You can proceed to the next section.
Get Your Telegram User ID
To send a message to your Telegram account, the bot needs to know your user ID.
In your Telegram account, search for “
myidbot
” or open this link
on 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 it 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
Here’s the hardware that you need to complete this project:
ESP32
– read
Best ESP32 Development Boards
1× Magnetic Reed Switch
1× 10kΩ resistor
1× breadboard
Jumper wires
You can use the preceding links or go directly to
MakerAdvisor.com/tools
to find all the parts for your projects at the best price!
Schematic – ESP32 with Reed Switch
We wired the reed switch to
GPIO 4
, but you can connect it to any suitable GPIO.
Code
Copy the sketch below to your Arduino IDE. Replace the SSID, password, BOT token, and user ID with your credentials.
/*
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*/
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
// Set GPIOs for LED and reedswitch
const int reedSwitch = 4;
const int led = 2; //optional
// Detects whenever the door changed state
bool changeState = false;
// Holds reedswitch state (1=opened, 0=close)
bool state;
String doorState;
// Auxiliary variables (it will only detect changes that are 1500 milliseconds apart)
unsigned long previousMillis = 0;
const long interval = 1500;
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);
// Runs whenever the reedswitch changes state
ICACHE_RAM_ATTR void changeDoorStatus() {
Serial.println("State changed");
changeState = true;
}
void setup() {
// Serial port for debugging purposes
Serial.begin(115200);
// Read the current door state
pinMode(reedSwitch, INPUT_PULLUP);
state = digitalRead(reedSwitch);
// Set LED state to match door state
pinMode(led, OUTPUT);
digitalWrite(led, !state);
// Set the reedswitch pin as interrupt, assign interrupt function and set CHANGE mode
attachInterrupt(digitalPinToInterrupt(reedSwitch), changeDoorStatus, CHANGE);
// Connect to Wi-Fi
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) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
bot.sendMessage(CHAT_ID, "Bot started up", "");
}
void loop() {
if (changeState){
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// If a state has occurred, invert the current door state
state = !state;
if(state) {
doorState = "closed";
}
else{
doorState = "open";
}
digitalWrite(led, !state);
changeState = false;
Serial.printl
We have a similar tutorial that sends emails instead of Telegram messages:
ESP32 Door Status Monitor with Email Notifications (IFTTT)
Read the ESP8266 Guide:
Door Status Monitor with Telegram Notifications
Project Overview
In this project, we’ll create a Telegram Bot that will send messages to your Telegram account whenever a door changes state. To detect the change, we’ll use a magnetic contact switch.
A magnetic contact switch is basically a reed switch encased in a plastic shell so that you can easily apply it to a door, a window, or a drawer to detect if it is open or closed.
The electrical circuit is closed when a magnet is near the switch—door closed. When the magnet is far away from the switch—door open—the circuit is open. See the figure below.
We can connect the reed switch to an ESP32 GPIO to detect changes in its state.
Introducing Telegram
Messenger is a cloud-based instant messaging and voice over IP service. You can easily install it on 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 the door changes state, you’ll receive a notification on 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
on 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. Mine is called
Door Sensor
, and the username is
ESPDoorSensorBot
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.
Sending a Message to the Bot
This step is very important. Don’t miss it. Otherwise, the project will not work.
You must send a message to your Telegram Bot from your Telegram account before it can send you messages.
Go back to the chats tab, and in the search field, type the username of your bot.
Select your bot to start a conversation.
Click on the Start link.
And that’s it! You can proceed to the next section.
Get Your Telegram User ID
To send a message to your Telegram account, the bot needs to know your user ID.
In your Telegram account, search for “
myidbot
” or open this link
on 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 it 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
Here’s the hardware that you need to complete this project:
ESP32
– read
Best ESP32 Development Boards
1× Magnetic Reed Switch
1× 10kΩ resistor
1× breadboard
Jumper wires
You can use the preceding links or go directly to
MakerAdvisor.com/tools
to find all the parts for your projects at the best price!
Schematic – ESP32 with Reed Switch
We wired the reed switch to
GPIO 4
, but you can connect it to any suitable GPIO.
Code
Copy the sketch below to your Arduino IDE. Replace the SSID, password, BOT token, and user ID with your credentials.
/*
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*/
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
// Set GPIOs for LED and reedswitch
const int reedSwitch = 4;
const int led = 2; //optional
// Detects whenever the door changed state
bool changeState = false;
// Holds reedswitch state (1=opened, 0=close)
bool state;
String doorState;
// Auxiliary variables (it will only detect changes that are 1500 milliseconds apart)
unsigned long previousMillis = 0;
const long interval = 1500;
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);
// Runs whenever the reedswitch changes state
ICACHE_RAM_ATTR void changeDoorStatus() {
Serial.println("State changed");
changeState = true;
}
void setup() {
// Serial port for debugging purposes
Serial.begin(115200);
// Read the current door state
pinMode(reedSwitch, INPUT_PULLUP);
state = digitalRead(reedSwitch);
// Set LED state to match door state
pinMode(led, OUTPUT);
digitalWrite(led, !state);
// Set the reedswitch pin as interrupt, assign interrupt function and set CHANGE mode
attachInterrupt(digitalPinToInterrupt(reedSwitch), changeDoorStatus, CHANGE);
// Connect to Wi-Fi
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) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
bot.sendMessage(CHAT_ID, "Bot started up", "");
}
void loop() {
if (changeState){
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// If a state has occurred, invert the current door state
state = !state;
if(state) {
doorState = "closed";
}
else{
doorState = "open";
}
digitalWrite(led, !state);
changeState = false;
Serial.printl
Technologies used
Hardware components
- ESP32
- ESP8266
- Arduino
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.