Motion Alert With ESP32 CAM (Text SMS + PHOTO)
Smart Security System Using ESP32 CAM Module
Introduction
A motion alert system is a valuable addition to any security setup, providing real-time notifications when motion is detected. In this article, we will explore how to build a motion alert system using an ultrasonic sensor and ESP32 Cam module. With this setup, you can receive instant alerts and photos on your phone whenever motion is detected.
Component Need
S.N | Component Name | Quantity | Link To Buy |
1 | ESP32 CAM | 1 | |
2 | Ultersonic Sensor | 1 | |
3 | SIM800l GSM Module | 1 | |
4 | TP4056 Module | 1 | |
5 | Zero PCB | 1 | |
6 | 3.7v Battery | 1 |
ESP32 CAM Module
The ESP32-CAM module is an on-a-chip camera module And is a 32-bit CPU. It also provides Wi-Fi connectivity. the ESp32 Cam is a cam-based Microcontroller if you use this microcontroller then you can do various projects related to OpenCV and AI-Based.
Features:
- The Clock Speed is 160MHz
- Is Low Power 32-bit CPU
- It also Supports UART/SPI/I2C/PWM/ADC/DAC
- Built Camer OV2640 with flash lamp
- Is Support STA/AP/Sta+AP Operation Mode
- Is Support Free RTOS
- Is Support TF Card
Specifications:
- Power Required: DC 5V
- Temperature Range: -20 to 85
- External Storage: micro SD card up to 4GB
- The wireless mode: wifi 802.11 b/g/n And Bluetooth 4.2 LE Module
GSM SIM800l Module
The SIM800L module is a compact size module that communicates over cellular networks. It also supports the GPRS Function. it is easy to interface with any microcontroller UART by sending and receiving SMS messages and making and receiving phone calls.
Module Can Do That
- Send Text Messages(SMS)
- Make or Receive Phone Calls
- Connect to the Internet Via GPRS
- TCP/IP Protocol
Features
- The Module Work Only 3.7v To 4.2 Volts
- Current Required Upto 2 ampere
- UART Communication Through
- Send And receive GPRS Data
- SIM Slides Available on the Backside
Ultrasonic Sensor
An ultrasonic sensor finds out the distance in a very short range of 2CM to 4M with very high accuracy.is easy to interface with any microcontroller like ESP32 CAM,ESP8266, ESP32,And Arduino.is to find out the distance, motion and objects.
The sensor is available in four pins VCC, ECHO, TRIGGER, And GND.
Features
- Output Type: Digital
- Operating Voltage: 5V
- current required: 2mA
- Distance: 2cm-400cm
TP4056 Module
TP4056 module is a popular charging module used to charge single-cell lithium-ion batteries. in this module, the charging IC Used TP4056 because it provides constant and linear voltage for charging Single cell batteries.
- The Module includes in battery connector and indicator it is easy to connect one lithium-ion battery cell and is also short-circuit protection.
ZERO PCB
- The Zero PCB Is used To make Any project for demo Purposes we just put all the components in the Zero PCB And in the back side soldering it and with the help of a small wire you just connect to each other depending on the circuit diagram of the Project
- Here we used the 4×6 size of Zero PCB in the market so many sizes available.
Circuit Diagram
- The VCC and GND pins of the ESP32-CAM, ultrasonic sensor are connectedto the 5v.
- The GND pins of the ESP32-CAM, ultrasonic sensor, and SIM800L module together.
- The TRIGGER_PIN is connected to the GPIO 14 Pin and the ECHO_PIN is connected to the GPIO 15 Pin Number on the ESP32-CAM.
- The RX pin of the SIM800L module connects the TX pin (GPIO 10) to the ESP32-CAM.
- The TX pin of the SIM800L module connects the RX pin (GPIO 11) to the ESP32-CAM.
PCB Design
Source Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
#include "esp_camera.h" #include "SPI.h" #include "driver/rtc_io.h" #include <ESP_Mail_Client.h> #include <FS.h> #include <WiFi.h> const char* ssid = "justdoelectronics"; const char* password = "123456789"; #define emailSenderAccount "SENDER_EMAIL@gmail.com" #define emailSenderPassword "YOUR_EMAIL_APP_PASSWORD" #define smtpServer "smtp.gmail.com" #define smtpServerPort 465 #define emailSubject "Photo Captured" #define emailRecipient "YOUR_EMAIL_RECIPIENT@example.com" #define CAMERA_MODEL_AI_THINKER #if defined(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 22 #else #error "Camera model not selected" #endif SMTPSession smtp; void smtpCallback(SMTP_Status status); #define FILE_PHOTO "photo.jpg" #define FILE_PHOTO_PATH "/photo.jpg" void setup() { WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); Serial.begin(115200); Serial.println(); WiFi.begin(ssid, password); Serial.print("Connecting to WiFi..."); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(); Serial.print("IP Address: http://"); Serial.println(WiFi.localIP()); ESP_MAIL_DEFAULT_FLASH_FS.begin(); camera_config_t config; config.ledc_channel = LEDC_CHANNEL_0; config.ledc_timer = LEDC_TIMER_0; config.pin_d0 = Y2_GPIO_NUM; config.pin_d1 = Y3_GPIO_NUM; config.pin_d2 = Y4_GPIO_NUM; config.pin_d3 = Y5_GPIO_NUM; config.pin_d4 = Y6_GPIO_NUM; config.pin_d5 = Y7_GPIO_NUM; config.pin_d6 = Y8_GPIO_NUM; config.pin_d7 = Y9_GPIO_NUM; config.pin_xclk = XCLK_GPIO_NUM; config.pin_pclk = PCLK_GPIO_NUM; config.pin_vsync = VSYNC_GPIO_NUM; config.pin_href = HREF_GPIO_NUM; config.pin_sccb_sda = SIOD_GPIO_NUM; config.pin_sccb_scl = SIOC_GPIO_NUM; config.pin_pwdn = PWDN_GPIO_NUM; config.pin_reset = RESET_GPIO_NUM; config.xclk_freq_hz = 20000000; config.pixel_format = PIXFORMAT_JPEG; config.grab_mode = CAMERA_GRAB_LATEST; if (psramFound()) { config.frame_size = FRAMESIZE_UXGA; config.jpeg_quality = 10; config.fb_count = 1; } else { config.frame_size = FRAMESIZE_SVGA; config.jpeg_quality = 12; config.fb_count = 1; } esp_err_t err = esp_camera_init(&config); if (err != ESP_OK) { Serial.printf("Camera init failed with error 0x%x", err); return; } capturePhotoSaveLittleFS(); sendPhoto(); } void loop() { } void capturePhotoSaveLittleFS(void) { camera_fb_t* fb = NULL; for (int i = 0; i < 3; i++) { fb = esp_camera_fb_get(); esp_camera_fb_return(fb); fb = NULL; } fb = NULL; fb = esp_camera_fb_get(); if (!fb) { Serial.println("Camera capture failed"); delay(1000); ESP.restart(); } Serial.printf("Picture file name: %s\n", FILE_PHOTO_PATH); File file = LittleFS.open(FILE_PHOTO_PATH, FILE_WRITE); if (!file) { Serial.println("Failed to open file in writing mode"); } else { file.write(fb->buf, fb->len); Serial.print("The picture has been saved in "); Serial.print(FILE_PHOTO_PATH); Serial.print(" - Size: "); Serial.print(fb->len); Serial.println(" bytes"); } file.close(); esp_camera_fb_return(fb); } void sendPhoto(void) { smtp.debug(1); smtp.callback(smtpCallback); Session_Config config; config.time.ntp_server = F("pool.ntp.org,time.nist.gov"); config.time.gmt_offset = 0; config.time.day_light_offset = 1; config.server.host_name = smtpServer; config.server.port = smtpServerPort; config.login.email = emailSenderAccount; config.login.password = emailSenderPassword; config.login.user_domain = ""; SMTP_Message message; message.enable.chunking = true; message.sender.name = "ESP32-CAM"; message.sender.email = emailSenderAccount; message.subject = emailSubject; message.addRecipient("Sara", emailRecipient); String htmlMsg = "<h2>Photo captured with ESP32-CAM .</h2>"; message.html.content = htmlMsg.c_str(); message.html.charSet = "utf-8"; message.html.transfer_encoding = Content_Transfer_Encoding::enc_qp; message.priority = esp_mail_smtp_priority::esp_mail_smtp_priority_normal; message.response.notify = esp_mail_smtp_notify_success | esp_mail_smtp_notify_failure | esp_mail_smtp_notify_delay; SMTP_Attachment att; att.descr.filename = FILE_PHOTO; att.descr.mime = "image/png"; att.file.path = FILE_PHOTO_PATH; att.file.storage_type = esp_mail_file_storage_type_flash; att.descr.transfer_encoding = Content_Transfer_Encoding::enc_base64; message.addAttachment(att); if (!smtp.connect(&config)) return; if (!MailClient.sendMail(&smtp, &message, true)) Serial.println("Error sending Email, " + smtp.errorReason()); } void smtpCallback(SMTP_Status status) { Serial.println(status.info()); if (status.success()) { Serial.println("----------------"); Serial.printf("Message sent success: %d\n", status.completedCount()); Serial.printf("Message sent failled: %d\n", status.failedCount()); Serial.println("----------------\n"); struct tm dt; for (size_t i = 0; i < smtp.sendingResult.size(); i++) { SMTP_Result result = smtp.sendingResult.getItem(i); time_t ts = (time_t)result.timestamp; localtime_r(&ts, &dt); ESP_MAIL_PRINTF("Message No: %d\n", i + 1); ESP_MAIL_PRINTF("Status: %s\n", result.completed ? "success" : "failed"); ESP_MAIL_PRINTF("Date/Time: %d/%d/%d %d:%d:%d\n", dt.tm_year + 1900, dt.tm_mon + 1, dt.tm_mday, dt.tm_hour, dt.tm_min, dt.tm_sec); ESP_MAIL_PRINTF("Recipient: %s\n", result.recipients.c_str()); ESP_MAIL_PRINTF("Subject: %s\n", result.subject.c_str()); } Serial.println("----------------\n"); smtp.sendingResult.clear(); } } |
Video Tutorial
Conclusion
This project provides we try to make an advanced security system with the help of the ESP32 CAM Module is sends the text message and retail time images of which type of motion is detected. With a motion alert system in place, it sends instant messages to a particular mobile number. the projects will be used in various fields.
If You Interested in More ESP32 Projects
Nice project, I like it,& expect long period..
But..
While on compiling this code , error that
RTC_CNTL_BROWN_OUT_REG.h issue cause…
How eliminate this error..
Thanks … Soon reply