//Apr 2,2023 Added time from internet to trigger pic capture and send email //Updates time from internet once a minute at 15 sec counter //Uses millis() 1 second counter for seconds. Rest of time from internet //Seconds counter synched to internet seconds at counter==15 in NTP time setup 3 //Note ******NTP time setup 4 and 5 of 6 not needed******* //First few pictures after reboot are dark until auto exposure corrects #include "esp_camera.h" #include "SPI.h" #include "driver/rtc_io.h" #include "ESP32_MailClient.h" #include #include #include int previousMillis = 0; // millis() and counter used for seconds not from internet int currentMillis, counter, picbit; // 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 "##########@gmail.com" #define sender_email_password "##########" // app password from 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" //***********NTP time intialize 1 of 6******************************************************** #include "time.h" const char* Timezone = "EST5EDT,M3.2.0,M11.1.0"; // EST USA corrects for DST String Date_str, Time_str, Time_format, Year_str, Month_str, Day_str, Hour_str, Min_str, Sec_str; int Year_integer, Month_integer, Day_integer, Hour_integer, Min_integer, Sec_integer, dow; //********** end NTP time intialize*********************************************************** void setup() { Serial.begin(115200); Serial.println(); WiFi.begin("WiFi name", "WiFi password"); Serial.print("Connecting to WiFi..."); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(); // WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector 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(); delay(2000); // 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; } //********** NTP time setup 2 of 6*********************************************** configTime(0, 0, "pool.ntp.org", "time.nist.gov"); setenv("TZ", Timezone, 1); Time_format = "I"; // or StartTime("I"); //***********end NTP time setup************************************************** } void loop() { currentMillis=millis(); if (currentMillis - previousMillis >= 1000){// execute the following once every second previousMillis = currentMillis; counter = counter +1; //seconds counter based on millis() not internet seconds //********** NTP time setup 3 of 6*********************************************** if (counter == 15) //Update time from internet once per minute at 15 secs on counter { UpdateLocalTime(Time_format);// update time from internet counter = Sec_integer; // synch counter } //**********end NTP time setup 3 of 6************************************************ Serial.println("Internet Date and Time "); Serial.println(Year_integer); //year Serial.println(Month_integer);//month Serial.println(Day_integer);//day Serial.println(Hour_integer);//hour Serial.println(Min_integer);//minute Serial.println(Sec_integer);//second Serial.println(" ");//day of week? Serial.println(counter);//1 sec counter if (counter == 45) picbit = 1; // set bit to capture and send pic only once if (counter == 60) counter = 0; // reset to zero at 60 seconds. synched from internet at 15 seconds if ((picbit == 1)&&(counter > 5)&&(counter < 10)&&(Hour_integer > 6)&&(Hour_integer < 20)) { // capture and send pic once per minute between 7 and 8 pm. picbit = 0; captureSave_image(); sendImage(); // bool formatted = SPIFFS.format(); // delay(1000); SPIFFS.remove(IMAGE_PATH); } } } // 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-1 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()); } //******NTP time setup 6 of 6 *********************************************************************** void UpdateLocalTime(String Format){ time_t now; time(&now); //See http://www.cplusplus.com/reference/ctime/strftime/ char hour_output[30], day_output[30]; strftime(day_output, 30, "%a %m-%d-%y", localtime(&now)); // Formats date as: Sat Jun-24-17 strftime(hour_output, 30, "%T", localtime(&now)); // Formats time as: 2:05:49pm Date_str = day_output; Time_str = hour_output; //*** get individual date and time numbers strftime(day_output, 30, "%Y", localtime(&now)); Year_str = day_output; Year_integer = Year_str.toInt(); strftime(day_output, 30, "%m", localtime(&now)); Month_str = day_output; Month_integer = Month_str.toInt(); strftime(day_output, 30, "%e", localtime(&now)); Day_str = day_output; Day_integer = Day_str.toInt(); strftime(hour_output, 30, "%H", localtime(&now)); Hour_str = hour_output; Hour_integer = Hour_str.toInt(); strftime(hour_output, 30, "%M", localtime(&now)); Min_str = hour_output; Min_integer = Min_str.toInt(); strftime(hour_output, 30, "%S", localtime(&now)); Sec_str = hour_output; Sec_integer = Sec_str.toInt(); } //*****end of NTP time setup 6 of 6**************************************************