PROJECT 12 · CLOUD / MQTT

ESP32 Telegram Bot

Chat with your board: read the sensor and switch the light from Telegram, anywhere.

⬇ Download the sketch Save Project_12_ESP32_Telegram_Bot.ino. Needs UniversalTelegramBot, ArduinoJson (v6+), and the DHT libraries, plus your Wi-Fi and a bot token.

1 · How it works

Telegram runs a bot API over HTTPS. The ESP32 asks Telegram "any new messages?" once a second and acts on them, and it can also send you a message when something happens (motion).

You (Telegram app) <-- HTTPS --> Telegram servers <-- HTTPS --> ESP32

Because it is just outbound HTTPS, this reaches your phone even on networks where a self-hosted web server would not.

CommandWhat it does
/statustemperature, humidity, light state
/light_on · /light_offswitch the LED or relay
/helplist the commands

2 · Create your bot

  1. In Telegram, chat with @BotFather and send /newbot.
  2. Pick a name and a username ending in bot. Copy the HTTP API token.
  3. Optional: open @userinfobot to get your numeric chat id for motion alerts.

3 · Libraries and wiring

Install: UniversalTelegramBot (Brian Lough), ArduinoJson (v6+), DHT sensor library, Adafruit Unified Sensor.

QtyPartNotes
1DHT11S to GPIO 4, + to 3.3 V, - to GND
1LED + 220 Ohm or relayto GPIO 26
1PIR (optional)VCC to 5 V, OUT to GPIO 27, GND to GND

4 · Edit, upload, chat

Edit the marked block: WIFI_SSID, WIFI_PASS, BOT_TOKEN, and optionally CHAT_ID. Upload, open Serial Monitor at 115200, then send /start to your bot in Telegram.

==============================================
 Project 12: ESP32 Telegram Bot
==============================================
Connecting to Wi-Fi: MyNetwork
....
Wi-Fi connected!
Bot ready. Open Telegram and send /start to your bot.

5 · Troubleshooting

SymptomLikely causeFix
Bot never repliesWrong tokenRe-copy from BotFather
Error on TELEGRAM_CERTIFICATE_ROOTWrong libraryUse UniversalTelegramBot by Brian Lough
JSON compile errorOld ArduinoJsonUpdate to v6 or newer
No motion alertsCHAT_ID empty or wrongGet your id from @userinfobot
← Project 11 · MQTT + Ubidots Project 13 · Capstone →