ESP32-CAM: send photo to Telegram

ESP32-CAM: send photo to Telegram

When working with a remote control or  surveillance system, you need a way to get notified by your ESP32 camera when something of interest happens. This may be when motion is detected, an object is recognized or a person walks in front of the camera.

Regardless the source of the event, you need a way to get a notification out of the board directed to you. Among the many channels possible (SMS, phone call, email, push notification...), Telegram is one of the most used in the Arduino ecosystem. In fact, it has several desirable properties:

  • is easily accessible via an HTTP API
  • you have it installed on your phone, so you always have it with you
  • is free
  • works in realtime

There already exists a few libraries that cover most of the available options of the API and they work great. If you want to keep your dependencies count to the minimum and leverage the EloquentEsp32Cam3 library (which you really should!), I implemented the bare minimum features required (send text and send pictures).

Requirements

You will need to create a Telegram bot and find your Telegram ID to follow this project. The process is detailed at the bottom of this page (it is free and will take 2 minutes to complete).

The following sketch sends pictures taken with the ESP32-CAM along with a caption that you can enter using the Serial Monitor.

/**
 * Send image to custom Telegram bot.
 * You have to create one using BotFather.
 *
 * Open the Serial Monitor and enter "send"
 */
#include <eloquent_esp32cam3.h>
#include <eloquent_esp32cam3/modules/telegram.h>

#define BOT_TOKEN "xxxxxxxxxx:yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
#define CHAT_ID "0123456789"

using eloquent::camera::Camera;
using eloquent::camera::telegram::TelegramBot;

Camera camera;
TelegramBot telegram(BOT_TOKEN);


void setup() {
    Serial.begin(115200);
    Serial.println("Send image to Telegram bot");

    // see "GetStarted.ino" for more comments
    camera.hardware.brownout.disable();
    camera.hardware.clock.fast();
    camera.hardware.pinout.ask();
    camera.frame.pixformat.jpeg();
    camera.frame.resolution.vga();
    camera.frame.quality.high();

    // connect to Wi-Fi
    wifiConnect("SSID", "PASSWORD");

    // init camera and discard first frames
    // (bad illumination)
    camera.begin();
    camera.raise();
    camera.discard(2);

    // init Telegram
    telegram.setTimeout(10000);
    telegram.begin();
    telegram.raise();
    Serial.println("Telegram ready!");
}


void loop() {
    // await for Serial
    if (!Serial.available())
        return;

    // send photo with caption
    String caption = Serial.readStringUntil('\n');
    auto image = camera.grab();
    telegram.sendImage(CHAT_ID, image, caption);

    // report status
    if (telegram.failed()) {
        Serial.print("Error: ");
        Serial.println(telegram.error);
    } else {
        Serial.println("OK!");
    }
}

Hopefully, you can easily follow the sketch logic without too much hassle. The core of the sketch lies in the lines

String caption = Serial.readStringUntil('\n');
auto image = camera.grab();
telegram.sendImage(CHAT_ID, image, caption);

which collect the image caption from the Serial monitor, grab a picture from the camera and sends them via the Telegram API to your bot chat.

Related posts