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:
- motion is detected
- an object is recognized
- a person is detected...
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:
- is easily accessible via API
- runs 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. 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.
Arduino IDE Tools configuration for ESP32S3
!!! Install ESP32 core version 2.x, version 3.x won't work !!!
Board | ESP32S3 Dev Module |
Upload Speed | 921600 |
USB Mode | Hardware CDC and JTAG |
USB CDC On Boot | Disabled |
USB Firmware MSC On Boot | Disabled |
USB DFU On Boot | Disabled |
Upload Mode | UART0 / Hardware CDC |
CPU Frequency | 240MHz (WiFi) |
Flash Mode | QIO 80MHz |
Flash Size | 4MB (32Mb) |
Partition Scheme | Huge APP (3MB No OTA/1MB SPIFFS) |
Core Debug Level | Info |
PSRAM | OPI PSRAM |
Arduino Runs On | Core 1 |
Events Run On | Core 1 |
Erase All Flash Before Sketch Upload | Disabled |
JTAG Adapter | Disabled |
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.
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");
}
The actually relevant lines are
while (!telegram.begin().isOk()) {}
to connect to the Telegram API and
if (telegram.to(TELEGRAM_CHAT).send(text).isOk()) {}
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!
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");
}
As you can see, the only difference is that we replaced
if (telegram.to(TELEGRAM_CHAT).send(text).isOk())
with
if (telegram.to(TELEGRAM_CHAT).send(camera.frame).isOk())
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.
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.
Replace this number in the CHAT_ID
define.