8.7 ESP Camera with Telegram Bot¶
In this project, we’ll demonstrate how to integrate the ESP32 with your favorite messaging application. For this demonstration, we’re using Telegram.
Create a Telegram Bot, allowing you to control your circuit from anywhere, capture photos, and manage the flash. Moreover, whenever someone passes by your device, it will snap a new photo and send a notification to your Telegram account.
Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
---|---|---|
ESP32 Starter Kit |
320+ |
You can also buy them separately from the links below.
COMPONENT INTRODUCTION |
PURCHASE LINK |
---|---|
- |
|
Creating a Telegram Bot
Head to Google Play or the App Store and download and install Telegram.
Search for
botfather
in the Telegram app, and once it appears, click on it to open. or you can directly access this link: t.me/botfather.Upon opening, you’ll be presented with a chat window. Send the command
/start
.Enter
/newbot
and follow the provided instructions to create your bot. Once successful, the BotFather will provide you with the access link and API for your new bot.
Authorizing Telegram Users
As anyone can interact with the bot you’ve created, there’s a risk of information leakage. To address this, we want the bot to only respond to authorized users.
In your Telegram account, search for
IDBot
or open the link: t.me/myidbot.Send the command
/getid
. Save the provided ID for later use in our program.
Upload the Code
First plug in the camera.
Build the circuit.
Open the code.
Open the
iot_7_cam_telegram.ino
file located in theesp32-starter-kit-main\c\codes\iot_7_cam_telegram
directory, or copy the code into the Arduino IDE.After selecting the board (ESP32 Dev Module) and the appropriate port, click the Upload button.
The
UniversalTelegramBot
andArduinoJson
libraries are used here, you can install them from the Library Manager.
Locate and modify the following lines with your WiFi details, replacing
<SSID>
and<PASSWORD>
:// Replace the next variables with your SSID/Password combination const char* ssid = "<SSID>"; const char* password = "<PASSWORD>";
Update the next line, replacing
<CHATID>
with your Telegram ID, which you obtained from @IDBot.// 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 String chatId = "<CHATID>";
Update the next line, substituting
<BOTTOKEN>
with the token of your Telegram BOT, which was provided by @BotFather.// Initialize Telegram BOT String BOTtoken = "<BOTTOKEN>";
After selecting the correct board (ESP32 Dev Module) and port, click the Upload button.
Open the Serial Monitor. If an IP address is printed, this indicates successful execution.
Connecting to xxxx ESP32-CAM IP Address: 192.168.18.76 Init Done!
Now, you can interact with your ESP32 via Telegram.