{"id":98951,"date":"2023-09-19T09:58:45","date_gmt":"2023-09-19T09:58:45","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=98951"},"modified":"2023-09-19T10:35:21","modified_gmt":"2023-09-19T10:35:21","slug":"telegram-esp32-cam-photo-arduino","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/telegram-esp32-cam-photo-arduino\/","title":{"rendered":"Telegram: ESP32-CAM Take and Send Photo (Arduino IDE)"},"content":{"rendered":"\n<p>In this tutorial, you\u2019ll create a Telegram bot to interact with the ESP32-CAM to request a new photo. You can request a new photo using your Telegram account from anywhere. You just need to have access to the internet on your smartphone.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/08\/ESP32-CAM-Telegram-Picture.jpg?resize=1024%2C576&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Telegram: ESP32-CAM Take and Send Photo (Arduino IDE)\" class=\"wp-image-136580\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/08\/ESP32-CAM-Telegram-Picture.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/08\/ESP32-CAM-Telegram-Picture.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/08\/ESP32-CAM-Telegram-Picture.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/08\/ESP32-CAM-Telegram-Picture.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p><strong>Note: <\/strong> this project is compatible with any ESP32 Camera Board with the OV2640 camera. You just need to make sure you use the right <a href=\"https:\/\/randomnerdtutorials.com\/esp32-cam-ai-thinker-pinout\/\">pinout for the board<\/a> you\u2019re using.<\/p>\n\n\n\n<p class=\"rntbox rntclgray\"><em>Updated 19 September 2023.<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Project Overview<\/h2>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/08\/ESP32-CAM-Take-Send-Photo-Telegram-Project-Overview.jpg?resize=820%2C615&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32-CAM Take and Send Photo to Telegram Project Overview\" class=\"wp-image-98957\" width=\"820\" height=\"615\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/08\/ESP32-CAM-Take-Send-Photo-Telegram-Project-Overview.jpg?w=867&amp;quality=100&amp;strip=all&amp;ssl=1 867w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/08\/ESP32-CAM-Take-Send-Photo-Telegram-Project-Overview.jpg?resize=300%2C225&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/08\/ESP32-CAM-Take-Send-Photo-Telegram-Project-Overview.jpg?resize=768%2C577&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 820px) 100vw, 820px\" \/><\/figure><\/div>\n\n\n<p>Here\u2019s an overview of the project you\u2019ll build:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You&#8217;ll create a Telegram bot for your ESP32-CAM;<\/li>\n\n\n\n<li>You can start a conversation with the ESP32-CAM bot;<\/li>\n\n\n\n<li>When you send the message <strong>\/photo<\/strong> to the ESP32-CAM bot, the ESP32-CAM board receives the message, takes a new photo, and responds with that photo;<\/li>\n\n\n\n<li>You can send the message <strong>\/flash<\/strong> to toggle the ESP32-CAM\u2019s LED flash;<\/li>\n\n\n\n<li>You can send the <strong>\/start<\/strong> message to receive a welcome message with the commands to control the board;<\/li>\n\n\n\n<li>The ESP32-CAM will only respond to messages coming from your Telegram account ID.<\/li>\n<\/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<p>We also have a dedicated project for the ESP32-CAM with Telegram that covers more advanced features: <a href=\"https:\/\/randomnerdtutorials.com\/esp32-cam-shield-pcb-telegram\/\">Take Photos, Control Outputs, Request Sensor Readings and Motion Notifications<\/a>.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Introducing Telegram<\/h1>\n\n\n\n<p><a href=\"https:\/\/telegram.org\/\">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\u2019s free and without any ads. Telegram allows you to create bots that you can interact with.<\/p>\n\n\n\n<p>&#8220;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&#8221;.<\/p>\n\n\n\n<p>The ESP32-CAM will interact with the Telegram bot to receive and handle the messages, and send responses. In this tutorial, you&#8217;ll learn how to use Telegram to send messages to your bot to request a new photo taken with the ESP32-CAM. You can receive the photo wherever you are (you just need Telegram and 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<div class=\"wp-block-image\">\n<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<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<div class=\"wp-block-image\">\n<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<p>The following window should open and you&#8217;ll be prompted to click the <strong>start<\/strong> button.<\/p>\n\n\n<div class=\"wp-block-image\">\n<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<p>Type <strong>\/newbot<\/strong> and follow the instructions to create your bot. Give it a name and username.<\/p>\n\n\n<div class=\"wp-block-image\">\n<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<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<div class=\"wp-block-image\">\n<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<p class=\"rntbox rntclblue\"><strong>Note: <\/strong> the bot token is a very long string. To make sure you get it right, you can go to the <a href=\"https:\/\/web.telegram.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">Telegram Web Interface<\/a> and copy your bot token from there.<\/p>\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;<strong>IDBot<\/strong>&#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<div class=\"wp-block-image\">\n<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<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<div class=\"wp-block-image\">\n<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<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\">\n<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>\n<\/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 href=\"https:\/\/github.com\/witnessmenow\/Universal-Arduino-Telegram-Bot\" target=\"_blank\" rel=\"noreferrer noopener\">Universal Telegram Bot Library<\/a> created by Brian Lough which 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\">\n<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>\n\n\n\n<li>Go to <strong>Sketch <\/strong>&gt; <strong>Include Library<\/strong> &gt; <strong>Add.ZIP Library..<\/strong>.<\/li>\n\n\n\n<li>Add the library you&#8217;ve just downloaded.<\/li>\n<\/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\">\n<li>Go to <strong>Sketch <\/strong>&gt; <strong>Include Library<\/strong> &gt; <strong>Manage Libraries<\/strong>.<\/li>\n\n\n\n<li>Search for &#8220;ArduinoJson&#8221;.<\/li>\n\n\n\n<li>Install the library.<\/li>\n<\/ol>\n\n\n\n<p>We&#8217;re using ArduinoJson library version 6.15.2.<\/p>\n\n\n<div class=\"wp-block-image\">\n<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<h2 class=\"wp-block-heading\">Code<\/h2>\n\n\n\n<p>Copy the following code the Arduino IDE. 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. Additionally, check the pin assignment for the camera board that you\u2019re using.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\n  Rui Santos\n  Complete project details at https:\/\/RandomNerdTutorials.com\/telegram-esp32-cam-photo-arduino\/\n  \n  Permission is hereby granted, free of charge, to any person obtaining a copy\n  of this software and associated documentation files.\n  \n  The above copyright notice and this permission notice shall be included in all\n  copies or substantial portions of the Software.\n*\/\n\n#include &lt;Arduino.h&gt;\n#include &lt;WiFi.h&gt;\n#include &lt;WiFiClientSecure.h&gt;\n#include &quot;soc\/soc.h&quot;\n#include &quot;soc\/rtc_cntl_reg.h&quot;\n#include &quot;esp_camera.h&quot;\n#include &lt;UniversalTelegramBot.h&gt;\n#include &lt;ArduinoJson.h&gt;\n\nconst char* ssid = &quot;REPLACE_WITH_YOUR_SSID&quot;;\nconst char* password = &quot;REPLACE_WITH_YOUR_PASSWORD&quot;;\n\n\/\/ Initialize Telegram BOT\nString BOTtoken = &quot;XXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&quot;;  \/\/ your Bot Token (Get from Botfather)\n\n\/\/ Use @myidbot to find out the chat ID of an individual or a group\n\/\/ Also note that you need to click &quot;start&quot; on a bot before it can\n\/\/ message you\nString CHAT_ID = &quot;XXXXXXXXXX&quot;;\n\nbool sendPhoto = false;\n\nWiFiClientSecure clientTCP;\nUniversalTelegramBot bot(BOTtoken, clientTCP);\n\n#define FLASH_LED_PIN 4\nbool flashState = LOW;\n\n\/\/Checks for new messages every 1 second.\nint botRequestDelay = 1000;\nunsigned long lastTimeBotRan;\n\n\/\/CAMERA_MODEL_AI_THINKER\n#define PWDN_GPIO_NUM     32\n#define RESET_GPIO_NUM    -1\n#define XCLK_GPIO_NUM      0\n#define SIOD_GPIO_NUM     26\n#define SIOC_GPIO_NUM     27\n\n#define Y9_GPIO_NUM       35\n#define Y8_GPIO_NUM       34\n#define Y7_GPIO_NUM       39\n#define Y6_GPIO_NUM       36\n#define Y5_GPIO_NUM       21\n#define Y4_GPIO_NUM       19\n#define Y3_GPIO_NUM       18\n#define Y2_GPIO_NUM        5\n#define VSYNC_GPIO_NUM    25\n#define HREF_GPIO_NUM     23\n#define PCLK_GPIO_NUM     22\n\n\nvoid configInitCamera(){\n  camera_config_t config;\n  config.ledc_channel = LEDC_CHANNEL_0;\n  config.ledc_timer = LEDC_TIMER_0;\n  config.pin_d0 = Y2_GPIO_NUM;\n  config.pin_d1 = Y3_GPIO_NUM;\n  config.pin_d2 = Y4_GPIO_NUM;\n  config.pin_d3 = Y5_GPIO_NUM;\n  config.pin_d4 = Y6_GPIO_NUM;\n  config.pin_d5 = Y7_GPIO_NUM;\n  config.pin_d6 = Y8_GPIO_NUM;\n  config.pin_d7 = Y9_GPIO_NUM;\n  config.pin_xclk = XCLK_GPIO_NUM;\n  config.pin_pclk = PCLK_GPIO_NUM;\n  config.pin_vsync = VSYNC_GPIO_NUM;\n  config.pin_href = HREF_GPIO_NUM;\n  config.pin_sccb_sda = SIOD_GPIO_NUM;\n  config.pin_sccb_scl = SIOC_GPIO_NUM;\n  config.pin_pwdn = PWDN_GPIO_NUM;\n  config.pin_reset = RESET_GPIO_NUM;\n  config.xclk_freq_hz = 20000000;\n  config.pixel_format = PIXFORMAT_JPEG;\n  config.grab_mode = CAMERA_GRAB_LATEST;\n\n  \/\/init with high specs to pre-allocate larger buffers\n  if(psramFound()){\n    config.frame_size = FRAMESIZE_UXGA;\n    config.jpeg_quality = 10;  \/\/0-63 lower number means higher quality\n    config.fb_count = 1;\n  } else {\n    config.frame_size = FRAMESIZE_SVGA;\n    config.jpeg_quality = 12;  \/\/0-63 lower number means higher quality\n    config.fb_count = 1;\n  }\n  \n  \/\/ camera init\n  esp_err_t err = esp_camera_init(&amp;config);\n  if (err != ESP_OK) {\n    Serial.printf(&quot;Camera init failed with error 0x%x&quot;, err);\n    delay(1000);\n    ESP.restart();\n  }\n}\n\nvoid handleNewMessages(int numNewMessages) {\n  Serial.print(&quot;Handle New Messages: &quot;);\n  Serial.println(numNewMessages);\n\n  for (int i = 0; i &lt; numNewMessages; i++) {\n    String chat_id = String(bot.messages[i].chat_id);\n    if (chat_id != CHAT_ID){\n      bot.sendMessage(chat_id, &quot;Unauthorized user&quot;, &quot;&quot;);\n      continue;\n    }\n    \n    \/\/ Print the received message\n    String text = bot.messages[i].text;\n    Serial.println(text);\n    \n    String from_name = bot.messages[i].from_name;\n    if (text == &quot;\/start&quot;) {\n      String welcome = &quot;Welcome , &quot; + from_name + &quot;\\n&quot;;\n      welcome += &quot;Use the following commands to interact with the ESP32-CAM \\n&quot;;\n      welcome += &quot;\/photo : takes a new photo\\n&quot;;\n      welcome += &quot;\/flash : toggles flash LED \\n&quot;;\n      bot.sendMessage(CHAT_ID, welcome, &quot;&quot;);\n    }\n    if (text == &quot;\/flash&quot;) {\n      flashState = !flashState;\n      digitalWrite(FLASH_LED_PIN, flashState);\n      Serial.println(&quot;Change flash LED state&quot;);\n    }\n    if (text == &quot;\/photo&quot;) {\n      sendPhoto = true;\n      Serial.println(&quot;New photo request&quot;);\n    }\n  }\n}\n\nString sendPhotoTelegram() {\n  const char* myDomain = &quot;api.telegram.org&quot;;\n  String getAll = &quot;&quot;;\n  String getBody = &quot;&quot;;\n\n  \/\/Dispose first picture because of bad quality\n  camera_fb_t * fb = NULL;\n  fb = esp_camera_fb_get();\n  esp_camera_fb_return(fb); \/\/ dispose the buffered image\n  \n  \/\/ Take a new photo\n  fb = NULL;  \n  fb = esp_camera_fb_get();  \n  if(!fb) {\n    Serial.println(&quot;Camera capture failed&quot;);\n    delay(1000);\n    ESP.restart();\n    return &quot;Camera capture failed&quot;;\n  }  \n  \n  Serial.println(&quot;Connect to &quot; + String(myDomain));\n\n\n  if (clientTCP.connect(myDomain, 443)) {\n    Serial.println(&quot;Connection successful&quot;);\n    \n    String head = &quot;--RandomNerdTutorials\\r\\nContent-Disposition: form-data; name=\\&quot;chat_id\\&quot;; \\r\\n\\r\\n&quot; + CHAT_ID + &quot;\\r\\n--RandomNerdTutorials\\r\\nContent-Disposition: form-data; name=\\&quot;photo\\&quot;; filename=\\&quot;esp32-cam.jpg\\&quot;\\r\\nContent-Type: image\/jpeg\\r\\n\\r\\n&quot;;\n    String tail = &quot;\\r\\n--RandomNerdTutorials--\\r\\n&quot;;\n\n    size_t imageLen = fb-&gt;len;\n    size_t extraLen = head.length() + tail.length();\n    size_t totalLen = imageLen + extraLen;\n  \n    clientTCP.println(&quot;POST \/bot&quot;+BOTtoken+&quot;\/sendPhoto HTTP\/1.1&quot;);\n    clientTCP.println(&quot;Host: &quot; + String(myDomain));\n    clientTCP.println(&quot;Content-Length: &quot; + String(totalLen));\n    clientTCP.println(&quot;Content-Type: multipart\/form-data; boundary=RandomNerdTutorials&quot;);\n    clientTCP.println();\n    clientTCP.print(head);\n  \n    uint8_t *fbBuf = fb-&gt;buf;\n    size_t fbLen = fb-&gt;len;\n    for (size_t n=0;n&lt;fbLen;n=n+1024) {\n      if (n+1024&lt;fbLen) {\n        clientTCP.write(fbBuf, 1024);\n        fbBuf += 1024;\n      }\n      else if (fbLen%1024&gt;0) {\n        size_t remainder = fbLen%1024;\n        clientTCP.write(fbBuf, remainder);\n      }\n    }  \n    \n    clientTCP.print(tail);\n    \n    esp_camera_fb_return(fb);\n    \n    int waitTime = 10000;   \/\/ timeout 10 seconds\n    long startTimer = millis();\n    boolean state = false;\n    \n    while ((startTimer + waitTime) &gt; millis()){\n      Serial.print(&quot;.&quot;);\n      delay(100);      \n      while (clientTCP.available()) {\n        char c = clientTCP.read();\n        if (state==true) getBody += String(c);        \n        if (c == '\\n') {\n          if (getAll.length()==0) state=true; \n          getAll = &quot;&quot;;\n        } \n        else if (c != '\\r')\n          getAll += String(c);\n        startTimer = millis();\n      }\n      if (getBody.length()&gt;0) break;\n    }\n    clientTCP.stop();\n    Serial.println(getBody);\n  }\n  else {\n    getBody=&quot;Connected to api.telegram.org failed.&quot;;\n    Serial.println(&quot;Connected to api.telegram.org failed.&quot;);\n  }\n  return getBody;\n}\n\nvoid setup(){\n  WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); \n  \/\/ Init Serial Monitor\n  Serial.begin(115200);\n\n  \/\/ Set LED Flash as output\n  pinMode(FLASH_LED_PIN, OUTPUT);\n  digitalWrite(FLASH_LED_PIN, flashState);\n\n  \/\/ Config and init the camera\n  configInitCamera();\n\n  \/\/ Connect to Wi-Fi\n  WiFi.mode(WIFI_STA);\n  Serial.println();\n  Serial.print(&quot;Connecting to &quot;);\n  Serial.println(ssid);\n  WiFi.begin(ssid, password);\n  clientTCP.setCACert(TELEGRAM_CERTIFICATE_ROOT); \/\/ Add root certificate for api.telegram.org\n  while (WiFi.status() != WL_CONNECTED) {\n    Serial.print(&quot;.&quot;);\n    delay(500);\n  }\n  Serial.println();\n  Serial.print(&quot;ESP32-CAM IP Address: &quot;);\n  Serial.println(WiFi.localIP()); \n}\n\nvoid loop() {\n  if (sendPhoto) {\n    Serial.println(&quot;Preparing photo&quot;);\n    sendPhotoTelegram(); \n    sendPhoto = false; \n  }\n  if (millis() &gt; lastTimeBotRan + botRequestDelay)  {\n    int numNewMessages = bot.getUpdates(bot.last_message_received + 1);\n    while (numNewMessages) {\n      Serial.println(&quot;got response&quot;);\n      handleNewMessages(numNewMessages);\n      numNewMessages = bot.getUpdates(bot.last_message_received + 1);\n    }\n    lastTimeBotRan = millis();\n  }\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/ESP32-CAM-Arduino-IDE\/raw\/master\/ESP32-CAM-Take-Send-Photo-Telegram\/ESP32-CAM-Take-Send-Photo-Telegram.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 section explains how the code works. Continue reading to learn how the code works or skip to the Demonstration section.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Importing Libraries<\/h3>\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;Arduino.h&gt;\n#include &lt;WiFi.h&gt;\n#include &lt;WiFiClientSecure.h&gt;\n#include \"soc\/soc.h\"\n#include \"soc\/rtc_cntl_reg.h\"\n#include \"esp_camera.h\"\n#include &lt;UniversalTelegramBot.h&gt;\n#include &lt;ArduinoJson.h&gt;<\/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 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>String CHAT_ID = \"XXXXXXXXXX\";<\/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>String BOTtoken = \"XXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\";<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">sendPhoto<\/span> boolean variable indicates whether it is time to send a new photo to your telegram account. By default, it is set to <span class=\"rnthl rntliteral\">false<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>bool sendPhoto = false;<\/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 clientTCP;<\/code><\/pre>\n\n\n\n<p>Create a bot with the token and client defined earlier.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>UniversalTelegramBot bot(BOTtoken, clientTCP);<\/code><\/pre>\n\n\n\n<p>Create a variable to hold the flash LED pin (<span class=\"rnthl rntliteral\">FLASH_LED_PIN<\/span>). In the ESP32-CAM AI Thinker, the flash is connected to <span class=\"rnthl rntclblue\">GPIO 4<\/span>. By default, set it to <span class=\"rnthl rntliteral\">LOW<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>define FLASH_LED_PIN 4\nbool flashState = LOW;<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">botRequestDelay<\/span> and <span class=\"rnthl rntliteral\">lasTimeBotRan<\/span> variables are used to check for new Telegram messages every x number of seconds. In this case, the code will check for new messages every second (1000 milliseconds). You can change that delay time in the <span class=\"rnthl rntliteral\">botRequestDelay<\/span> variable.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>int botRequestDelay = 1000;\nunsigned long lastTimeBotRan;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">ESP32-CAM Initialization<\/h3>\n\n\n\n<p>The following lines assign the ESP32-CAM AI-Thinker pins. If you\u2019re using a different ESP32 camera model, don\u2019t forget to change the pinout (read <a href=\"https:\/\/randomnerdtutorials.com\/esp32-cam-camera-pin-gpios\/\">ESP32-CAM Camera Boards: Pin and GPIOs Assignment Guide<\/a>).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/CAMERA_MODEL_AI_THINKER\n#define PWDN_GPIO_NUM     32\n#define RESET_GPIO_NUM    -1\n#define XCLK_GPIO_NUM      0\n#define SIOD_GPIO_NUM     26\n#define SIOC_GPIO_NUM     27\n#define Y9_GPIO_NUM       35\n#define Y8_GPIO_NUM       34\n#define Y7_GPIO_NUM       39\n#define Y6_GPIO_NUM       36\n#define Y5_GPIO_NUM       21\n#define Y4_GPIO_NUM       19\n#define Y3_GPIO_NUM       18\n#define Y2_GPIO_NUM        5\n#define VSYNC_GPIO_NUM    25\n#define HREF_GPIO_NUM     23\n#define PCLK_GPIO_NUM     22<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">configInitCamera()<\/span> function initializes the ESP32 camera.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void configInitCamera(){\n  camera_config_t config;\n  config.ledc_channel = LEDC_CHANNEL_0;\n  config.ledc_timer = LEDC_TIMER_0;\n  config.pin_d0 = Y2_GPIO_NUM;\n  config.pin_d1 = Y3_GPIO_NUM;\n  config.pin_d2 = Y4_GPIO_NUM;\n  config.pin_d3 = Y5_GPIO_NUM;\n  config.pin_d4 = Y6_GPIO_NUM;\n  config.pin_d5 = Y7_GPIO_NUM;\n  config.pin_d6 = Y8_GPIO_NUM;\n  config.pin_d7 = Y9_GPIO_NUM;\n  config.pin_xclk = XCLK_GPIO_NUM;\n  config.pin_pclk = PCLK_GPIO_NUM;\n  config.pin_vsync = VSYNC_GPIO_NUM;\n  config.pin_href = HREF_GPIO_NUM;\n  config.pin_sscb_sda = SIOD_GPIO_NUM;\n  config.pin_sscb_scl = SIOC_GPIO_NUM;\n  config.pin_pwdn = PWDN_GPIO_NUM;\n  config.pin_reset = RESET_GPIO_NUM;\n  config.xclk_freq_hz = 20000000;\n  config.pixel_format = PIXFORMAT_JPEG;\n  config.grab_mode = CAMERA_GRAB_LATEST;\n\n  \/\/init with high specs to pre-allocate larger buffers\n  if(psramFound()){\n    config.frame_size = FRAMESIZE_UXGA;\n    config.jpeg_quality = 10;  \/\/0-63 lower number means higher quality\n    config.fb_count = 1;\n  } else {\n    config.frame_size = FRAMESIZE_SVGA;\n    config.jpeg_quality = 12;  \/\/0-63 lower number means higher quality\n    config.fb_count = 1;\n  }\n  \n  \/\/ camera init\n  esp_err_t err = esp_camera_init(&amp;config);\n  if (err != ESP_OK) {\n    Serial.printf(\"Camera init failed with error 0x%x\", err);\n    delay(1000);\n    ESP.restart();\n  }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">handleNewMessages()<\/h3>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">handleNewMessages()<\/span> function handles what happens when new messages arrive.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void handleNewMessages(int numNewMessages) {\n  Serial.print(\"Handle New Messages: \");\n  Serial.println(numNewMessages);<\/code><\/pre>\n\n\n\n<p>It checks the available messages:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>for (int i = 0; i &lt; numNewMessages; i++) {<\/code><\/pre>\n\n\n\n<p>Get the chat ID for a particular message and store it in the <span class=\"rnthl rntliteral\">chat_id<\/span> variable. The chat ID identifies who sent the message.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>String chat_id = String(bot.messages&#091;i].chat_id);<\/code><\/pre>\n\n\n\n<p>If the <span class=\"rnthl rntliteral\">chat_id<\/span> is different from your chat ID (<span class=\"rnthl rntliteral\">CHAT_ID<\/span>), it means that someone (that is not you) has sent a message to your bot. If that&#8217;s the case, ignore the message and wait for the next message.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (chat_id != CHAT_ID){\n  bot.sendMessage(chat_id, \"Unauthorized user\", \"\");\n  continue;\n}<\/code><\/pre>\n\n\n\n<p>Otherwise, it means that the message was sent from a valid user, so we&#8217;ll save it in the <span class=\"rnthl rntliteral\">text<\/span> variable and check its content.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>String text = bot.messages&#091;i].text;\nSerial.println(text);<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">from_name<\/span> variable saves the name of the sender.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>String from_name = bot.messages&#091;i].from_name;<\/code><\/pre>\n\n\n\n<p>If it receives the <strong>\/start<\/strong> message, we&#8217;ll send the valid commands to control the ESP. This is useful if you happen to forget what are the commands to control your board.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (text == \"\/start\") {\n  String welcome = \"Welcome , \" + from_name + \"\\n\";\n  welcome += \"Use the following commands to interact with the ESP32-CAM \\n\";\n  welcome += \"\/photo : takes a new photo\\n\";\n  welcome += \"\/flash : toggles flash LED \\n\";\n  bot.sendMessage(CHAT_ID, welcome, \"\");\n}<\/code><\/pre>\n\n\n\n<p>Sending a message to the bot is very simple. You just need to use the <span class=\"rnthl rntliteral\">sendMessage()<\/span> method on the bot object and pass as arguments the recipient&#8217;s 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>In our example, we&#8217;ll send the message to the ID stored on the <span class=\"rnthl rntliteral\">CHAT_ID<\/span> variable (that corresponds to your personal chat id) and send the message saved on the welcome variable.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>bot.sendMessage(CHAT_ID, welcome, \"\");<\/code><\/pre>\n\n\n\n<p>If it receives the <strong>\/flash<\/strong> message, invert the <span class=\"rnthl rntliteral\">flashState<\/span> variable and update the flash led state. If it was previously <span class=\"rnthl rntliteral\">LOW<\/span>, set it to <span class=\"rnthl rntliteral\">HIGH<\/span>. If it was previously <span class=\"rnthl rntliteral\">HIGH<\/span>, set it to <span class=\"rnthl rntliteral\">LOW<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (text == \"\/flash\") {\n  flashState = !flashState;\n  digitalWrite(FLASH_LED_PIN, flashState);\n  Serial.println(\"Change flash LED state\");\n}<\/code><\/pre>\n\n\n\n<p>Finally, if it receives the <strong>\/photo<\/strong> message, set the <span class=\"rnthl rntliteral\">sendPhoto<\/span> variable to true. Then, in the <span class=\"rnthl rntliteral\">loop()<\/span>, check the value of the <span class=\"rnthl rntliteral\">sendPhoto<\/span> variable and proceed accordingly.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (text == \"\/photo\") {\n  sendPhoto = true;\n  Serial.println(\"New photo request\");\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">sendPhotoTelegram()<\/h3>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">sendPhotoTelegram()<\/span> function takes a photo with the ESP32-CAM.<\/p>\n\n\n\n<p class=\"rntbox rntclgray\"><strong>Note<\/strong>: many times, the first picture taken with the ESP32-CAM is not good because the sensor has not adjusted the white balance yet. So, to make sure we get a good picture, we discard the first one.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/Dispose first picture because of bad quality\n  camera_fb_t * fb = NULL;\n  fb = esp_camera_fb_get();\n  esp_camera_fb_return(fb); \/\/ dispose the buffered image\n  \n  \/\/ Take a new photo\n  fb = NULL;  \n  fb = esp_camera_fb_get();  \n  if(!fb) {\n    Serial.println(\"Camera capture failed\");\n    delay(1000);\n    ESP.restart();\n    return \"Camera capture failed\";\n  }  <\/code><\/pre>\n\n\n\n<p>Then, it makes an HTTP POST request to send the photo to your telegram bot.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>clientTCP.println(\"POST \/bot\"+BOTtoken+\"\/sendPhoto HTTP\/1.1\");\nclientTCP.println(\"Host: \" + String(myDomain));\nclientTCP.println(\"Content-Length: \" + String(totalLen));\nclientTCP.println(\"Content-Type: multipart\/form-data; boundary=RandomNerdTutorials\");\nclientTCP.println();\nclientTCP.print(head);<\/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<p>Set the flash LED as an output and set it to its initial state.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>pinMode(FLASH_LED_PIN, OUTPUT);\ndigitalWrite(FLASH_LED_PIN, flashState);<\/code><\/pre>\n\n\n\n<p>Call the <span class=\"rnthl rntliteral\">configInitCamera()<\/span> function to configure and initialize the camera.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>configInitCamera();<\/code><\/pre>\n\n\n\n<p>Connect your ESP32-CAM to your local network.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>WiFi.mode(WIFI_STA);\nSerial.println();\nSerial.print(\"Connecting to \");\nSerial.println(ssid);\nWiFi.begin(ssid, password);\nwhile (WiFi.status() != WL_CONNECTED) {\n  Serial.print(\".\");\n  delay(500);\n}<\/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\">sendPhoto<\/span> variable. If it is <span class=\"rnthl rntliteral\">true<\/span>, call the <span class=\"rnthl rntliteral\">sendPhotoTelegram()<\/span> function to take and send a photo to your telegram account.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (sendPhoto) {\n  Serial.println(\"Preparing photo\");\n  sendPhotoTelegram();<\/code><\/pre>\n\n\n\n<p>When it\u2019s done, set the <span class=\"rnthl rntliteral\">sendPhoto<\/span> variable to <span class=\"rnthl rntliteral\">false<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>sendPhoto = false;<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span>, you also check for new messages every second.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (millis() &gt; lastTimeBotRan + botRequestDelay)  {\n  int numNewMessages = bot.getUpdates(bot.last_message_received + 1);\n  while (numNewMessages) {\n    Serial.println(\"got response\");\n    handleNewMessages(numNewMessages);\n    numNewMessages = bot.getUpdates(bot.last_message_received + 1);\n  }\n  lastTimeBotRan = millis();\n}<\/code><\/pre>\n\n\n\n<p>When a new message arrives, call the <span class=\"rnthl rntliteral\">handleNewMessages()<\/span> function.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>while (numNewMessages) {\n  Serial.println(\"got response\");\n  handleNewMessages(numNewMessages);\n  numNewMessages = bot.getUpdates(bot.last_message_received + 1);\n}<\/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\">Demonstration<\/h2>\n\n\n\n<p>Upload the code to your ESP32-CAM 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-CAM on-board 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>Go to your Telegram account and open a conversation with your bot. Send the following commands and see the bot responding:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\/start<\/strong> shows the welcome message with the valid commands;<\/li>\n\n\n\n<li><strong>\/flash<\/strong> inverts the state of the LED flash;<\/li>\n\n\n\n<li><strong>\/photo<\/strong> takes a new photo and sends it to your Telegram account.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"321\" height=\"644\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/08\/ESP32-CAM-Take-Send-Photo-Telegram.jpg?resize=321%2C644&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32-CAM Take and Send Photo to Telegram Demonstration\" class=\"wp-image-98958\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/08\/ESP32-CAM-Take-Send-Photo-Telegram.jpg?w=321&amp;quality=100&amp;strip=all&amp;ssl=1 321w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/08\/ESP32-CAM-Take-Send-Photo-Telegram.jpg?resize=150%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 150w\" sizes=\"(max-width: 321px) 100vw, 321px\" \/><\/figure><\/div>\n\n\n<p>At the same time, on the Serial Monitor, you should see that the ESP32-CAM is receiving the messages.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"669\" height=\"567\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/08\/ESP32-CAM-Telegram-Serial-Monitor.png?resize=669%2C567&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32-CAM Send Photo to Telegram Serial Monitor\" class=\"wp-image-98955\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/08\/ESP32-CAM-Telegram-Serial-Monitor.png?w=669&amp;quality=100&amp;strip=all&amp;ssl=1 669w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/08\/ESP32-CAM-Telegram-Serial-Monitor.png?resize=300%2C254&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 669px) 100vw, 669px\" \/><\/figure><\/div>\n\n\n<p>If you try to interact with your bot from another account, you&#8217;ll get the &#8220;Unauthorized user&#8221; message.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"352\" height=\"711\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Control-ESP32-ESP8266-Outputs-Telegram-Unauthorized-User.png?resize=352%2C711&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-97845\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Control-ESP32-ESP8266-Outputs-Telegram-Unauthorized-User.png?w=352&amp;quality=100&amp;strip=all&amp;ssl=1 352w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Control-ESP32-ESP8266-Outputs-Telegram-Unauthorized-User.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<h1 class=\"wp-block-heading\">Wrapping Up<\/h1>\n\n\n\n<p>In this tutorial, you\u2019ve learned how to send a photo from the ESP32-CAM to your Telegram account. As long as you have access to the internet on your smartphone, you can request a new photo no matter where you are. This is great to monitor your ESP32-CAM from anywhere in the world.<\/p>\n\n\n\n<p>We have other tutorials using Telegram that you might be interested in:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-cam-shield-pcb-telegram\/\">ESP32-CAM with Telegram: Take Photos, Control Outputs, Request Sensor Readings and Motion Notifications<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/telegram-esp32-motion-detection-arduino\/\">Telegram: ESP32 Motion Detection with Notifications<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/telegram-control-esp32-esp8266-nodemcu-outputs\/\">Telegram: Control ESP32\/ESP8266 Outputs<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/telegram-request-esp32-esp8266-nodemcu-sensor-readings\/\">Telegram: Request ESP32\/ESP8266 Sensor Readings<\/a><\/li>\n<\/ul>\n\n\n\n<p>Learn more about the ESP32-CAM with our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><a href=\"https:\/\/randomnerdtutorials.com\/esp32-cam-projects-ebook\/\">Build ESP32-CAM Projects using Arduino IDE eBook<\/a><\/strong><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp32-cam\/\">More ESP32-CAM Projects and Tutorials&#8230;<\/a><\/li>\n<\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you\u2019ll create a Telegram bot to interact with the ESP32-CAM to request a new photo. You can request a new photo using your Telegram account from anywhere. &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Telegram: ESP32-CAM Take and Send Photo (Arduino IDE)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/telegram-esp32-cam-photo-arduino\/#more-98951\" aria-label=\"Read more about Telegram: ESP32-CAM Take and Send Photo (Arduino IDE)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":136580,"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":[281,276,277,319,264],"tags":[],"class_list":["post-98951","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32-project","category-esp32","category-esp32-arduino-ide","category-esp32-cam","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/08\/ESP32-CAM-Telegram-Picture.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\/98951","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\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=98951"}],"version-history":[{"count":6,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/98951\/revisions"}],"predecessor-version":[{"id":136585,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/98951\/revisions\/136585"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/136580"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=98951"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=98951"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=98951"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}