Security-Camera with Esp32 Cam, PIR and Telegram Bot (2024)

Things used in this project

Hardware components

Security-Camera with Esp32 Cam, PIR and Telegram Bot (1)
PIR Sensor, 7 m
×1
ESP32-CAM
×1
Security-Camera with Esp32 Cam, PIR and Telegram Bot (2)
Cable, USB to TTL Serial Converter 5V
×1
Security-Camera with Esp32 Cam, PIR and Telegram Bot (3)
Arduino Mega 2560
×1
  • Buy from Newark
  • Buy from Adafruit
  • Buy from Arduino Store
  • Buy from CPC

Story

Introduction - Introduzione

GitHub : https://github.com/albino98/telegram_esp32

ENG

I devised this project to create a smart security system, without the need for external input devices, such as a numeric keypad for alarm activation, etc.

In fact, the project allows you to manage the esp32 directly from your mobile phone or PC via a telegram bot.

Credits:

To create the project I took inspiration from other projects on the net, such as the following:

https://randomnerdtutorials.com/esp32-cam-take-photo-save-microsd-card/

ITA

Ho ideato questo progetto per creare un sistema di sicurezza smart, senza il bisogno di dispositivi esterni per l’input, come ad esempio un tastierino numerico per l’attivazione dell’allarme, ecc.

Il progetto infatti consente di gestire il l’esp32 direttamente dal cellulare o da pc tramite un bot telegram.

Credits:

Per creare il progetto ho preso spunto da altri progetti in rete, come ad esempio il seguente:

https://randomnerdtutorials.com/esp32-cam-take-photo-save-microsd-card/

Components - Componenti

ENG

  • ESP32 cam
  • PIR
  • connection cable

I bought this kit from Amazon: https://www.amazon.it/ideaspark-ESP32-CAM-Istantanea-Riconoscimento-Telecamera/dp/B084H5XNGQ/ref=sr_1_11?__mk_it_IT=%C3%85M%C3%85%C5%BD%C3%95%C3%91&dchild=1&keywords=esp32+kit&qid=1614103288&sr=8-11

The kit already contains the USB2.0 serial cable from USB to TTL PL2303TA needed to program the esp32.

ITA

  • ESP32 cam
  • PIR
  • cavo di collegamento

Io ho comprato questo kit da Amazon: https://www.amazon.it/ideaspark-ESP32-CAM-Istantanea-Riconoscimento-Telecamera/dp/B084H5XNGQ/ref=sr_1_11?__mk_it_IT=%C3%85M%C3%85%C5%BD%C3%95%C3%91&dchild=1&keywords=esp32+kit&qid=1614103288&sr=8-11

Il kit contiene già il Cavo USB2.0 seriale da USB a TTL PL2303TA necessario per programmare l'esp32.

Apps and services

  • Arduino ide
  • Telegram

Connection schema - Schema di collegamento

Connection between the Esp32Cam and the USB to serial cable - Connessione tra l'Esp32Cam e il cavo da USB a seriale

** Connect GPIO 0 and GND ports with DuPont cable as following to load the code - Collega GPIO 0 e GND per caricare il codice sull'ESP. **

** Remember to disconnect the GPIO 0 and GND ports after loading the code and press the reset button on the Esp to execute the code - Ricorda di disconnettere i pin GPIO 0 e GND dopo aver caricato il codice e premere il tasto reset dell'Esp per eseguire il codice. **

Connection between Esp32Cam and PIR - Connessione tra l'Esp32Cam e il sensore di movimento

If ESP32 CAM takes pictures frequently. Or never take a picture. Adjust the rotary buttons on the HC-SR501 Human Infrared Sensor - Se il Pir è troppo sensibile e quindi fa scattare troppe fotografie oppure è troppo poco sensibile, regolalo attraverso i pulsanti rotanti.

About the project

ITA

Questo semplice sistema di sorveglianza domestico ti consente di ricevere una fotografia su telegram non appena viene rilevato un movimento tramite il sensore PIR, di richiedere all’esp32 una foto istantanea, accendere o spegnere il led integrato sull’esp o di armare e disarmare il sensore PIR. Il tutto tramite dei semplici comandi del bot telegram. Per la creazione del bot telegram ci sono molte guide online che spiegano come crearlo partendo dal botFather.

ENG

This simple home surveillance system allows you to receive a photo on telegram when movement is detected via the PIR sensor, to request an instant photo from the esp32, turn on or off the integrated led on the esp or to arm and disarm the PIR sensor.. All through simple commands of the telegram bot. For the creation of the telegram bot there are many online guides that explain how to create it starting from the botFather.

Arduino Ide configuration

Code

https://github.com/albino98/telegram_esp32

https://github.com/albino98/telegram_esp32/blob/main/telegram_esp32.ino

Read more

Schematics

Schematics

Security-Camera with Esp32 Cam, PIR and Telegram Bot (4)

Code

  • Arduino Code

Arduino Code

Arduino

https://github.com/albino98/telegram_esp32/blob/main/telegram_esp32.ino

/*albino98https://github.com/albino98/telegram_esp32.git*/#ifdef ESP32 #include <WiFi.h>#else #include <ESP8266WiFi.h>#endif#include <WiFiClientSecure.h>#include <UniversalTelegramBot.h> // Universal Telegram Bot Library written by Brian Lough: https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot#include <ArduinoJson.h>#include "esp_camera.h"#include "soc/soc.h"#include "soc/rtc_cntl_reg.h"#include "driver/rtc_io.h"// Replace with your network credentialsconst char* ssid = "NetworkName";const char* password = "NetworkPW";const int motionSensor = 13;int armed = 1;#define BOTtoken "xxxxxxx:xxxxxxxxxxxxxxxxxx" // your Bot Token (Get from Botfather)// Use @myidbot to find out the chat ID of an individual or a group#define CHAT_ID "xxxxxxx"//Pin definition for CAMERA_MODEL_AI_THINKER#define PWDN_GPIO_NUM 32#define RESET_GPIO_NUM -1#define XCLK_GPIO_NUM 0#define SIOD_GPIO_NUM 26#define SIOC_GPIO_NUM 27#define Y9_GPIO_NUM 35#define Y8_GPIO_NUM 34#define Y7_GPIO_NUM 39#define Y6_GPIO_NUM 36#define Y5_GPIO_NUM 21#define Y4_GPIO_NUM 19#define Y3_GPIO_NUM 18#define Y2_GPIO_NUM 5#define VSYNC_GPIO_NUM 25#define HREF_GPIO_NUM 23#define PCLK_GPIO_NUM 22WiFiClientSecure client;UniversalTelegramBot bot(BOTtoken, client);// Checks for new messages every 1 second.int botRequestDelay = 1000;unsigned long lastTimeBotRan;const int ledPin = 4;bool ledState = LOW;camera_fb_t * fb;uint8_t* fb_buffer;size_t fb_length;int currentByte;boolean startTimer = false;// Handle what happens when you receive new messagesvoid handleNewMessages(int numNewMessages) { Serial.println("handleNewMessages"); Serial.println(String(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 != CHAT_ID){ bot.sendMessage(chat_id, "Unauthorized user", ""); continue; }  // Print the received message String text = bot.messages[i].text; Serial.println(text); String from_name = bot.messages[i].from_name; if (text == "/start") { String welcome = "Welcome, " + from_name + ".\n"; welcome += "Use the following commands to control your outputs.\n\n"; welcome += "/take_photo to take a picture \n"; welcome += "/arm to arm the security system \n"; welcome += "/disarm to disarm the security system \n"; welcome += "/led_on to turn GPIO ON \n"; welcome += "/led_off to turn GPIO OFF \n"; welcome += "/state to request current GPIO state and security system state \n"; bot.sendMessage(chat_id, welcome, ""); } if (text == "/led_on") { bot.sendMessage(chat_id, "LED state set to ON", ""); ledState = HIGH; digitalWrite(ledPin, ledState); }  if (text == "/led_off") { bot.sendMessage(chat_id, "LED state set to OFF", ""); ledState = LOW; digitalWrite(ledPin, ledState); } if (text == "/take_photo") { take_send_photo(CHAT_ID); } if (text == "/security_on") { armed = 1; bot.sendMessage(chat_id, "Security System is ON", ""); } if (text == "/security_off") { armed = 0; bot.sendMessage(chat_id, "Security System is OFF", ""); }  if (text == "/state") { if (digitalRead(ledPin)){ bot.sendMessage(chat_id, "LED is ON", ""); } else{ bot.sendMessage(chat_id, "LED is OFF", ""); } if(armed == 1){ bot.sendMessage(chat_id, "The security system is ON", ""); } else { bot.sendMessage(chat_id, "The security system is OFF", "");  } } }}bool isMoreDataAvailable() { return (fb_length - currentByte);}uint8_t photoNextByte() { currentByte++; return (fb_buffer[currentByte - 1]);}void take_send_photo(String chat_id){ camera_fb_t * fb = NULL; fb = esp_camera_fb_get(); currentByte = 0; fb_length = fb->len; fb_buffer = fb->buf; bot.sendPhotoByBinary(chat_id, "image/jpeg", fb->len, isMoreDataAvailable, photoNextByte, nullptr, nullptr); esp_camera_fb_return(fb); fb_length = NULL; fb_buffer = NULL;}void setup() { armed = 1; WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG,0);//disable brownout detector Serial.begin(115200); camera_config_t configg; configg.ledc_channel = LEDC_CHANNEL_0; configg.ledc_timer = LEDC_TIMER_0; configg.pin_d0 = Y2_GPIO_NUM; configg.pin_d1 = Y3_GPIO_NUM; configg.pin_d2 = Y4_GPIO_NUM; configg.pin_d3 = Y5_GPIO_NUM; configg.pin_d4 = Y6_GPIO_NUM; configg.pin_d5 = Y7_GPIO_NUM; configg.pin_d6 = Y8_GPIO_NUM; configg.pin_d7 = Y9_GPIO_NUM; configg.pin_xclk = XCLK_GPIO_NUM; configg.pin_pclk = PCLK_GPIO_NUM; configg.pin_vsync = VSYNC_GPIO_NUM; configg.pin_href = HREF_GPIO_NUM; configg.pin_sscb_sda = SIOD_GPIO_NUM; configg.pin_sscb_scl = SIOC_GPIO_NUM; configg.pin_pwdn = PWDN_GPIO_NUM; configg.pin_reset = RESET_GPIO_NUM; configg.xclk_freq_hz = 20000000; configg.pixel_format = PIXFORMAT_JPEG; pinMode(ledPin, OUTPUT); digitalWrite(ledPin, ledState); rtc_gpio_hold_dis(GPIO_NUM_4); pinMode(GPIO_NUM_13, INPUT_PULLUP); if(psramFound()){ configg.frame_size = FRAMESIZE_UXGA; configg.jpeg_quality = 10; configg.fb_count = 2; }else{ configg.frame_size = FRAMESIZE_SVGA; configg.jpeg_quality = 12; configg.fb_count = 1; } //Init Camera esp_err_t err = esp_camera_init(&configg); if(err != ESP_OK){ Serial.printf("Camera init failed with error");  return; }  // Connect to Wi-Fi WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi.."); } // Print ESP32 Local IP Address Serial.println(WiFi.localIP());}void loop() { if(armed == 1){ int isDetected = digitalRead(13); if(isDetected == 1){ Serial.println("Presence detected"); take_send_photo(CHAT_ID); delay(3000); } } //delay(1000);  //if (millis() > lastTimeBotRan + botRequestDelay) { int numNewMessages = bot.getUpdates(bot.last_message_received + 1); while(numNewMessages) { Serial.println("got response"); handleNewMessages(numNewMessages); numNewMessages = bot.getUpdates(bot.last_message_received + 1); } delay(1000); lastTimeBotRan = millis(); // }}

Credits

Albino Cianciotti

2 projects • 0 followers

Comments

Security-Camera with Esp32 Cam, PIR and Telegram Bot (2024)

References

Top Articles
Latest Posts
Article information

Author: Ray Christiansen

Last Updated:

Views: 6374

Rating: 4.9 / 5 (69 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Ray Christiansen

Birthday: 1998-05-04

Address: Apt. 814 34339 Sauer Islands, Hirtheville, GA 02446-8771

Phone: +337636892828

Job: Lead Hospitality Designer

Hobby: Urban exploration, Tai chi, Lockpicking, Fashion, Gunsmithing, Pottery, Geocaching

Introduction: My name is Ray Christiansen, I am a fair, good, cute, gentle, vast, glamorous, excited person who loves writing and wants to share my knowledge and understanding with you.