{"id":97919,"date":"2020-07-17T09:28:00","date_gmt":"2020-07-17T09:28:00","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=97919"},"modified":"2020-09-02T10:36:03","modified_gmt":"2020-09-02T10:36:03","slug":"telegram-esp32-motion-detection-arduino","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/telegram-esp32-motion-detection-arduino\/","title":{"rendered":"Telegram: ESP32 Motion Detection with Notifications (Arduino IDE)"},"content":{"rendered":"\n<p>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&#8217;ll be notified no matter where you are. The ESP board will be programmed using Arduino IDE.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-PIR-Motion-Sensor-Telegram-Send-Message-Notification-Arduino.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 PIR Motion Sensor Telegram Send Message Notification Arduino\" class=\"wp-image-98053\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-PIR-Motion-Sensor-Telegram-Send-Message-Notification-Arduino.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-PIR-Motion-Sensor-Telegram-Send-Message-Notification-Arduino.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-PIR-Motion-Sensor-Telegram-Send-Message-Notification-Arduino.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-PIR-Motion-Sensor-Telegram-Send-Message-Notification-Arduino.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Project Overview<\/h2>\n\n\n\n<p>This tutorial shows how to get notifications in your Telegram account when the ESP32 detects motion. <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"890\" height=\"434\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-PIR-Motion-Sensor-Send-Message-Notificatio-Telegram.png?resize=890%2C434&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 with PIR Motion Sensor Send Message Notification to Telegram using Arduino IDE\" class=\"wp-image-98040\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-PIR-Motion-Sensor-Send-Message-Notificatio-Telegram.png?w=890&amp;quality=100&amp;strip=all&amp;ssl=1 890w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-PIR-Motion-Sensor-Send-Message-Notificatio-Telegram.png?resize=300%2C146&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-PIR-Motion-Sensor-Send-Message-Notificatio-Telegram.png?resize=768%2C375&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 890px) 100vw, 890px\" \/><\/figure><\/div>\n\n\n\n<p>Here&#8217;s an overview on how the project works:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>You&#8217;ll create a Telegram bot for your ESP32.<\/li><li>The ESP32 is connected to a PIR motion sensor.<\/li><li>When the sensor detects motion, the ESP32 sends a warning message to your telegram account.<\/li><li>You&#8217;ll be notified in your telegram account whenever motion is detected.<\/li><\/ul>\n\n\n\n<p>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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introducing Telegram<\/h2>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/telegram.org\/\" target=\"_blank\">Telegram<\/a> 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.<\/p>\n\n\n\n<p>&#8220;<em>Bots are third-party applications that run inside Telegram. Users can interact with bots by sending them messages, commands and&nbsp;inline requests. You control your bots using HTTPS requests to Telegram Bot API<\/em>&#8220;.<\/p>\n\n\n\n<p>The ESP32 will interact with the Telegram bot to send messages to your telegram account. Whenever motion is detected, you&#8217;ll receive a notification in your smartphone (as long as you have access to the internet).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating a Telegram Bot<\/h2>\n\n\n\n<p>Go to Google Play or App Store, download and install <strong>Telegram<\/strong>.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"354\" height=\"269\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Install-Telegram.png?resize=354%2C269&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Install and Download Telegram\" class=\"wp-image-97821\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Install-Telegram.png?w=354&amp;quality=100&amp;strip=all&amp;ssl=1 354w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Install-Telegram.png?resize=300%2C228&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 354px) 100vw, 354px\" \/><\/figure><\/div>\n\n\n\n<p>Open Telegram and follow the next steps to create a Telegram Bot. First, search for &#8220;<strong>botfather<\/strong>&#8221; and click the BotFather as shown below. Or open this link <a href=\"http:\/\/t.me\/botfather\" target=\"_blank\" rel=\"noreferrer noopener\">t.me\/botfather<\/a> in your smartphone.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"362\" height=\"198\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Telegram-Botfather.png?resize=362%2C198&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"botfather\" class=\"wp-image-97822\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Telegram-Botfather.png?w=362&amp;quality=100&amp;strip=all&amp;ssl=1 362w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Telegram-Botfather.png?resize=300%2C164&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 362px) 100vw, 362px\" \/><\/figure><\/div>\n\n\n\n<p>The following window should open and you&#8217;ll be prompted to click the <strong>start<\/strong> button.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"352\" height=\"708\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/start-bot-father-telegram.png?resize=352%2C708&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Telegram Start BotFather to Create a new Bot\" class=\"wp-image-97824\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/start-bot-father-telegram.png?w=352&amp;quality=100&amp;strip=all&amp;ssl=1 352w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/start-bot-father-telegram.png?resize=149%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 149w\" sizes=\"(max-width: 352px) 100vw, 352px\" \/><\/figure><\/div>\n\n\n\n<p>Type <strong>\/newbot<\/strong> and follow the instructions to create your bot. Give it a name and username.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"354\" height=\"396\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Create-bot-Telegram-Botfather_f.png?resize=354%2C396&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Telegram BotFather Create a New Bot\" class=\"wp-image-97826\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Create-bot-Telegram-Botfather_f.png?w=354&amp;quality=100&amp;strip=all&amp;ssl=1 354w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Create-bot-Telegram-Botfather_f.png?resize=268%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 268w\" sizes=\"(max-width: 354px) 100vw, 354px\" \/><\/figure><\/div>\n\n\n\n<p>If your bot is successfully created, you&#8217;ll receive a message with a link to access the bot and the <strong>bot token<\/strong>. Save the bot token because you&#8217;ll need it so that the ESP32 can interact with the bot.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"356\" height=\"537\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Bot-Token-Telegram-Bot-Father.png?resize=356%2C537&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Telegram BotFather Get Bot Token\" class=\"wp-image-97827\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Bot-Token-Telegram-Bot-Father.png?w=356&amp;quality=100&amp;strip=all&amp;ssl=1 356w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Bot-Token-Telegram-Bot-Father.png?resize=199%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 199w\" sizes=\"(max-width: 356px) 100vw, 356px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Get Your Telegram User ID<\/h2>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>In your Telegram account, search for &#8220;IDBot&#8221; or open this link <a href=\"http:\/\/t.me\/myidbot\" target=\"_blank\" rel=\"noreferrer noopener\">t.me\/myidbot<\/a> in your smartphone.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"348\" height=\"173\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Telegram-ID-Bot.png?resize=348%2C173&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Telegram Get Chat ID with IDBot\" class=\"wp-image-97830\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Telegram-ID-Bot.png?w=348&amp;quality=100&amp;strip=all&amp;ssl=1 348w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Telegram-ID-Bot.png?resize=300%2C149&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 348px) 100vw, 348px\" \/><\/figure><\/div>\n\n\n\n<p>Start a conversation with that bot and type <strong>\/getid<\/strong>. You will get a reply back with your user ID. Save that <strong>user ID<\/strong>, because you&#8217;ll need it later in this tutorial.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"354\" height=\"500\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Telegram-Get-Chat-ID.png?resize=354%2C500&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Telegram Get Chat ID with IDBot getid\" class=\"wp-image-97836\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Telegram-Get-Chat-ID.png?w=354&amp;quality=100&amp;strip=all&amp;ssl=1 354w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Telegram-Get-Chat-ID.png?resize=212%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 212w\" sizes=\"(max-width: 354px) 100vw, 354px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Preparing Arduino IDE<\/h2>\n\n\n\n<p>We\u2019ll program the&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\">ESP32<\/a> board using Arduino IDE, so make sure you have them installed in your Arduino IDE.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/installing-the-esp32-board-in-arduino-ide-windows-instructions\/\">Installing the ESP32 Board in Arduino IDE (Windows, Mac OS X, Linux)<\/a><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Universal Telegram Bot Library<\/h3>\n\n\n\n<p>To interact with the Telegram bot, we&#8217;ll use the <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/witnessmenow\/Universal-Arduino-Telegram-Bot\" target=\"_blank\">Universal Telegram Bot Library<\/a> created by Brian Lough that provides an easy interface for the Telegram Bot API.<\/p>\n\n\n\n<p>Follow the next steps to install the latest release of the library.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/witnessmenow\/Universal-Arduino-Telegram-Bot\/archive\/master.zip\" target=\"_blank\">Click here to download the Universal Arduino Telegram Bot library<\/a>.<\/li><li>Go to <strong>Sketch <\/strong>&gt; <strong>Include Library<\/strong> &gt; <strong>Add.ZIP Library..<\/strong>.<\/li><li>Add the library you&#8217;ve just downloaded.<\/li><\/ol>\n\n\n\n<p><strong>Important: <\/strong> don&#8217;t install the library through the Arduino Library Manager because it might install a deprecated version.<\/p>\n\n\n\n<p>For all the details about the library, take a look at the Universal Arduino Telegram Bot Library <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/witnessmenow\/Universal-Arduino-Telegram-Bot\" target=\"_blank\">GitHub<\/a> page.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ArduinoJson Library<\/h3>\n\n\n\n<p>You also have to install the <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/bblanchon\/ArduinoJson\" target=\"_blank\">ArduinoJson<\/a> library. Follow the next steps to install the library.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Go to <strong>Skech <\/strong>&gt; <strong>Include Library<\/strong> &gt; <strong>Manage Libraries<\/strong>.<\/li><li>Search for &#8220;ArduinoJson&#8221;.<\/li><li>Install the library.<\/li><\/ol>\n\n\n\n<p>We&#8217;re using ArduinoJson library version 6.5.12.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"786\" height=\"443\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Install-ArduinoJson-Library.png?resize=786%2C443&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Install in Arduino IDE the ArduinoJSON library\" class=\"wp-image-97837\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Install-ArduinoJson-Library.png?w=786&amp;quality=100&amp;strip=all&amp;ssl=1 786w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Install-ArduinoJson-Library.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Install-ArduinoJson-Library.png?resize=768%2C433&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Parts Required<\/h2>\n\n\n\n<p>For this project, you need the following parts:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\">ESP32 board<\/a>&nbsp;(read&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/esp32-development-boards-review-comparison\/\" target=\"_blank\">Best ESP32 dev boards<\/a>)<\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/mini-hc-sr505-pir-motion-sensor\/\" target=\"_blank\"><\/a><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/mini-hc-sr505-pir-motion-sensor\/\" target=\"_blank\">Mini PIR motion sensor (AM312)<\/a>&nbsp;or&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/pir-motion-sensor-hc-sr501\/\" target=\"_blank\">PIR motion sensor (HC-SR501)<\/a><\/li><li><a rel=\"noreferrer noopener\" style=\"font-size: inherit; background-color: initial;\" href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\">Jumper wires<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\">Breadboard<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Schematic Diagram<\/h2>\n\n\n\n<p>For this project you need to wire a PIR motion sensor to your ESP32 board. Follow the next schematic diagram.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"649\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-PIR-Motion-Sensor-Wiring-Diagram.png?resize=1024%2C649&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 PIR Motion Sensor Wiring Diagram\" class=\"wp-image-98042\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-PIR-Motion-Sensor-Wiring-Diagram.png?resize=1024%2C649&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-PIR-Motion-Sensor-Wiring-Diagram.png?resize=300%2C190&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-PIR-Motion-Sensor-Wiring-Diagram.png?resize=768%2C487&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-PIR-Motion-Sensor-Wiring-Diagram.png?w=1065&amp;quality=100&amp;strip=all&amp;ssl=1 1065w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<p>In this example, we&#8217;re wiring the PIR motion sensor data pin to <span class=\"rnthl rntclblue\">GPIO 27<\/span>. You can use any other suitable GPIO. Read <a href=\"https:\/\/randomnerdtutorials.com\/esp32-pinout-reference-gpios\/\">ESP32 GPIO Guide<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Telegram Motion Detection with Notifications &#8211; ESP32 Sketch<\/h2>\n\n\n\n<p>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.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\r\n  Rui Santos\r\n  Complete project details at https:\/\/RandomNerdTutorials.com\/telegram-esp32-motion-detection-arduino\/\r\n  \r\n  Project created using Brian Lough's Universal Telegram Bot Library: https:\/\/github.com\/witnessmenow\/Universal-Arduino-Telegram-Bot\r\n*\/\r\n\r\n#include &lt;WiFi.h&gt;\r\n#include &lt;WiFiClientSecure.h&gt;\r\n#include &lt;UniversalTelegramBot.h&gt;\r\n#include &lt;ArduinoJson.h&gt;\r\n\r\n\/\/ Replace with your network credentials\r\nconst char* ssid = &quot;REPLACE_WITH_YOUR_SSID&quot;;\r\nconst char* password = &quot;REPLACE_WITH_YOUR_PASSWORD&quot;;\r\n\r\n\/\/ Initialize Telegram BOT\r\n#define BOTtoken &quot;XXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&quot;  \/\/ your Bot Token (Get from Botfather)\r\n\r\n\/\/ Use @myidbot to find out the chat ID of an individual or a group\r\n\/\/ Also note that you need to click &quot;start&quot; on a bot before it can\r\n\/\/ message you\r\n#define CHAT_ID &quot;XXXXXXXXXX&quot;\r\n\r\nWiFiClientSecure client;\r\nUniversalTelegramBot bot(BOTtoken, client);\r\n\r\nconst int motionSensor = 27; \/\/ PIR Motion Sensor\r\nbool motionDetected = false;\r\n\r\n\/\/ Indicates when motion is detected\r\nvoid IRAM_ATTR detectsMovement() {\r\n  \/\/Serial.println(&quot;MOTION DETECTED!!!&quot;);\r\n  motionDetected = true;\r\n}\r\n\r\nvoid setup() {\r\n  Serial.begin(115200);\r\n\r\n  \/\/ PIR Motion Sensor mode INPUT_PULLUP\r\n  pinMode(motionSensor, INPUT_PULLUP);\r\n  \/\/ Set motionSensor pin as interrupt, assign interrupt function and set RISING mode\r\n  attachInterrupt(digitalPinToInterrupt(motionSensor), detectsMovement, RISING);\r\n\r\n  \/\/ Attempt to connect to Wifi network:\r\n  Serial.print(&quot;Connecting Wifi: &quot;);\r\n  Serial.println(ssid);\r\n\r\n  WiFi.mode(WIFI_STA);\r\n  WiFi.begin(ssid, password);\r\n  client.setCACert(TELEGRAM_CERTIFICATE_ROOT); \/\/ Add root certificate for api.telegram.org\r\n  \r\n  while (WiFi.status() != WL_CONNECTED) {\r\n    Serial.print(&quot;.&quot;);\r\n    delay(500);\r\n  }\r\n\r\n  Serial.println(&quot;&quot;);\r\n  Serial.println(&quot;WiFi connected&quot;);\r\n  Serial.print(&quot;IP address: &quot;);\r\n  Serial.println(WiFi.localIP());\r\n\r\n  bot.sendMessage(CHAT_ID, &quot;Bot started up&quot;, &quot;&quot;);\r\n}\r\n\r\nvoid loop() {\r\n  if(motionDetected){\r\n    bot.sendMessage(CHAT_ID, &quot;Motion detected!!&quot;, &quot;&quot;);\r\n    Serial.println(&quot;Motion Detected&quot;);\r\n    motionDetected = false;\r\n  }\r\n}\r\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP\/ESP_Telegram\/ESP32_Telegram_Motion.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How the Code Works<\/h2>\n\n\n\n<p>This sections explain how the code works. Continue reading or skip to the <a href=\"#demonstration\">Demonstration<\/a> section. <\/p>\n\n\n\n<p>Start by importing the required libraries.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;WiFi.h>\n#include &lt;WiFiClientSecure.h>\n#include &lt;UniversalTelegramBot.h>\n#include &lt;ArduinoJson.h><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Network Credentials<\/h3>\n\n\n\n<p>Insert your network credentials in the following variables.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>const char* ssid = \"REPLACE_WITH_YOUR_SSID\";\nconst char* password = \"REPLACE_WITH_YOUR_PASSWORD\";<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Telegram Bot Token<\/h3>\n\n\n\n<p>Insert your Telegram Bot token you&#8217;ve got from Botfather on the <span class=\"rnthl rntliteral\">BOTtoken<\/span> variable.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define BOTtoken \"XXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\"  \/\/ your Bot Token (Get from Botfather)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Telegram User ID<\/h3>\n\n\n\n<p>Insert your chat ID. The one you&#8217;ve got from the IDBot.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define CHAT_ID \"XXXXXXXXXX\"<\/code><\/pre>\n\n\n\n<p>Create a new WiFi client with <span class=\"rnthl rntliteral\">WiFiClientSecure<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>WiFiClientSecure client;<\/code><\/pre>\n\n\n\n<p>Create a <span class=\"rnthl rntliteral\">bot<\/span> with the token and client defined earlier.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>UniversalTelegramBot bot(BOTtoken, client);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Motion Sensor<\/h3>\n\n\n\n<p>Define the GPIO that the motion sensor is connected to.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>const int motionSensor = 27; \/\/ PIR Motion Sensor<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">motionDetected<\/span> boolean variable is used to indicate whether motion was detected or not. It is set to <span class=\"rnthl rntliteral\">false<\/span> by default.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>bool motionDetected = false;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">detectsMovement()<\/h3>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">detectsmovement()<\/span> function is a callback function that will be executed when motion is detected. In this case, it simply changes the state of the <span class=\"rnthl rntliteral\">motionDetected<\/span> variable to <span class=\"rnthl rntliteral\">true<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void IRAM_ATTR detectsMovement() {\n  \/\/Serial.println(\"MOTION DETECTED!!!\");\n  motionDetected = true;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">setup()<\/h3>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, initialize the Serial Monitor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.begin(115200);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">PIR Motion Sensor Interrupt<\/h4>\n\n\n\n<p>Set the PIR motion sensor as an interrupt and set the <span class=\"rnthl rntliteral\">detectsMovement()<\/span> as the callback function (when motion is detected, that function will be executed):<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ PIR Motion Sensor mode INPUT_PULLUP\npinMode(motionSensor, INPUT_PULLUP);\n\/\/ Set motionSensor pin as interrupt, assign interrupt function and set RISING mode\nattachInterrupt(digitalPinToInterrupt(motionSensor), detectsMovement, RISING);<\/code><\/pre>\n\n\n\n<p><strong>Note: <\/strong> Recommended reading: <a href=\"https:\/\/randomnerdtutorials.com\/esp32-pir-motion-sensor-interrupts-timers\/\">ESP32 with PIR Motion Sensor using Interrupts and Timers<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Init Wi-Fi<\/h4>\n\n\n\n<p>Initialize Wi-Fi and connect the ESP32 to your local network with the SSID and password defined earlier.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>WiFi.mode(WIFI_STA);\nWiFi.begin(ssid, password);\nwhile (WiFi.status() != WL_CONNECTED) {\n  delay(1000);\n  Serial.println(\"Connecting to WiFi..\");\n}<\/code><\/pre>\n\n\n\n<p>Finally, send a message to indicate that the Bot has started up:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>bot.sendMessage(CHAT_ID, \"Bot started up\", \"\");<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">loop()<\/h3>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span>, check the state of the <span class=\"rnthl rntliteral\">motionDetected<\/span> variable. <\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void loop() {\n  if(motionDetected){<\/code><\/pre>\n\n\n\n<p>If it&#8217;s <span class=\"rnthl rntliteral\">true<\/span>, it means that motion was detected. So, send a message to your Telegram account indicating that motion was detected. <\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>bot.sendMessage(CHAT_ID, \"Motion detected!!\", \"\");<\/code><\/pre>\n\n\n\n<p>Sending a message to the bot is very simply. You just need to use the <span class=\"rnthl rntliteral\">sendMessage()<\/span> method on the <span class=\"rnthl rntliteral\">bot<\/span> object and pass as arguments the recipient\u2019s chat ID, the message, and the parse mode.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>bool sendMessage(String chat_id, String text, String parse_mode = \"\")<\/code><\/pre>\n\n\n\n<p>Finally, after sending the message, set the <span class=\"rnthl rntliteral\">motionDetected<\/span> variable to <span class=\"rnthl rntliteral\">false<\/span>, so it can detect motion again. <\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>motionDetected = false;<\/code><\/pre>\n\n\n\n<p>That&#8217;s pretty much how the code works.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"demonstration\">Demonstration<\/h2>\n\n\n\n<p class=\"rntbox rntcred\"><strong>Important: <\/strong> go to your Telegram account and search for your bot. You need to click &#8220;<strong>start<\/strong>&#8221; on a bot before it can message you.<\/p>\n\n\n\n<p>Upload the code to your ESP32 board. Don&#8217;t forget to go to <strong>Tools <\/strong>&gt; <strong>Board <\/strong>and select the board you&#8217;re using. Go to <strong>Tools <\/strong>&gt; <strong>Port <\/strong>and select the COM port your board is connected to.<\/p>\n\n\n\n<p>After uploading the code, press the ESP32 on-board EN\/RST button so that it starts running the code. Then, you can open the Serial Monitor to check what&#8217;s happening in the background.<\/p>\n\n\n\n<p>When your board first boots, it will send a message to your Telegram account: &#8220;Bot started up&#8221;. Then, move your hand in front of the PIR motion sensor and check that you&#8217;ve received the motion detected notification.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"352\" height=\"279\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Motion-Detected-Telegram-Notification.png?resize=352%2C279&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 Motion Detected Telegram Notification\" class=\"wp-image-97963\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Motion-Detected-Telegram-Notification.png?w=352&amp;quality=100&amp;strip=all&amp;ssl=1 352w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Motion-Detected-Telegram-Notification.png?resize=300%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 352px) 100vw, 352px\" \/><\/figure><\/div>\n\n\n\n<p>At the same time, this is what you should get on the Serial Monitor.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"669\" height=\"435\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/ESP32-Telegram-Motion-Detected-Serial-Monitor-Demonstration.png?resize=669%2C435&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 Telegram Motion Detected Serial Monitor Demonstration\" class=\"wp-image-97964\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/ESP32-Telegram-Motion-Detected-Serial-Monitor-Demonstration.png?w=669&amp;quality=100&amp;strip=all&amp;ssl=1 669w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/ESP32-Telegram-Motion-Detected-Serial-Monitor-Demonstration.png?resize=300%2C195&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 669px) 100vw, 669px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this tutorial you&#8217;ve learned how to create a Telegram Bot to interact with the ESP32 board. When motion is detected, a message is sent.<\/p>\n\n\n\n<p>With this bot, you can also use your Telegram account to send messages to the ESP32 to <a href=\"https:\/\/randomnerdtutorials.com\/telegram-control-esp32-esp8266-nodemcu-outputs\/\">control its outputs<\/a> or <a href=\"https:\/\/randomnerdtutorials.com\/telegram-request-esp32-esp8266-nodemcu-sensor-readings\/\">request sensor readings<\/a>, for example.<\/p>\n\n\n\n<p>The great thing about using Telegram to control your ESP boards, is that as long as you have an internet connection (and your boards too), you can control and monitor them from anywhere in the world.<\/p>\n\n\n\n<p>More projects with Telegram:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/telegram-control-esp32-esp8266-nodemcu-outputs\/\">Control Outputs<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/telegram-request-esp32-esp8266-nodemcu-sensor-readings\/\">Request Sensor Readings<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/telegram-esp8266-nodemcu-motion-detection-arduino\/\">ESP8266 Motion Detection<\/a><\/li><\/ul>\n\n\n\n<p>We hope you&#8217;ve found this project interesting. Learn more about the ESP32 with our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/learn-esp32-with-arduino-ide\/\">Learn ESP32 with Arduino IDE (eBook + Video Course)<\/a><\/li><li><a style=\"font-size: inherit; background-color: initial;\" href=\"https:\/\/randomnerdtutorials.com\/projects-esp32\/\">More ESP32 Projects and Tutorials&#8230;<\/a><\/li><\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;ll be &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Telegram: ESP32 Motion Detection with Notifications (Arduino IDE)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/telegram-esp32-motion-detection-arduino\/#more-97919\" aria-label=\"Read more about Telegram: ESP32 Motion Detection with Notifications (Arduino IDE)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":1,"featured_media":98053,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[276,281,277,299,264],"tags":[],"class_list":["post-97919","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32","category-esp32-project","category-esp32-arduino-ide","category-0-esp32","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-PIR-Motion-Sensor-Telegram-Send-Message-Notification-Arduino.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/97919","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=97919"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/97919\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/98053"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=97919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=97919"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=97919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}