In this tutorial, we’re going to show you how to control an LED through Telegram using an ESP32 or ESP8266 and the Arduino IDE. You’ll need to have a Telegram account and a Telegram bot We will use the Telegram Bot API to control the bot.
We can control any LED using our Telegram Bot. You just need to send a message to your bot to set your output to High or Low. check out Obstacle Avoidance Sensor with ESP32
Required Material
- ESP8266
- LED
- Jumper Wires
- Breadboard
Creating a Telegram Bot
Download and install Telegram on your device. Telegram is a messaging app with a large user base. It is very popular and can be found on Google Play or App Store.
Open Telegram and follow the next steps to create a Telegram Bot. First, find “botfather” on Telegram and click the BotFather as shown below. Or open this link t.me/botfather on your smartphone.
We will create a new bot, using the Telegram Bot API. Botfather will create a new Telegram bot for you and add it to your account. After the bot is created, you need to configure it and add some bots.
The following window should open and you should click the “/Start” button.
To create a new bot, click or type /newbot and follow the instructions. Give it a name and username.
If your bot has been created successfully, you will receive a message with a Username to access the bot and the bot token. Save the bot token because you will need it so that the ESP8266/ESP32 can interact with the bot.
Installing Telegram Bot Library
At first, you may download the Telegram Bot library (download) and then add it to Arduino IDE.
https://github.com/cotestatnt/asynctelegram
Follow these steps to install the library.
- Click here to download the ArduinoJson and async-telegram Telegram Bot library.
- Go to Sketch > Include Library > Add.ZIP Library...
- Add the library you’ve just downloaded.
Note: Do not install the library through the Arduino Library Manager, because it might install a deprecated version. Instead, install the library manually.
Source code
This program will be able to be compiled with both the ESP8266 and the ESP32. I used a Wemos D1 ESP8266 board and a DOIT ESP32 DEVKIT V1 board to test.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
#include <Arduino.h> #include "AsyncTelegram.h" AsyncTelegram myBot; const char* ssid = "ESP Deaughter"; const char* pass = "esp_wifi_scan"; const char* token = "5717203763:AAHn2To3dt6_6RtkF9qCw3g6IZnRYMWjbeI"; int led = 2; void setup() { // initialize the Serial Serial.begin(115200); Serial.println("Starting TelegramBot..."); WiFi.setAutoConnect(true); WiFi.mode(WIFI_STA); WiFi.begin(ssid, pass); delay(500); while (WiFi.status() != WL_CONNECTED) { Serial.print('.'); delay(500); } myBot.setUpdateTime(1000); myBot.setTelegramToken(token); Serial.print("\nTest Telegram connection... "); myBot.begin() ? Serial.println("OK") : Serial.println("NOK"); pinMode(led, OUTPUT); digitalWrite(led, HIGH); } void loop() { TBMessage msg; if (myBot.getNewMessage(msg)) { if (msg.text.equalsIgnoreCase("On")) { digitalWrite(led, LOW); myBot.sendMessage(msg, "Light is now ON"); } else if (msg.text.equalsIgnoreCase("Off")) { digitalWrite(led, HIGH); myBot.sendMessage(msg, "Light is now OFF"); } else { // generate the message for the sender String reply; reply = "Welcome In DIYPROJECTSLAB.Com BOT\n" ; reply += "You can use below commands to operate:\n" ; reply += "============================\n\n" ; //reply += msg.sender.username; reply += ". Light on = On\n"; reply += ". Light off = Off\n"; myBot.sendMessage(msg, reply); } } } |
Change Credentials
1 2 3 |
const char* ssid = "REPLACE_WITH_YOUR_SSID"; const char* pass = "REPLACE_WITH_YOUR_PASS"; const char* token = "REPLACE_WITH_YOUR_TOKEN"; |
Upload the code to your ESP32 or ESP8266 board.
Testing
Ensure the ESP is up and running and connected to your router. Check in the Serial Monitor to see if the connection with Telegram has been established.
Back to the Telegram Messenger app & switch over to your phone’s screen and access the messaging screen with your Bot.
Send the following commands to observe the bot’s response:
- On turns the LED on.
- Off turns the LED off.
Then just type On (or On or Off ) and Onboard LED will turn on. Type off and it will turn off again. Each time you will get a message back from the ESP to verify that the command has been issued and approved by the ESP.