{"id":97860,"date":"2020-07-08T14:26:27","date_gmt":"2020-07-08T14:26:27","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=97860"},"modified":"2020-09-02T10:40:09","modified_gmt":"2020-09-02T10:40:09","slug":"telegram-request-esp32-esp8266-nodemcu-sensor-readings","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/telegram-request-esp32-esp8266-nodemcu-sensor-readings\/","title":{"rendered":"Telegram: Request ESP32\/ESP8266 Sensor Readings (Arduino IDE)"},"content":{"rendered":"\n<p>This guide shows how to request ESP32 or ESP8266 NodeMCU sensor readings using Telegram. As an example, we&#8217;ll request temperature and humidity readings from a BME280 sensor. You just need to send a message to your Telegram Bot to monitor your sensors or inputs from anywhere in the world. The ESP boards will be programmed using Arduino IDE.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-ESP8266-NodeMCU-Send-Publish-Sensor-Readings-BME280-Telegram-Arduino.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 NodeMCU Send Publish Sensor Readings BME280 Telegram Arduino\" class=\"wp-image-98052\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-ESP8266-NodeMCU-Send-Publish-Sensor-Readings-BME280-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-Send-Publish-Sensor-Readings-BME280-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-Send-Publish-Sensor-Readings-BME280-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-Send-Publish-Sensor-Readings-BME280-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\n<h2 class=\"wp-block-heading\">Project Overview<\/h2>\n\n\n\n<p>In this tutorial we&#8217;ll build a simple project that requests ESP32 or ESP8266 NodeMCU temperature and humidity readings using the Telegram app. We&#8217;ll use a BME280 sensor, but you can use any <a href=\"https:\/\/randomnerdtutorials.com\/dht11-vs-dht22-vs-lm35-vs-ds18b20-vs-bme280-vs-bmp180\/\">other sensor<\/a>.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"890\" height=\"662\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-ESP8266-NodeMCU-Telegram-Request-Sensor-Readings-Project-Overview.png?resize=890%2C662&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 NodeMCU Telegram Request BME280 Sensor Readings Project Overview\" class=\"wp-image-98047\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-ESP8266-NodeMCU-Telegram-Request-Sensor-Readings-Project-Overview.png?w=890&amp;quality=100&amp;strip=all&amp;ssl=1 890w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-ESP8266-NodeMCU-Telegram-Request-Sensor-Readings-Project-Overview.png?resize=300%2C223&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-Request-Sensor-Readings-Project-Overview.png?resize=768%2C571&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 890px) 100vw, 890px\" \/><\/figure><\/div>\n\n\n\n<ul class=\"wp-block-list\"><li>You&#8217;ll create a Telegram bot for your ESP32 or ESP8266 NodeMCU board;<\/li><li>You can start a conversation with the bot;<\/li><li>When you send the message <strong>\/readings<\/strong> to the bot, the ESP board receives the message and responds with the current temperature and humidity readings;<\/li><li>You can send the <strong>\/start<\/strong> message to receive a welcome message with the commands to control the board. <\/li><\/ul>\n\n\n\n<p>This is a simple project, but shows how you can use Telegram in your IoT and Home Automation projects. The idea is to apply the concepts learned in your own projects.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introducing Telegram App<\/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 request sensor readings 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 <strong>Telegram<\/strong>.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"354\" height=\"269\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Install-Telegram.png?resize=354%2C269&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Install and Download Telegram application to your Android o iOS smartphone\" class=\"wp-image-97821\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Install-Telegram.png?w=354&amp;quality=100&amp;strip=all&amp;ssl=1 354w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Install-Telegram.png?resize=300%2C228&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 354px) 100vw, 354px\" \/><\/figure><\/div>\n\n\n\n<p>Open Telegram and follow the next steps to create a Telegram Bot. First, search for &#8220;<strong>botfather<\/strong>&#8221; and click the BotFather as shown below. Or open this link <a href=\"http:\/\/t.me\/botfather\" target=\"_blank\" rel=\"noreferrer noopener\">t.me\/botfather<\/a> in your smartphone.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"362\" height=\"198\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Telegram-Botfather.png?resize=362%2C198&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"botfather\" class=\"wp-image-97822\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Telegram-Botfather.png?w=362&amp;quality=100&amp;strip=all&amp;ssl=1 362w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Telegram-Botfather.png?resize=300%2C164&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 362px) 100vw, 362px\" \/><\/figure><\/div>\n\n\n\n<p>The following window should open and you&#8217;ll be prompted to click the <strong>start<\/strong> button.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"352\" height=\"708\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/start-bot-father-telegram.png?resize=352%2C708&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Telegram Start BotFather to Create a new Bot\" class=\"wp-image-97824\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/start-bot-father-telegram.png?w=352&amp;quality=100&amp;strip=all&amp;ssl=1 352w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/start-bot-father-telegram.png?resize=149%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 149w\" sizes=\"(max-width: 352px) 100vw, 352px\" \/><\/figure><\/div>\n\n\n\n<p>Type <strong>\/newbot<\/strong> and follow the instructions to create your bot. Give it a name and username.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"354\" height=\"396\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Create-bot-Telegram-Botfather_f.png?resize=354%2C396&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Telegram BotFather Create a New Bot\" class=\"wp-image-97826\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Create-bot-Telegram-Botfather_f.png?w=354&amp;quality=100&amp;strip=all&amp;ssl=1 354w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Create-bot-Telegram-Botfather_f.png?resize=268%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 268w\" sizes=\"(max-width: 354px) 100vw, 354px\" \/><\/figure><\/div>\n\n\n\n<p>If your bot is successfully created, you&#8217;ll receive a message with a link to access the bot and the <strong>bot token<\/strong>. Save the bot token because you&#8217;ll need it so that the ESP32\/ESP8266 can interact with the bot. <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"356\" height=\"537\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Bot-Token-Telegram-Bot-Father.png?resize=356%2C537&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Telegram BotFather Get Bot Token\" class=\"wp-image-97827\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Bot-Token-Telegram-Bot-Father.png?w=356&amp;quality=100&amp;strip=all&amp;ssl=1 356w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Bot-Token-Telegram-Bot-Father.png?resize=199%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 199w\" sizes=\"(max-width: 356px) 100vw, 356px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Get Your Telegram User ID<\/h2>\n\n\n\n<p>Anyone that knows your bot username can interact with it. To make sure that we ignore messages that are not from our Telegram account (or any authorized users), you can get your Telegram User ID. Then, when your telegram bot receives a message, the ESP can check whether the sender ID corresponds to your User ID and handle the message or ignore it. <\/p>\n\n\n\n<p>In your Telegram account, search for &#8220;IDBot&#8221; or open this link <a href=\"http:\/\/t.me\/myidbot\" target=\"_blank\" rel=\"noreferrer noopener\">t.me\/myidbot<\/a> in your smartphone.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"348\" height=\"173\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Telegram-ID-Bot.png?resize=348%2C173&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Telegram Get Chat ID with IDBot\" class=\"wp-image-97830\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Telegram-ID-Bot.png?w=348&amp;quality=100&amp;strip=all&amp;ssl=1 348w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Telegram-ID-Bot.png?resize=300%2C149&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 348px) 100vw, 348px\" \/><\/figure><\/div>\n\n\n\n<p>Start a conversation with that bot and type <strong>\/getid<\/strong>. You will get a reply back with your user ID. Save that <strong>user ID<\/strong>, because you&#8217;ll need it later in this tutorial.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"354\" height=\"500\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Telegram-Get-Chat-ID.png?resize=354%2C500&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Telegram Get Chat ID with IDBot getid\" class=\"wp-image-97836\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Telegram-Get-Chat-ID.png?w=354&amp;quality=100&amp;strip=all&amp;ssl=1 354w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Telegram-Get-Chat-ID.png?resize=212%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 212w\" sizes=\"(max-width: 354px) 100vw, 354px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Preparing Arduino IDE<\/h2>\n\n\n\n<p>We\u2019ll program the&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\">ESP32<\/a> 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\"><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><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><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Universal Telegram Bot Library<\/h3>\n\n\n\n<p>To interact with the Telegram bot, we&#8217;ll use the <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/witnessmenow\/Universal-Arduino-Telegram-Bot\" target=\"_blank\">Universal Telegram Bot Library<\/a> created by Brian Lough that provides an easy interface for the Telegram Bot API.<\/p>\n\n\n\n<p>Follow the next steps to install the latest release of the library.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/witnessmenow\/Universal-Arduino-Telegram-Bot\/archive\/master.zip\" target=\"_blank\">Click here to download the Universal Arduino Telegram Bot library<\/a>.<\/li><li>Go to <strong>Sketch <\/strong>&gt; <strong>Include Library<\/strong> &gt; <strong>Add.ZIP Library..<\/strong>.<\/li><li>Add the library you&#8217;ve just downloaded.<\/li><\/ol>\n\n\n\n<p><strong>Important: <\/strong> don&#8217;t install the library through the Arduino Library Manager because it might install a deprecated version.<\/p>\n\n\n\n<p>For all the details about the library, take a look at the Universal Arduino Telegram Bot Library <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/witnessmenow\/Universal-Arduino-Telegram-Bot\" target=\"_blank\">GitHub<\/a> page.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ArduinoJson Library<\/h3>\n\n\n\n<p>You also have to install the <a href=\"https:\/\/github.com\/bblanchon\/ArduinoJson\" 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\"><li>Go to <strong>Skech <\/strong>&gt; <strong>Include Library<\/strong> &gt; <strong>Manage Libraries<\/strong>.<\/li><li>Search for &#8220;ArduinoJson&#8221;.<\/li><li>Install the library.<\/li><\/ol>\n\n\n\n<p>We&#8217;re using ArduinoJson library version 6.15.2.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"786\" height=\"443\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Install-ArduinoJson-Library.png?resize=786%2C443&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Install ArduinoJson Library Arduino IDE\" class=\"wp-image-97837\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Install-ArduinoJson-Library.png?w=786&amp;quality=100&amp;strip=all&amp;ssl=1 786w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Install-ArduinoJson-Library.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Install-ArduinoJson-Library.png?resize=768%2C433&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">BME280 Sensor Libraries<\/h3>\n\n\n\n<p>To get readings from the BME280 sensor module, we\u2019ll use the&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/adafruit\/Adafruit_BME280_Library\" target=\"_blank\">Adafruit_BME280 library<\/a>. You also need to install the&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/adafruit\/Adafruit_Sensor\" target=\"_blank\">Adafruit_Sensor library<\/a>. Follow the next steps to install the libraries in your Arduino IDE:<\/p>\n\n\n\n<p>1. Open your Arduino IDE and go to&nbsp;<strong>Sketch&nbsp;<\/strong>&gt;&nbsp;<strong>Include Library<\/strong>&nbsp;&gt;&nbsp;<strong>Manage Libraries<\/strong>. The Library Manager should open.<\/p>\n\n\n\n<p>2. Search for \u201c<strong>adafruit bme280<\/strong>&nbsp;\u201d on the Search box and install the library.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"786\" height=\"443\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/08\/install-bme280.png?resize=786%2C443&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Installing BME280 library in Arduino IDE\" class=\"wp-image-70233\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/08\/install-bme280.png?w=786&amp;quality=100&amp;strip=all&amp;ssl=1 786w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/08\/install-bme280.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/08\/install-bme280.png?resize=768%2C433&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/figure><\/div>\n\n\n\n<p>To use the BME280 library, you also need to install the&nbsp;<a href=\"https:\/\/github.com\/adafruit\/Adafruit_Sensor\" target=\"_blank\" rel=\"noreferrer noopener\">Adafruit Unified Sensor<\/a>. Follow the next steps to install the library in your Arduino IDE:<\/p>\n\n\n\n<p>3. Search for \u201c<strong>Adafruit Unified Sensor<\/strong>\u201cin the search box. Scroll all the way down to find the library and install it.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/adafruit_unified_sensor_library.png?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Installing Adafruit Unified Sensor Driver library\" class=\"wp-image-84295\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/adafruit_unified_sensor_library.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/adafruit_unified_sensor_library.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>After installing the libraries, restart your Arduino IDE.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Parts Required<\/h2>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"421\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP32-BME280-circuit.jpg?resize=750%2C421&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-86632\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP32-BME280-circuit.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP32-BME280-circuit.jpg?resize=300%2C168&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>For this example we\u2019ll get sensor readings from the BME280 sensor. Here\u2019s a list of parts you need to build the circuit for this project:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\">ESP32 board<\/a>&nbsp;(read&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/esp32-development-boards-review-comparison\/\" target=\"_blank\">Best ESP32 dev boards<\/a>)<\/li><li>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><li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/bme280-sensor-module\/\" target=\"_blank\">BME280 sensor<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\">Jumper wires<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\">Breadboard<\/a><\/li><\/ul>\n\n\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Schematic Diagram<\/h2>\n\n\n\n<p>The BME280 sensor module we\u2019re using communicates via <a href=\"https:\/\/randomnerdtutorials.com\/esp32-i2c-communication-arduino-ide\/\">I2C communication protocol<\/a>, so you need to connect it to the ESP32 or ESP8266 I2C pins.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">BME280 wiring to ESP32<\/h4>\n\n\n\n<p>The default <a href=\"https:\/\/randomnerdtutorials.com\/esp32-i2c-communication-arduino-ide\/\">ESP32 I2C pins<\/a> are:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>GPIO 22<\/strong>:&nbsp;<strong>SCL<\/strong> (SCK)<\/li><li><strong>GPIO 21<\/strong>:&nbsp;<strong>SDA<\/strong> (SDI)<\/li><\/ul>\n\n\n\n<p>So, assemble your circuit as shown in the next schematic diagram (<a href=\"https:\/\/randomnerdtutorials.com\/esp32-bme280-arduino-ide-pressure-temperature-humidity\/\">Guide for ESP32 with BME280<\/a>&nbsp;and&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/esp32-web-server-with-bme280-mini-weather-station\/\">ESP32 BME280 Web Server<\/a>).<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"675\" height=\"670\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-BME280-Sensor-Temperature-Humidity-Pressure-Wiring-Diagram-Circuit.png?resize=675%2C670&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 BME280 Sensor Temperature Humidity Pressure Wiring Diagram Circuit\" class=\"wp-image-98048\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-BME280-Sensor-Temperature-Humidity-Pressure-Wiring-Diagram-Circuit.png?w=675&amp;quality=100&amp;strip=all&amp;ssl=1 675w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-BME280-Sensor-Temperature-Humidity-Pressure-Wiring-Diagram-Circuit.png?resize=300%2C298&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-BME280-Sensor-Temperature-Humidity-Pressure-Wiring-Diagram-Circuit.png?resize=150%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 150w\" sizes=\"(max-width: 675px) 100vw, 675px\" \/><\/figure><\/div>\n\n\n\n<p class=\"rntbox rntclblue\"><strong>Recommended reading:<\/strong>&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/esp32-pinout-reference-gpios\/\">ESP32 Pinout Reference Guide<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">BME280 wiring to ESP8266 NodeMCU<\/h4>\n\n\n\n<p>The default ESP8266 I2C pins are:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>GPIO 5&nbsp;<\/strong>(D1): <strong>SCL<\/strong> (SCK)<\/li><li><strong>GPIO 4&nbsp;<\/strong>(D2): <strong>SDA<\/strong> (SDI)<\/li><\/ul>\n\n\n\n<p>Assemble your circuit as in the next schematic diagram if you\u2019re using an ESP8266 board (<a href=\"https:\/\/randomnerdtutorials.com\/esp8266-bme280-arduino-ide\/\">Guide for ESP8266 NodeMCU with BME280<\/a>).<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"675\" height=\"531\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP8266-NodeMCU-BME280-Sensor-Temperature-Humidity-Pressure-Wiring-Diagram-Circuit.png?resize=675%2C531&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 NodeMCU BME280 Sensor Temperature Humidity Pressure Wiring Diagram Circuit\" class=\"wp-image-98049\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP8266-NodeMCU-BME280-Sensor-Temperature-Humidity-Pressure-Wiring-Diagram-Circuit.png?w=675&amp;quality=100&amp;strip=all&amp;ssl=1 675w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP8266-NodeMCU-BME280-Sensor-Temperature-Humidity-Pressure-Wiring-Diagram-Circuit.png?resize=300%2C236&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 675px) 100vw, 675px\" \/><\/figure><\/div>\n\n\n\n<p class=\"rntbox rntclblue\"><strong>Recommended reading:<\/strong>&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/esp8266-pinout-reference-gpios\/\">ESP8266 Pinout Reference Guide<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Telegram Request Sensor Readings &#8211; Code<\/h2>\n\n\n\n<p>The following code allows you to request BME280 sensor readings from your ESP32 or ESP8266 board by sending a message 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-request-esp32-esp8266-nodemcu-sensor-readings\/\n  \n  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.\n  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\n  Project created using Brian Lough's Universal Telegram Bot Library: https:\/\/github.com\/witnessmenow\/Universal-Arduino-Telegram-Bot\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#include &lt;Adafruit_BME280.h&gt;\n#include &lt;Adafruit_Sensor.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\/\/ 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\/\/ Initialize Telegram BOT\n#define BOTtoken &quot;XXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&quot;  \/\/ your Bot Token (Get from Botfather)\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\n\/\/ BME280 connect to ESP32 I2C (GPIO 21 = SDA, GPIO 22 = SCL)\n\/\/ BME280 connect to ESP8266 I2C (GPIO 4 = SDA, GPIO 5 = SCL)\nAdafruit_BME280 bme;         \n\n\/\/ Get BME280 sensor readings and return them as a String variable\nString getReadings(){\n  float temperature, humidity;\n  temperature = bme.readTemperature();\n  humidity = bme.readHumidity();\n  String message = &quot;Temperature: &quot; + String(temperature) + &quot; \u00baC \\n&quot;;\n  message += &quot;Humidity: &quot; + String (humidity) + &quot; % \\n&quot;;\n  return message;\n}\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 command to get current readings.\\n\\n&quot;;\n      welcome += &quot;\/readings \\n&quot;;\n      bot.sendMessage(chat_id, welcome, &quot;&quot;);\n    }\n\n    if (text == &quot;\/readings&quot;) {\n      String readings = getReadings();\n      bot.sendMessage(chat_id, readings, &quot;&quot;);\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  \/\/ Init BME280 sensor\n  if (!bme.begin(0x76)) {\n    Serial.println(&quot;Could not find a valid BME280 sensor, check wiring!&quot;);\n    while (1);\n  }\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_Request_Readings.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>The code is compatible with ESP32 and ESP8266 NodeMCU boards. 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 section 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>\n#else\n  #include &lt;ESP8266WiFi.h>\n#endif\n#include &lt;WiFiClientSecure.h>\n#include &lt;UniversalTelegramBot.h>\n#include &lt;ArduinoJson.h>\n#include &lt;Adafruit_BME280.h>\n#include &lt;Adafruit_Sensor.h><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Network Credentials<\/h3>\n\n\n\n<p>Insert your network credentials in the following variables.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>const char* ssid = \"REPLACE_WITH_YOUR_SSID\";\nconst char* password = \"REPLACE_WITH_YOUR_PASSWORD\";<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Telegram User ID<\/h3>\n\n\n\n<p>Insert your user 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<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<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\">BME280 Object<\/h3>\n\n\n\n<p>Create an <span class=\"rnthl rntliteral\">Adafruit_BME280<\/span> called <span class=\"rnthl rntliteral\">bme<\/span>. This creates an I2C object on the default ESP I2C pins.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Adafruit_BME280 bme; <\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">getReadings()<\/h3>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">getReadings()<\/span> function requests temperature and humidity from the BME280 sensor and returns the results as a string variable that we can send to the Telegram bot.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>String getReadings(){\n  float temperature, humidity;\n  temperature = bme.readTemperature();\n  humidity = bme.readHumidity();\n  String message = \"Temperature: \" + String(temperature) + \" \u00baC \\n\";\n  message += \"Humidity: \" + String (humidity) + \" % \\n\";\n  return message;\n}<\/code><\/pre>\n\n\n\n<p>To learn more about interfacing the BME280 sensor with the ESP32 and ESP8266, read:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-bme280-arduino-ide-pressure-temperature-humidity\/\">ESP32 with BME280 Sensor using Arduino IDE (Pressure, Temperature, Humidity)<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-bme280-arduino-ide\/\">ESP8266 with BME280 using Arduino IDE (Pressure, Temperature, Humidity)<\/a><\/li><\/ul>\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 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 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 command to get current readings.\\n\\n\";\n  welcome += \"\/readings \\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 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> \/readings<\/strong> message, get the current sensor readings by calling the <span class=\"rnthl rntliteral\">getReadings()<\/span> function. Then, simply send the message.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (text == \"\/readings\") {\n  String readings = getReadings();\n  bot.sendMessage(chat_id, readings, \"\");\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 Universal Telegram Bot Library library examples for the ESP8266, it says: &#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<h4 class=\"wp-block-heading\">Init BME280<\/h4>\n\n\n\n<p>Initialize the BME280 sensor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (!bme.begin(0x76)) {\n  Serial.println(\"Could not find a valid BME280 sensor, check wiring!\");\n  while (1);\n}<\/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() > 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  }\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 ESP board, open the <strong>Tools<\/strong> menu &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 ESP on-board EN\/RST button so that it starts running the code. Then, 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\"><li><strong>\/start<\/strong> shows the welcome message with the valid commands.<\/li><li><strong>\/readings<\/strong> returns the current temperature and humidity readings from the BME280 sensor.<\/li><\/ul>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"353\" height=\"378\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Request-ESP32-ESP8266-Sensor-Readings-Telegram_f.png?resize=353%2C378&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Request ESP32 ESP8266 Sensor Readings Telegram Demonstration\" class=\"wp-image-97870\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Request-ESP32-ESP8266-Sensor-Readings-Telegram_f.png?w=353&amp;quality=100&amp;strip=all&amp;ssl=1 353w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Request-ESP32-ESP8266-Sensor-Readings-Telegram_f.png?resize=280%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 280w\" sizes=\"(max-width: 353px) 100vw, 353px\" \/><\/figure><\/div>\n\n\n\n<p>At the same time, on the Serial Monitor, you should see that the ESP32 or ESP8266 is receiving the messages.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"669\" height=\"435\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/ESP32-ESP8266-Telegram-Request-Sensor-Readings-Serial-Monitor.png?resize=669%2C435&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 Telegram Request Sensor Readings Serial Monitor\" class=\"wp-image-97866\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/ESP32-ESP8266-Telegram-Request-Sensor-Readings-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\/06\/ESP32-ESP8266-Telegram-Request-Sensor-Readings-Serial-Monitor.png?resize=300%2C195&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 669px) 100vw, 669px\" \/><\/figure><\/div>\n\n\n\n<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\n<div class=\"wp-block-image\"><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 Request Sensor Readings Telegram app 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\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 NodeMCU boards. With this bot, you can use your Telegram account to monitor sensors and control outputs.<\/p>\n\n\n\n<p>We&#8217;ve shown you a simple example on how to request sensor readings from a BME280 sensor. 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-control-esp32-esp8266-nodemcu-outputs\/\">send a Telegram message to control outputs<\/a> or send a message to your account 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\"><li><a href=\"https:\/\/randomnerdtutorials.com\/learn-esp32-with-arduino-ide\/\">Learn ESP32 with Arduino IDE (eBook + Video Course)<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/home-automation-using-esp8266\/\">Home Automation using ESP8266<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp32\/\">More ESP32 projects and tutorials&#8230;<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp8266\/\">More ESP8266 projects and tutorials&#8230;<\/a><\/li><\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This guide shows how to request ESP32 or ESP8266 NodeMCU sensor readings using Telegram. As an example, we&#8217;ll request temperature and humidity readings from a BME280 sensor. You just need &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Telegram: Request ESP32\/ESP8266 Sensor Readings (Arduino IDE)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/telegram-request-esp32-esp8266-nodemcu-sensor-readings\/#more-97860\" aria-label=\"Read more about Telegram: Request ESP32\/ESP8266 Sensor Readings (Arduino IDE)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":98052,"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-97860","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-Send-Publish-Sensor-Readings-BME280-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\/97860","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=97860"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/97860\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/98052"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=97860"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=97860"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=97860"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}