Table of Contents

Video Surveillance System using ESP32-CAM

Grigorie Ruxandra - SRIC

Demo: https://www.canva.com/design/DAFkkIwiYsA/Pa3jRddZpZGIIisbDiHYRA/edit?utm_content=DAFkkIwiYsA&utm_campaign=designshare&utm_medium=link2&utm_source=sharebutton

Project Description

The purpose of this project is to have an ESP32CAM that can be remotely controlled to take and send a picture to the Telegram App, using a Telegram bot, and also to send a picture when motion is detected in front of the camera.

Hardware Description

Software Description

The main loop of the program checks after a fixed period if any command from the Telegram bot was issued. If the ”/photo” command was received, a new photo will be taken using the ESP32CAM and sent to the telegram bot API using a POST HTTP request. The main loop of the program also checks after a fixed period if the ultrasonic sensor detected movement, and if it did, it takes and sends a picture to the telegram bot API.

A Telegram bot can be easily created using the library <UniversalTelegramBot.h> The chatId corresponds to our own chat identification, and will be used in order to prevent commands being issued to the bot from other chats except ours.

// Universal Telegram chat identification 
String chatId = "1531821904";
// Universal Telegram bot identification 
String BOTtoken = "6257850545:AAHI7y5Yo9qIcwc4EECumoDjSS0gVwmpHcA";
UniversalTelegramBot bot(BOTtoken, clientTCP);
void loop(){

  if (sendPhoto){
    sendPhotoTelegram(); 
    digitalWrite(33, HIGH);
    sendPhoto = false; 
  }

  checkMotion();
  checkBotMessages();
}

If the ultrasonic sensor detects motion, a picture is sent to the Telegram Bot after motionDetectDelay miliseconds.

void checkMotion() {
  currentDistance = getDistance() + 10;
  if (currentDistance < initialDistance) {
    motionDetected = true;
  }
  if (millis() > lastTimeMeasured + motionDetectDelay) {
    if (motionDetected) {
      bot.sendMessage(chatId, "Motion detected!!", "");
      sendPhotoTelegram();
      motionDetected = false;
      digitalWrite(33, LOW);
    }
    lastTimeMeasured = millis();
  }
}

The code checks if a command was issued to the Telelgram bot, and if so, checks if the command was issued from the chat id that we configured as ours, in order to prevent others from issuing commands to our bot. If a valid command was issued, it is performed.

void handleNewMessages(int numNewMessages){
  for (int i = 0; i < numNewMessages; i++){
    // Chat id of the requester
    String chat_id = String(bot.messages[i].chat_id);
    if (chat_id != chatId){
      bot.sendMessage(chat_id, "Unauthorized user", "");
      continue;
    }
    
    // Print the received message
    String text = bot.messages[i].text;
   // Serial.println(text);

    String fromName = bot.messages[i].from_name;

    if (text == "/photo") {
      sendPhoto = true;
    }
    if (text == "/start"){
      String welcome = "Welcome to the ESP32-CAM Telegram bot.\n";
      welcome += "/photo : takes a new photo\n";
      welcome += "/readings : request sensor readings\n\n";
      welcome += "You'll receive a photo whenever motion is detected.\n";
      bot.sendMessage(chatId, welcome, "Markdown");
    }
  }
}

Results

References