Telegram Bot: Request ESP32/ESP8266 Sensor Readings from Anywhere
Request ESP32 or ESP8266 NodeMCU sensor readings from anywhere using the Telegram application. You'll request temperature and humidity with a BME280 sensor.
- Suitable forB.Tech/Diploma IoT/ESP32 students
- Build time1-2 weeks
- LevelBeginner
- Available atAjvika Labs, Kakinada
About this project
This guide shows how to request ESP32 or ESP8266 NodeMCU sensor readings using Telegram. As an example, we’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.
Project Overview
In this tutorial we’ll build a simple project that requests ESP32 or ESP8266 NodeMCU temperature and humidity readings using the Telegram app. We’ll use a BME280 sensor, but you can use any
other sensor
You’ll create a Telegram bot for your ESP32 or ESP8266 NodeMCU board;
You can start a conversation with the bot;
When you send the message
/readings
to the bot, the ESP board receives the message and responds with the current temperature and humidity readings;
You can send the
/start
message to receive a welcome message with the commands to control the board.
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.
Introducing Telegram App
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.
“
Bots are third-party applications that run inside Telegram. Users can interact with bots by sending them messages, commands and inline requests. You control your bots using HTTPS requests to Telegram Bot API
“.
The ESP32/ESP8266 will interact with the Telegram bot to receive and handle the messages, and send responses. In this tutorial you’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).
Creating a Telegram Bot
Go to Google Play or App Store, download and install
Open Telegram and follow the next steps to create a Telegram Bot. First, search for “
botfather
” and click the BotFather as shown below. Or open this link
in your smartphone.
The following window should open and you’ll be prompted to click the
start
button.
Type
/newbot
and follow the instructions to create your bot. Give it a name and username.
If your bot is successfully created, you’ll receive a message with a link to access the bot and the
bot token
. Save the bot token because you’ll need it so that the ESP32/ESP8266 can interact with the bot.
Get Your Telegram User ID
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.
In your Telegram account, search for “IDBot” or open this link
in your smartphone.
Start a conversation with that bot and type
/getid
. You will get a reply back with your user ID. Save that
user ID
, because you’ll need it later in this tutorial.
Preparing Arduino IDE
We’ll program the
ESP32
and
ESP8266
boards using Arduino IDE, so make sure you have them installed in your Arduino IDE.
Installing the ESP32 Board in Arduino IDE (Windows, Mac OS X, Linux)
Installing ESP8266 Board in Arduino IDE (Windows, Mac OS X, Linux)
Universal Telegram Bot Library
To interact with the Telegram bot, we’ll use the
Universal Telegram Bot Library
created by Brian Lough that provides an easy interface for the Telegram Bot API.
Follow the next steps to install the latest release of the library.
Click here to download the Universal Arduino Telegram Bot library
Go to
Sketch
>
Include Library
>
Add.ZIP Library..
Add the library you’ve just downloaded.
Important:
don’t install the library through the Arduino Library Manager because it might install a deprecated version.
For all the details about the library, take a look at the Universal Arduino Telegram Bot Library
GitHub
page.
ArduinoJson Library
You also have to install the
ArduinoJson
library. Follow the next steps to install the library.
Go to
Skech
>
Include Library
>
Manage Libraries
Search for “ArduinoJson”.
Install the library.
We’re using ArduinoJson library version 6.15.2.
BME280 Sensor Libraries
To get readings from the BME280 sensor module, we’ll use the
Adafruit_BME280 library
. You also need to install the
Adafruit_Sensor library
. Follow the next steps to install the libraries in your Arduino IDE:
1. Open your Arduino IDE and go to
Sketch
>
Include Library
>
Manage Libraries
. The Library Manager should open.
2. Search for “
adafruit bme280
” on the Search box and install the library.
To use the BME280 library, you also need to install the
Adafruit Unified Sensor
. Follow the next steps to install the library in your Arduino IDE:
3. Search for “
Adafruit Unified Sensor
“in the search box. Scroll all the way down to find the library and install it.
After installing the libraries, restart your Arduino IDE.
Parts Required
For this example we’ll get sensor readings from the BME280 sensor. Here’s a list of parts you need to build the circuit for this project:
ESP32 board
(read
Best ESP32 dev boards
)
Alternative –
ESP8266
board (read
Best ESP8266 dev boards
)
BME280 sensor
Jumper wires
Breadboard
You can use the preceding links or go directly to
MakerAdvisor.com/tools
to find all the parts for your projects at the best price!
Schematic Diagram
The BME280 sensor module we’re using communicates via
I2C communication protocol
, so you need to connect it to the ESP32 or ESP8266 I2C pins.
BME280 wiring to ESP32
The default
ESP32 I2C pins
are:
GPIO 22
:
SCL
(SCK)
GPIO 21
:
SDA
(SDI)
So, assemble your circuit as shown in the next schematic diagram (
Guide for ESP32 with BME280
and
ESP32 BME280 Web Server
).
Recommended reading:
ESP32 Pinout Reference Guide
BME280 wiring to ESP8266 NodeMCU
The default ESP8266 I2C pins are:
GPIO 5
(D1):
SCL
(SCK)
GPIO 4
(D2):
SDA
(SDI)
Assemble your circuit as in the next schematic diagram if you’re using an ESP8266 board (
Guide for ESP8266 NodeMCU with BME280
).
Recommended reading:
ESP8266 Pinout Reference Guide
Telegram Request Sensor Readings – Code
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.
/*
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
Project created using Brian Lough's Universal Telegram Bot Library: https://github.com/witnessmenow/Universal-Arduino-
*/
#ifdef ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h> // Universal Telegram Bot Library written by Brian Lough: https://github.com/witnessmenow/Universal-Arduino-
#include <ArduinoJson.h>
#include <Adafruit_BME280.h>
#include <Adafruit_Sensor.h>
// Replace with your network credentials
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";
// Use @myidbot to find out the chat ID of an individual or a group
// Also note that you need to click "start" on a bot before it can
// message you
#define CHAT_ID "XXXXXXXXXX"
// Initialize Telegram BOT
#define BOTtoken "XXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get from Botfather)
#ifdef ES
Project Overview
In this tutorial we’ll build a simple project that requests ESP32 or ESP8266 NodeMCU temperature and humidity readings using the Telegram app. We’ll use a BME280 sensor, but you can use any
other sensor
You’ll create a Telegram bot for your ESP32 or ESP8266 NodeMCU board;
You can start a conversation with the bot;
When you send the message
/readings
to the bot, the ESP board receives the message and responds with the current temperature and humidity readings;
You can send the
/start
message to receive a welcome message with the commands to control the board.
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.
Introducing Telegram App
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.
“
Bots are third-party applications that run inside Telegram. Users can interact with bots by sending them messages, commands and inline requests. You control your bots using HTTPS requests to Telegram Bot API
“.
The ESP32/ESP8266 will interact with the Telegram bot to receive and handle the messages, and send responses. In this tutorial you’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).
Creating a Telegram Bot
Go to Google Play or App Store, download and install
Open Telegram and follow the next steps to create a Telegram Bot. First, search for “
botfather
” and click the BotFather as shown below. Or open this link
in your smartphone.
The following window should open and you’ll be prompted to click the
start
button.
Type
/newbot
and follow the instructions to create your bot. Give it a name and username.
If your bot is successfully created, you’ll receive a message with a link to access the bot and the
bot token
. Save the bot token because you’ll need it so that the ESP32/ESP8266 can interact with the bot.
Get Your Telegram User ID
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.
In your Telegram account, search for “IDBot” or open this link
in your smartphone.
Start a conversation with that bot and type
/getid
. You will get a reply back with your user ID. Save that
user ID
, because you’ll need it later in this tutorial.
Preparing Arduino IDE
We’ll program the
ESP32
and
ESP8266
boards using Arduino IDE, so make sure you have them installed in your Arduino IDE.
Installing the ESP32 Board in Arduino IDE (Windows, Mac OS X, Linux)
Installing ESP8266 Board in Arduino IDE (Windows, Mac OS X, Linux)
Universal Telegram Bot Library
To interact with the Telegram bot, we’ll use the
Universal Telegram Bot Library
created by Brian Lough that provides an easy interface for the Telegram Bot API.
Follow the next steps to install the latest release of the library.
Click here to download the Universal Arduino Telegram Bot library
Go to
Sketch
>
Include Library
>
Add.ZIP Library..
Add the library you’ve just downloaded.
Important:
don’t install the library through the Arduino Library Manager because it might install a deprecated version.
For all the details about the library, take a look at the Universal Arduino Telegram Bot Library
GitHub
page.
ArduinoJson Library
You also have to install the
ArduinoJson
library. Follow the next steps to install the library.
Go to
Skech
>
Include Library
>
Manage Libraries
Search for “ArduinoJson”.
Install the library.
We’re using ArduinoJson library version 6.15.2.
BME280 Sensor Libraries
To get readings from the BME280 sensor module, we’ll use the
Adafruit_BME280 library
. You also need to install the
Adafruit_Sensor library
. Follow the next steps to install the libraries in your Arduino IDE:
1. Open your Arduino IDE and go to
Sketch
>
Include Library
>
Manage Libraries
. The Library Manager should open.
2. Search for “
adafruit bme280
” on the Search box and install the library.
To use the BME280 library, you also need to install the
Adafruit Unified Sensor
. Follow the next steps to install the library in your Arduino IDE:
3. Search for “
Adafruit Unified Sensor
“in the search box. Scroll all the way down to find the library and install it.
After installing the libraries, restart your Arduino IDE.
Parts Required
For this example we’ll get sensor readings from the BME280 sensor. Here’s a list of parts you need to build the circuit for this project:
ESP32 board
(read
Best ESP32 dev boards
)
Alternative –
ESP8266
board (read
Best ESP8266 dev boards
)
BME280 sensor
Jumper wires
Breadboard
You can use the preceding links or go directly to
MakerAdvisor.com/tools
to find all the parts for your projects at the best price!
Schematic Diagram
The BME280 sensor module we’re using communicates via
I2C communication protocol
, so you need to connect it to the ESP32 or ESP8266 I2C pins.
BME280 wiring to ESP32
The default
ESP32 I2C pins
are:
GPIO 22
:
SCL
(SCK)
GPIO 21
:
SDA
(SDI)
So, assemble your circuit as shown in the next schematic diagram (
Guide for ESP32 with BME280
and
ESP32 BME280 Web Server
).
Recommended reading:
ESP32 Pinout Reference Guide
BME280 wiring to ESP8266 NodeMCU
The default ESP8266 I2C pins are:
GPIO 5
(D1):
SCL
(SCK)
GPIO 4
(D2):
SDA
(SDI)
Assemble your circuit as in the next schematic diagram if you’re using an ESP8266 board (
Guide for ESP8266 NodeMCU with BME280
).
Recommended reading:
ESP8266 Pinout Reference Guide
Telegram Request Sensor Readings – Code
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.
/*
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
Project created using Brian Lough's Universal Telegram Bot Library: https://github.com/witnessmenow/Universal-Arduino-
*/
#ifdef ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h> // Universal Telegram Bot Library written by Brian Lough: https://github.com/witnessmenow/Universal-Arduino-
#include <ArduinoJson.h>
#include <Adafruit_BME280.h>
#include <Adafruit_Sensor.h>
// Replace with your network credentials
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";
// Use @myidbot to find out the chat ID of an individual or a group
// Also note that you need to click "start" on a bot before it can
// message you
#define CHAT_ID "XXXXXXXXXX"
// Initialize Telegram BOT
#define BOTtoken "XXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get from Botfather)
#ifdef ES
Technologies used
Hardware components
- ESP32
- ESP8266
- Arduino
- BME280
Software & tools
- Arduino IDE
What you get
- Working project / hardware kit, tested before delivery
- Complete source code with explanation
- Project demo and guidance for your viva
- Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed.
Payment is in two parts:
• 50% advance to start the project
• Remaining 50% before delivery
Note:
• Documentation and PPT are not included by default
• If you need documentation/PPT, it can be arranged for extra time and cost
We'll confirm the exact timeline and cost with you after reviewing your requirements.