This is an old revision of the document!


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 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. The main loop of the program also after a fixed period checks 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>

// 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();
  }
}
  • Telegram bot picture send

The code gets one frame from the OV2640 camera and sends it to the Telegram Bot.

String sendPhotoTelegram(){
  const char* myDomain = "api.telegram.org";
  String getAll = "";
  String getBody = "";

  camera_fb_t * fb = NULL;
  fb = esp_camera_fb_get();  
  if(!fb) {
    delay(1000);
    ESP.restart();
    return "Camera capture failed";
  }  
  
  if (clientTCP.connect(myDomain, 443)) {    
    String head = "--CSIoT\r\nContent-Disposition: form-data; name=\"chat_id\"; \r\n\r\n" + chatId + "\r\n--CSIoT\r\nContent-Disposition: form-data; name=\"photo\"; filename=\"esp32-cam.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n";
    String tail = "\r\n--CSIoT--\r\n";

    uint16_t imageLen = fb->len;
    uint16_t extraLen = head.length() + tail.length();
    uint16_t totalLen = imageLen + extraLen;
  
    clientTCP.println("POST /bot"+BOTtoken+"/sendPhoto HTTP/1.1");
    clientTCP.println("Host: " + String(myDomain));
    clientTCP.println("Content-Length: " + String(totalLen));
    clientTCP.println("Content-Type: multipart/form-data; boundary=CSIoT");
    clientTCP.println();
    clientTCP.print(head);
  
    uint8_t *fbBuf = fb->buf;
    size_t fbLen = fb->len;
    for (size_t n=0;n<fbLen;n=n+1024) {
      if (n+1024<fbLen) {
        clientTCP.write(fbBuf, 1024);
        fbBuf += 1024;
      }
      else if (fbLen%1024>0) {
        size_t remainder = fbLen%1024;
        clientTCP.write(fbBuf, remainder);
      }
    }  
    
    clientTCP.print(tail);
    
    esp_camera_fb_return(fb);
    
    int waitTime = 10000;
    long startTimer = millis();
    boolean state = false;
    
    while ((startTimer + waitTime) > millis()){
      delay(100);      
      while (clientTCP.available()) {
        char c = clientTCP.read();
        if (state==true) getBody += String(c);        
        if (c == '\n') {
          if (getAll.length()==0) state=true; 
          getAll = "";
        } 
        else if (c != '\r')
          getAll += String(c);
        startTimer = millis();
      }
      if (getBody.length()>0) break;
    }
    clientTCP.stop();
  }
  else {
    getBody="Connected to api.telegram.org failed.";
  }
  return getBody;
}

Results

  • Asamblat

  • Bot Telegram

iothings/proiecte/2022sric/surveillance.1685681877.txt.gz · Last modified: 2023/06/02 07: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