#include "esp_camera.h" #include "SPI.h" #include "driver/rtc_io.h" #include "ESP32_MailClient.h" #include #include #include const char* ssid = "#####"; const char* password = "#####"; // To send Email using Gmail use port 465 (SSL) and SMTP Server smtp.gmail.com // YOU MUST ENABLE less secure app option https://myaccount.google.com/lesssecureapps?pli=1 #define sender_email "#####" #define sender_email_password "#####"// app password issued by Google #define SMTP_Server "smtp.gmail.com" #define SMTP_Server_Port 465 #define email_subject "ESP32-CAM Photo Captured" #define email_recipient "#####@gmail.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 SMTPData smtpData; #define IMAGE_PATH "/image.jpg" void setup() { WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector 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(); if (!SPIFFS.begin(true)) { Serial.println("An Error has occurred while mounting SPIFFS"); ESP.restart(); } else { delay(500); Serial.println("SPIFFS mounted successfully"); } bool formatted = SPIFFS.format(); Serial.print("IP Address: http://"); Serial.println(WiFi.localIP()); 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_sscb_sda = SIOD_GPIO_NUM; config.pin_sscb_scl = SIOC_GPIO_NUM; config.pin_pwdn = PWDN_GPIO_NUM; config.pin_reset = RESET_GPIO_NUM; config.xclk_freq_hz = 20000000;//5000000 config.pixel_format = PIXFORMAT_JPEG; if(psramFound()){ config.frame_size = FRAMESIZE_UXGA; config.jpeg_quality = 10; config.fb_count = 2; } 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; } captureSave_image(); sendImage(); } void loop() { delay(60000); captureSave_image(); sendImage(); bool formatted = SPIFFS.format(); } // Check if photo capture was successful bool check_photo( fs::FS &fs ) { File f_pic = fs.open( IMAGE_PATH ); unsigned int pic_sz = f_pic.size(); return ( pic_sz > 100 ); } // Capture Photo and Save it to SPIFFS void captureSave_image( void ) { camera_fb_t * fb = NULL; bool ok = 0; do { Serial.println("ESP32-CAM capturing photo..."); fb = esp_camera_fb_get(); delay(500); if (!fb) { Serial.println("Failed"); return; } Serial.printf("Picture file name: %s\n", IMAGE_PATH); File file = SPIFFS.open(IMAGE_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(IMAGE_PATH); Serial.print(" - Size: "); Serial.print(file.size()); Serial.println(" bytes"); } file.close(); esp_camera_fb_return(fb); ok = check_photo(SPIFFS); } while ( !ok ); } void sendImage( void ) { smtpData.setLogin(SMTP_Server, SMTP_Server_Port, sender_email, sender_email_password); smtpData.setSender("ESP32-CAM", sender_email); smtpData.setPriority("High"); smtpData.setSubject(email_subject); smtpData.setMessage("CAM-2 Captured Image.", false);// added 2 to id which cam smtpData.addRecipient(email_recipient); smtpData.addAttachFile(IMAGE_PATH, "image/jpg"); smtpData.setFileStorageType(MailClientStorageType::SPIFFS); smtpData.setSendCallback(sendCallback); if (!MailClient.sendMail(smtpData)) Serial.println("Error sending Email, " + MailClient.smtpErrorReason()); smtpData.empty(); } void sendCallback(SendStatus msg) { Serial.println(msg.info()); }