Video Surveillance System using ESP32-CAM

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

  • Ai-Thinker ESP32CAM
  • HC-SR04 ultrasonic sensor
  • 5V power source

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.

  • Telegram Bot init

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);
  • Main loop
void loop(){

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

  checkMotion();
  checkBotMessages();
}
  • Motion detection

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();
  }
}
  • Check if command was issued from Telegram bot

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

  • Asamblat

  • Bot Telegram

References

iothings/proiecte/2022sric/surveillance.txt · Last modified: 2023/06/02 09:57 by ruxandra.grigorie
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0