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+

ESP32 Starter Kit

You can also buy them separately from the links below.

COMPONENT INTRODUCTION

PURCHASE LINK

ESP32 WROOM 32E

BUY

ESP32 Camera Extension

-

Breadboard

BUY

Jumper Wires

BUY

Resistor

BUY

LED

BUY

PIR Motion Sensor Module

BUY

Creating a Telegram Bot

  1. Head to Google Play or the App Store and download and install Telegram.

  2. 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.

    ../../_images/sp230515_135927.png
  3. Upon opening, you’ll be presented with a chat window. Send the command /start.

    ../../_images/sp230515_140149.png
  4. 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.

    ../../_images/sp230515_140830.png

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.

  1. In your Telegram account, search for IDBot or open the link: t.me/myidbot.

    ../../_images/sp230515_144241.png
  2. Send the command /getid. Save the provided ID for later use in our program.

    ../../_images/sp230515_144614.png

Upload the Code

  1. First plug in the camera.

  2. Build the circuit.

    ../../_images/iot_7_cam_telegram_bb.png
  3. Open the code.

    • Open the iot_7_cam_telegram.ino file located in the esp32-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.

    • Always displaying “Unknown COMxx”?

    • The UniversalTelegramBot and ArduinoJson libraries are used here, you can install them from the Library Manager.

  4. 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>";
    
  1. 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>";
    
  2. Update the next line, substituting <BOTTOKEN> with the token of your Telegram BOT, which was provided by @BotFather.

    // Initialize Telegram BOT
    String BOTtoken = "<BOTTOKEN>";
    
  3. After selecting the correct board (ESP32 Dev Module) and port, click the Upload button.

  4. 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!
    
  5. Now, you can interact with your ESP32 via Telegram.

    ../../_images/sp230515_161237.png