ESP32 cam Telegram notifications

ESP32 cam Telegram notifications cover

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.

Something of interest may be:

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, Telegram is one of the most used in the Arduino ecosystem, because it:

There already exists a few libraries that cover most of the available options of the API. If you want to stay lean, though, and leverage the syntactic style of the EloquentEsp32Cam library, you're in luck since there's a minimal implementation of Telegram for you.

Hardware requirements

This project works with S3 and non-S3 boards.

Software requirements

This project is tested on EloquentEsp32Cam version 2.0.5

You will need a Telegram Bot Token and a Chat ID. See appendix.

Send text message to Telegram

Sending a text message requires one line for configuration and one line to actually send the text. Here's a sketch that sends a message with the content you type in the the Serial monitor.

See source

Filename: Send_Text.ino

/**
 * Send text to Telegram
 *
 * BE SURE TO SET "TOOLS > CORE DEBUG LEVEL = INFO"
 * to turn on debug messages
 */
// WiFi credentials
#define WIFI_SSID "SSID"
#define WIFI_PASS "PASSWORD"

// replace with your bot token and chat id
#define TELEGRAM_TOKEN "1234567890:AABBCCDDEEFFGGHHIILLMMN-NOOPPQQRRSS"
#define TELEGRAM_CHAT "0123456789"

#include <eloquent_esp32cam.h>
#include <eloquent_esp32cam/extra/esp32/telegram.h>

using eloq::wifi;
using eloq::telegram;


/**
 * 
 */
void setup() {
    delay(3000);
    Serial.begin(115200);
    Serial.println("___TELEGRAM TEXT MESSAGE___");

    // connect to WiFi
    while (!wifi.connect().isOk())
      Serial.println(wifi.exception.toString());

    // connect to Telegram API
    while (!telegram.begin().isOk())
      Serial.println(telegram.exception.toString());

    Serial.println("Telegram OK");
    Serial.println("Enter the text you want to send to Telegram chat");
}


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

    String text = Serial.readStringUntil('\n');

    Serial.print("Sending text: ");
    Serial.println(text);
      
    // send
    if (telegram.to(TELEGRAM_CHAT).send(text).isOk())
        Serial.println("Message sent to Telegram!");
    else
        Serial.println(telegram.exception.toString());

  Serial.println("Enter the text you want to send to Telegram chat");
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

The actually relevant lines are

while (!telegram.begin().isOk()) {}
1

to connect to the Telegram API and

if (telegram.to(TELEGRAM_CHAT).send(text).isOk()) {}
1

to send the text message.

Send image to Telegram

Sending a picture to Telegram is as easy as sending a text: you only need to capture a frame first!

See source

Filename: Send_Image.ino

/**
 * Send image from camera to Telegram
 *
 * BE SURE TO SET "TOOLS > CORE DEBUG LEVEL = INFO"
 * to turn on debug messages
 */
// WiFi credentials
#define WIFI_SSID "SSID"
#define WIFI_PASS "PASSWORD"

// replace with your bot token and chat id
#define TELEGRAM_TOKEN "1234567890:AABBCCDDEEFFGGHHIILLMMN-NOOPPQQRRSS"
#define TELEGRAM_CHAT "0123456789"

#include <eloquent_esp32cam.h>
#include <eloquent_esp32cam/extra/esp32/telegram.h>

using eloq::camera;
using eloq::wifi;
using eloq::telegram;


/**
 * 
 */
void setup() {
    delay(3000);
    Serial.begin(115200);
    Serial.println("___TELEGRAM IMAGE___");

    // camera settings
    // replace with your own model!
    camera.pinout.aithinker();
    camera.brownout.disable();
    camera.resolution.vga();
    camera.quality.high();

    // init camera
    while (!camera.begin().isOk())
        Serial.println(camera.exception.toString());

    // connect to WiFi
    while (!wifi.connect().isOk())
      Serial.println(wifi.exception.toString());

    // connect to Telegram API
    while (!telegram.begin().isOk())
      Serial.println(telegram.exception.toString());

    Serial.println("Camera OK");
    Serial.println("Telegram OK");
    Serial.println("Enter 'image' (without quotes) to send image to Telegram chat");
}


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

    if (Serial.readStringUntil('\n') != "image") {
        Serial.println("I only understand 'image' (without quotes)");
        return;
    }

    Serial.println("Sending photo...");
      
    // capture new frame
    if (!camera.capture().isOk()) {
        Serial.println(camera.exception.toString());
        return;
    }
    
    // send
    if (telegram.to(TELEGRAM_CHAT).send(camera.frame).isOk())
        Serial.println("Photo sent to Telegram");
    else
        Serial.println(telegram.exception.toString());

    Serial.println("Enter 'image' (without quotes) to send image to Telegram chat");
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

As you can see, the only difference is that we replaced

if (telegram.to(TELEGRAM_CHAT).send(text).isOk())
1

with

if (telegram.to(TELEGRAM_CHAT).send(camera.frame).isOk())
1

Appendix A. Create a Telegram Bot

If you want to use your own Telegram Bot to get motion notifications, you need to create one.

Creating a Telegram bot is a straightforward process: there's a bot to do it!

Search for BotFather in your Telegram account and send him a /newbot message, then follow the instructions. You will have something similar to the  screenshot below.

esp32-cam-motion-detection-telegram-bot-creation.jpeg 196.63 KB
Replace this string in the TELEGRAM_TOKEN define.

Appendix B. Get your Chat ID

You can get your very own chat id messaging the myidbot bot from your Telegram account.

esp32-cam-motion-detection-telegram-getid.jpeg 142.39 KB
Replace this number in the CHAT_ID define.

Become an ESP32-CAM EXPERT

Related Posts

Subscribe to my newsletter

Join 1000 businesses and hobbysts skyrocketing their Arduino + ESP32 skills twice a month