{"id":97812,"date":"2020-07-09T13:37:34","date_gmt":"2020-07-09T13:37:34","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=97812"},"modified":"2023-09-11T20:54:18","modified_gmt":"2023-09-11T20:54:18","slug":"telegram-control-esp32-esp8266-nodemcu-outputs","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/telegram-control-esp32-esp8266-nodemcu-outputs\/","title":{"rendered":"Telegram: Control ESP32\/ESP8266 Outputs (Arduino IDE)"},"content":{"rendered":"\n<p>This guide shows how to control the ESP32 or ESP8266 NodeMCU GPIOs from anywhere in the world using Telegram. As an example, we\u2019ll control an LED, but you can control any other output. You just need to send a message to your Telegram Bot to set your outputs HIGH or LOW. The ESP boards will be programmed using Arduino IDE.<\/p>\n\n\n<div class=\"wp-block-image\">\n<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-ESP8266-NodeMCU-Control-Outputs-LED-Telegram-Arduino.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 NodeMCU Control Outputs LED Telegram Arduino\" class=\"wp-image-98051\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-ESP8266-NodeMCU-Control-Outputs-LED-Telegram-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-ESP8266-NodeMCU-Control-Outputs-LED-Telegram-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-ESP8266-NodeMCU-Control-Outputs-LED-Telegram-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-ESP8266-NodeMCU-Control-Outputs-LED-Telegram-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<h2 class=\"wp-block-heading\">Project Overview<\/h2>\n\n\n\n<p>In this tutorial we\u2019ll build a simple project that allows you to control ESP32 or ESP8266 NodeMCU GPIOs using Telegram. You can also <a href=\"https:\/\/randomnerdtutorials.com\/esp32-relay-module-ac-web-server\/\">control a relay module<\/a>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"856\" height=\"580\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-ESP8266-NodeMCU-Telegram-Control-Outputs-Overview.png?resize=856%2C580&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 NodeMCU Telegram Control Outputs Overview\" class=\"wp-image-98056\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-ESP8266-NodeMCU-Telegram-Control-Outputs-Overview.png?w=856&amp;quality=100&amp;strip=all&amp;ssl=1 856w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-ESP8266-NodeMCU-Telegram-Control-Outputs-Overview.png?resize=300%2C203&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-ESP8266-NodeMCU-Telegram-Control-Outputs-Overview.png?resize=768%2C520&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 856px) 100vw, 856px\" \/><\/figure><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>You&#8217;ll create a Telegram bot for your ESP32\/ESP8266 board;<\/li>\n\n\n\n<li>You can start a conversation with the bot;<\/li>\n\n\n\n<li>When you send the message <strong>\/led_on<\/strong> to the bot, the ESP board receives the message and turns GPIO 2 on;<\/li>\n\n\n\n<li>Similarly, when you send the message <strong>\/led_off<\/strong>, it turns GPIO 2 off;<\/li>\n\n\n\n<li>Additionally, you can also send the message <strong>\/state<\/strong> to request the current GPIO state. When the ESP receives that message, the bot responds with the current GPIO state;<\/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<\/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 href=\"https:\/\/telegram.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">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\/ESP8266 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 control the ESP outputs from anywhere (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&nbsp;<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 \u201c<strong>botfather<\/strong>\u201d 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\u2019ll be prompted to click the&nbsp;<strong>start<\/strong>&nbsp;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&nbsp;<strong>\/newbot<\/strong>&nbsp;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\u2019ll receive a message with a link to access the bot and the&nbsp;<strong>bot token<\/strong>. Save the bot token because you\u2019ll need it so that the ESP32\/ESP8266 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<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\" title=\"Telegram: ESP32-CAM Take and Send Photo (Arduino IDE)\">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&nbsp;<strong>\/getid<\/strong>. You will get a reply back with your user ID. Save that&nbsp;<strong>user ID<\/strong>, because you\u2019ll 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> and <a href=\"https:\/\/makeradvisor.com\/tools\/esp8266-esp-12e-nodemcu-wi-fi-development-board\/\" target=\"_blank\" rel=\"noreferrer noopener\">ESP8266<\/a> boards 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\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/how-to-install-esp8266-board-arduino-ide\/\">Installing ESP8266 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 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\">\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>And that&#8217;s it. The library is installed.<\/p>\n\n\n\n<p class=\"rntbox rntcorange\"><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 href=\"https:\/\/github.com\/bblanchon\" target=\"_blank\" rel=\"noreferrer noopener\">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>Skech <\/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=\"\" 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\">Parts Required<\/h2>\n\n\n\n<p>For this example we\u2019ll control the ESP on-board LEDs:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<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>\n\n\n\n<li>Alternative \u2013&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/esp8266-esp-12e-nodemcu-wi-fi-development-board\/\" target=\"_blank\">ESP8266<\/a>&nbsp;board (read&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\">Best ESP8266 dev boards<\/a>)<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Control Outputs using Telegram &#8211; ESP32\/ESP8266 Sketch<\/h2>\n\n\n\n<p>The following code allows you to control your ESP32 or ESP8266 NodeMCU GPIOs by sending messages to a Telegram Bot. To make it 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\">\/*\n  Rui Santos\n  Complete project details at https:\/\/RandomNerdTutorials.com\/telegram-control-esp32-esp8266-nodemcu-outputs\/\n  \n  Project created using Brian Lough's Universal Telegram Bot Library: https:\/\/github.com\/witnessmenow\/Universal-Arduino-Telegram-Bot\n  Example based on the Universal Arduino Telegram Bot Library: https:\/\/github.com\/witnessmenow\/Universal-Arduino-Telegram-Bot\/blob\/master\/examples\/ESP8266\/FlashLED\/FlashLED.ino\n*\/\n\n#ifdef ESP32\n  #include &lt;WiFi.h&gt;\n#else\n  #include &lt;ESP8266WiFi.h&gt;\n#endif\n#include &lt;WiFiClientSecure.h&gt;\n#include &lt;UniversalTelegramBot.h&gt;   \/\/ Universal Telegram Bot Library written by Brian Lough: https:\/\/github.com\/witnessmenow\/Universal-Arduino-Telegram-Bot\n#include &lt;ArduinoJson.h&gt;\n\n\/\/ Replace with your network credentials\nconst char* ssid = &quot;REPLACE_WITH_YOUR_SSID&quot;;\nconst char* password = &quot;REPLACE_WITH_YOUR_PASSWORD&quot;;\n\n\/\/ Initialize Telegram BOT\n#define 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\n#define CHAT_ID &quot;XXXXXXXXXX&quot;\n\n#ifdef ESP8266\n  X509List cert(TELEGRAM_CERTIFICATE_ROOT);\n#endif\n\nWiFiClientSecure client;\nUniversalTelegramBot bot(BOTtoken, client);\n\n\/\/ Checks for new messages every 1 second.\nint botRequestDelay = 1000;\nunsigned long lastTimeBotRan;\n\nconst int ledPin = 2;\nbool ledState = LOW;\n\n\/\/ Handle what happens when you receive new messages\nvoid handleNewMessages(int numNewMessages) {\n  Serial.println(&quot;handleNewMessages&quot;);\n  Serial.println(String(numNewMessages));\n\n  for (int i=0; i&lt;numNewMessages; i++) {\n    \/\/ Chat id of the requester\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\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 control your outputs.\\n\\n&quot;;\n      welcome += &quot;\/led_on to turn GPIO ON \\n&quot;;\n      welcome += &quot;\/led_off to turn GPIO OFF \\n&quot;;\n      welcome += &quot;\/state to request current GPIO state \\n&quot;;\n      bot.sendMessage(chat_id, welcome, &quot;&quot;);\n    }\n\n    if (text == &quot;\/led_on&quot;) {\n      bot.sendMessage(chat_id, &quot;LED state set to ON&quot;, &quot;&quot;);\n      ledState = HIGH;\n      digitalWrite(ledPin, ledState);\n    }\n    \n    if (text == &quot;\/led_off&quot;) {\n      bot.sendMessage(chat_id, &quot;LED state set to OFF&quot;, &quot;&quot;);\n      ledState = LOW;\n      digitalWrite(ledPin, ledState);\n    }\n    \n    if (text == &quot;\/state&quot;) {\n      if (digitalRead(ledPin)){\n        bot.sendMessage(chat_id, &quot;LED is ON&quot;, &quot;&quot;);\n      }\n      else{\n        bot.sendMessage(chat_id, &quot;LED is OFF&quot;, &quot;&quot;);\n      }\n    }\n  }\n}\n\nvoid setup() {\n  Serial.begin(115200);\n\n  #ifdef ESP8266\n    configTime(0, 0, &quot;pool.ntp.org&quot;);      \/\/ get UTC time via NTP\n    client.setTrustAnchors(&amp;cert); \/\/ Add root certificate for api.telegram.org\n  #endif\n\n  pinMode(ledPin, OUTPUT);\n  digitalWrite(ledPin, ledState);\n  \n  \/\/ Connect to Wi-Fi\n  WiFi.mode(WIFI_STA);\n  WiFi.begin(ssid, password);\n  #ifdef ESP32\n    client.setCACert(TELEGRAM_CERTIFICATE_ROOT); \/\/ Add root certificate for api.telegram.org\n  #endif\n  while (WiFi.status() != WL_CONNECTED) {\n    delay(1000);\n    Serial.println(&quot;Connecting to WiFi..&quot;);\n  }\n  \/\/ Print ESP32 Local IP Address\n  Serial.println(WiFi.localIP());\n}\n\nvoid loop() {\n  if (millis() &gt; lastTimeBotRan + botRequestDelay)  {\n    int numNewMessages = bot.getUpdates(bot.last_message_received + 1);\n\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\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP\/ESP_Telegram\/ESP_Telegram_Control_Outputs.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>The code is compatible with ESP32 and ESP8266 NodeMCU boards (it&#8217;s based on the Universal Arduino Telegram Bot library <a href=\"https:\/\/github.com\/witnessmenow\/Universal-Arduino-Telegram-Bot\/blob\/master\/examples\/ESP8266\/FlashLED\/FlashLED.ino\" target=\"_blank\" rel=\"noreferrer noopener\">example<\/a>). The code will load the right libraries accordingly to the selected board.<\/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>#ifdef ESP32\n  #include &lt;WiFi.h&gt;\n#else\n  #include &lt;ESP8266WiFi.h&gt;\n#endif\n#include &lt;WiFiClientSecure.h&gt;\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\">Define Output<\/h3>\n\n\n\n<p>Set the GPIO you want to control. In our case, we&#8217;ll control GPIO 2 (built-in LED) and its state is <span class=\"rnthl rntliteral\">LOW<\/span> by default.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>const int ledPin = 2;\nbool ledState = LOW;<\/code><\/pre>\n\n\n\n<p class=\"rntbox rntclblue\"><strong>Note: <\/strong> if you&#8217;re using an ESP8266, the built-in LED works with inverted logic. So, you should send a <span class=\"rnthl rntliteral\">LOW<\/span> signal to turn the LED on and a <span class=\"rnthl rntliteral\">HIGH<\/span> signal to turn it off.<\/p>\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<p>The <span class=\"rnthl rntliteral\">botRequestDelay<\/span> and <span class=\"rnthl rntliteral\">lastTimeBotRan<\/span> 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\">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.println(\"handleNewMessages\");\n  Serial.println(String(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 that particular message and store it in the <span class=\"rnthl rntliteral\">chat_id<\/span> variable. The chat ID allows us to identify 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 ESP32\/ESP8266. 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 control your outputs.\\n\\n\";\n  welcome += \"\/led_on to turn GPIO ON \\n\";\n  welcome += \"\/led_off to turn GPIO OFF \\n\";\n  welcome += \"\/state to request current GPIO state \\n\";\n  bot.sendMessage(chat_id, welcome, \"\");\n}<\/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&#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 particular example, we&#8217;ll send the message to the ID stored on the <span class=\"rnthl rntliteral\">chat_id<\/span> variable (that corresponds to the person who&#8217;ve sent the message) and send the message saved on the <span class=\"rnthl rntliteral\">welcome<\/span> 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> \/led_on<\/strong> message, turn the LED on and send a message confirming we&#8217;ve received the message. Also, update the <span class=\"rnthl rntliteral\">ledState<\/span> variable with the new state.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (text == \"\/led_on\") {\n  bot.sendMessage(chat_id, \"LED state set to ON\", \"\");\n  ledState = HIGH;\n  digitalWrite(ledPin, ledState);\n}<\/code><\/pre>\n\n\n\n<p>Do something similar for the <strong>\/led_off<\/strong> message.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (text == \"\/led_off\") {\n  bot.sendMessage(chat_id, \"LED state set to OFF\", \"\");\n  ledState = LOW;\n  digitalWrite(ledPin, ledState);\n}<\/code><\/pre>\n\n\n\n<p class=\"rntbox rntclblue\"><strong>Note: <\/strong> if you&#8217;re using an ESP8266, the built-in LED works with inverted logic. So, you should send a <span class=\"rnthl rntliteral\">LOW<\/span> signal to turn the LED on and a <span class=\"rnthl rntliteral\">HIGH<\/span> signal to turn it off.<\/p>\n\n\n\n<p>Finally, if the received message is <strong>\/state,<\/strong> check the current GPIO state and send a message accordingly.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (text == \"\/state\") {\n  if (digitalRead(ledPin)){\n    bot.sendMessage(chat_id, \"LED is ON\", \"\");\n  }\n  else{\n    bot.sendMessage(chat_id, \"LED is OFF\", \"\");\n  }\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<p>If you&#8217;re using the ESP8266, you need to use the following line:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#ifdef ESP8266\n  client.setInsecure();\n#endif<\/code><\/pre>\n\n\n\n<p>In the library examples for the ESP8266 they say: &#8220;<em>This is the simplest way of getting this working. If you are passing sensitive information, or controlling something important, please either use certStore or at least client.setFingerPrint<\/em>&#8220;.<\/p>\n\n\n\n<p>We have a tutorial showing how to make HTTPS requests with the ESP8266: <a href=\"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-https-requests\/\">ESP8266 NodeMCU HTTPS Requests (Arduino IDE)<\/a>.<\/p>\n\n\n\n<p>Set the LED as an output and set it to LOW when the ESP first starts:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>pinMode(ledPin, OUTPUT);\ndigitalWrite(ledPin, ledState);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Init Wi-Fi<\/h4>\n\n\n\n<p>Initialize Wi-Fi and connect the ESP 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<h3 class=\"wp-block-heading\">loop()<\/h3>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span>, check for new messages every second.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void loop() {\n  if (millis() &gt; lastTimeBotRan + botRequestDelay)  {\n    int numNewMessages = bot.getUpdates(bot.last_message_received + 1);\n\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  }\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\" id=\"demonstration\">Demonstration<\/h2>\n\n\n\n<p>Upload the code to your ESP32 or ESP8266 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\/ESP8266 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>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>\/led_on<\/strong> turns the LED on.<\/li>\n\n\n\n<li><strong>\/led_off<\/strong> turns the LED off.<\/li>\n\n\n\n<li><strong>\/state<\/strong> requests the current LED state.<\/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=\"352\" height=\"708\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Control-ESP32-ESP8266-Outputs-Telegram.png?resize=352%2C708&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Control ESP32 ESP8266 Outputs Telegram\" class=\"wp-image-97842\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Control-ESP32-ESP8266-Outputs-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\/Control-ESP32-ESP8266-Outputs-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>The on-board LED should turn on and turn off accordingly (the ESP8266 on-board LED works in reverse, it&#8217;s off when you send <strong>\/led_on<\/strong> and on when you send<strong> \/led_off<\/strong>).<\/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=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-board-Built_in-LED-turned-on-HIGH.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 board Built in LED turned on HIGH\" class=\"wp-image-96167\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-board-Built_in-LED-turned-on-HIGH.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-board-Built_in-LED-turned-on-HIGH.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>At the same time, on the Serial Monitor you should see that the ESP 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=\"435\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Control-ESP32-ESP8266-Outputs-Telegram-1.png?resize=669%2C435&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Control ESP32 ESP8266 Outputs Telegram Serial Monitor Demonstration\" class=\"wp-image-97843\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Control-ESP32-ESP8266-Outputs-Telegram-1.png?w=669&amp;quality=100&amp;strip=all&amp;ssl=1 669w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Control-ESP32-ESP8266-Outputs-Telegram-1.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<p>If you try to interact with your bot from another account, you&#8217;ll get the the &#8220;<strong>Unauthorized user<\/strong>&#8221; message.<\/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=\"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=\"Control ESP32 ESP8266 Outputs Telegram Unauthorized user\" 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<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 or ESP8266. With this bot, you can use your Telegram account to send messages to the ESP and control its outputs. The ESP can also interact with the bot to send responses.<\/p>\n\n\n\n<p>We&#8217;ve shown you a simple example on how to control an output. The idea is to modify the project to add more commands to execute other tasks. For example, you can <a href=\"https:\/\/randomnerdtutorials.com\/telegram-request-esp32-esp8266-nodemcu-sensor-readings\/\">request sensor readings<\/a> or send a message when motion is detected.<\/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>We hope you&#8217;ve found this project interesting. Learn more about the ESP32 and ESP8266 with our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/learn-esp32-with-arduino-ide\/\">Learn ESP32 with Arduino IDE (eBook + Video Course)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/home-automation-using-esp8266\/\">Home Automation using ESP8266<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp32\/\">More ESP32 projects and tutorials&#8230;<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp8266\/\">More ESP8266 projects and tutorials&#8230;<\/a><\/li>\n<\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This guide shows how to control the ESP32 or ESP8266 NodeMCU GPIOs from anywhere in the world using Telegram. As an example, we\u2019ll control an LED, but you can control &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Telegram: Control ESP32\/ESP8266 Outputs (Arduino IDE)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/telegram-control-esp32-esp8266-nodemcu-outputs\/#more-97812\" aria-label=\"Read more about Telegram: Control ESP32\/ESP8266 Outputs (Arduino IDE)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":98051,"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,299,264],"tags":[],"class_list":["post-97812","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32-project","category-esp32","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-ESP8266-NodeMCU-Control-Outputs-LED-Telegram-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\/97812","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=97812"}],"version-history":[{"count":1,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/97812\/revisions"}],"predecessor-version":[{"id":136285,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/97812\/revisions\/136285"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/98051"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=97812"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=97812"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=97812"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}