Look, we’ve all been there. You build a beautifully over-engineered temperature monitor for your server rack. It works perfectly. But to check the temperature, you have to log into a local web server, remember the IP address, and squint at a tiny graph. What is this, 2012?
Or worse, you rely on serial monitor output, meaning your computer is permanently tethered to the board like it’s on life support. If the magic blue smoke escapes while you are at the grocery store, you won’t know until you get home to the smell of burnt silicon and regret.
Enter the Telegram Bot. By hooking your ESP32 Development Board up to the Telegram API, your microcontroller can literally text you. Did the garage door open? Ping. Is the soil moisture in your prized fern critically low? Ping. Do you want to turn on the living room lights by texting a robot? Done.
In this lesson, we will cover how to register a bot with the BotFather, handle secure HTTPS connections on the ESP32, and write the code to send and receive messages.
Before writing any code, you need a Telegram account and a Bot Token.
@BotFather./newbot command.You’ll need the UniversalTelegramBot library and ArduinoJson. Install them via the Arduino Library Manager.
Here is a minimal example of an ESP32 connecting to Wi-Fi and sending a message when it boots:
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
#define BOT_TOKEN "YOUR_BOT_TOKEN"
#define CHAT_ID "YOUR_CHAT_ID" // Get this from @IDBot
WiFiClientSecure secured_client;
UniversalTelegramBot bot(BOT_TOKEN, secured_client);
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
}
// Send a message!
bot.sendMessage(CHAT_ID, "Hello human. The ESP32 is online and awaiting your orders.", "");
}
void loop() {
// We will handle incoming messages here in the next section
}
This transforms your ESP32 from a silent brick into a communicative companion. In the next section, we’ll dive into parsing incoming messages so you can command it remotely. Be sure to check out our previous guide on ESP32 Battery Saving (Deep Sleep) if you plan on running this on a battery!