ESP32 Cam PIR and Chrono Photos

davidmc36
Posts: 1
Joined: Wed Feb 09, 2022 7:22 am

ESP32 Cam PIR and Chrono Photos

Postby davidmc36 » Wed Feb 09, 2022 7:40 am

Just starting to try some things with ESP32.

I wonder if it is possible to have both of these functions programmed in at the same time.

I want to be able to have motion detection along with periodic snap-shots (hourly).

Would there be danger of crash if it were busy taking a Motion activated photo then the timer also asked for a photo?
  1. /*********
  2.   Rui Santos
  3.   Complete project details at https://RandomNerdTutorials.com/esp32-cam-pir-motion-detector-photo-capture/
  4.  
  5.   IMPORTANT!!!
  6.    - Select Board "AI Thinker ESP32-CAM"
  7.    - GPIO 0 must be connected to GND to upload a sketch
  8.    - After connecting GPIO 0 to GND, press the ESP32-CAM on-board RESET button to put your board in flashing mode
  9.  
  10.   Permission is hereby granted, free of charge, to any person obtaining a copy
  11.   of this software and associated documentation files.
  12.   The above copyright notice and this permission notice shall be included in all
  13.   copies or substantial portions of the Software.
  14. *********/
  15.  
  16. #include "esp_camera.h"
  17. #include "Arduino.h"
  18. #include "FS.h"                // SD Card ESP32
  19. #include "SD_MMC.h"            // SD Card ESP32
  20. #include "soc/soc.h"           // Disable brownour problems
  21. #include "soc/rtc_cntl_reg.h"  // Disable brownour problems
  22. #include "driver/rtc_io.h"
  23. #include <EEPROM.h>            // read and write from flash memory
  24. // define the number of bytes you want to access
  25. #define EEPROM_SIZE 1
  26.  
  27. RTC_DATA_ATTR int bootCount = 0;
  28.  
  29. // Pin definition for CAMERA_MODEL_AI_THINKER
  30. #define PWDN_GPIO_NUM     32
  31. #define RESET_GPIO_NUM    -1
  32. #define XCLK_GPIO_NUM      0
  33. #define SIOD_GPIO_NUM     26
  34. #define SIOC_GPIO_NUM     27
  35. #define Y9_GPIO_NUM       35
  36. #define Y8_GPIO_NUM       34
  37. #define Y7_GPIO_NUM       39
  38. #define Y6_GPIO_NUM       36
  39. #define Y5_GPIO_NUM       21
  40. #define Y4_GPIO_NUM       19
  41. #define Y3_GPIO_NUM       18
  42. #define Y2_GPIO_NUM        5
  43. #define VSYNC_GPIO_NUM    25
  44. #define HREF_GPIO_NUM     23
  45. #define PCLK_GPIO_NUM     22
  46.  
  47. int pictureNumber = 0;
  48.  
  49. void setup() {
  50.   WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector
  51.   Serial.begin(115200);
  52.  
  53.   Serial.setDebugOutput(true);
  54.  
  55.   camera_config_t config;
  56.   config.ledc_channel = LEDC_CHANNEL_0;
  57.   config.ledc_timer = LEDC_TIMER_0;
  58.   config.pin_d0 = Y2_GPIO_NUM;
  59.   config.pin_d1 = Y3_GPIO_NUM;
  60.   config.pin_d2 = Y4_GPIO_NUM;
  61.   config.pin_d3 = Y5_GPIO_NUM;
  62.   config.pin_d4 = Y6_GPIO_NUM;
  63.   config.pin_d5 = Y7_GPIO_NUM;
  64.   config.pin_d6 = Y8_GPIO_NUM;
  65.   config.pin_d7 = Y9_GPIO_NUM;
  66.   config.pin_xclk = XCLK_GPIO_NUM;
  67.   config.pin_pclk = PCLK_GPIO_NUM;
  68.   config.pin_vsync = VSYNC_GPIO_NUM;
  69.   config.pin_href = HREF_GPIO_NUM;
  70.   config.pin_sscb_sda = SIOD_GPIO_NUM;
  71.   config.pin_sscb_scl = SIOC_GPIO_NUM;
  72.   config.pin_pwdn = PWDN_GPIO_NUM;
  73.   config.pin_reset = RESET_GPIO_NUM;
  74.   config.xclk_freq_hz = 20000000;
  75.   config.pixel_format = PIXFORMAT_JPEG;
  76.  
  77.   pinMode(4, INPUT);
  78.   digitalWrite(4, LOW);
  79.   rtc_gpio_hold_dis(GPIO_NUM_4);
  80.  
  81.   if(psramFound()){
  82.     config.frame_size = FRAMESIZE_UXGA; // FRAMESIZE_ + QVGA|CIF|VGA|SVGA|XGA|SXGA|UXGA
  83.     config.jpeg_quality = 10;
  84.     config.fb_count = 2;
  85.   } else {
  86.     config.frame_size = FRAMESIZE_SVGA;
  87.     config.jpeg_quality = 12;
  88.     config.fb_count = 1;
  89.   }
  90.  
  91.   // Init Camera
  92.   esp_err_t err = esp_camera_init(&config);
  93.   if (err != ESP_OK) {
  94.     Serial.printf("Camera init failed with error 0x%x", err);
  95.     return;
  96.   }
  97.  
  98.   Serial.println("Starting SD Card");
  99.  
  100.   delay(500);
  101.   if(!SD_MMC.begin()){
  102.     Serial.println("SD Card Mount Failed");
  103.     //return;
  104.   }
  105.  
  106.   uint8_t cardType = SD_MMC.cardType();
  107.   if(cardType == CARD_NONE){
  108.     Serial.println("No SD Card attached");
  109.     return;
  110.   }
  111.    
  112.   camera_fb_t * fb = NULL;
  113.  
  114.   // Take Picture with Camera
  115.   fb = esp_camera_fb_get();  
  116.   if(!fb) {
  117.     Serial.println("Camera capture failed");
  118.     return;
  119.   }
  120.   // initialize EEPROM with predefined size
  121.   EEPROM.begin(EEPROM_SIZE);
  122.   pictureNumber = EEPROM.read(0) + 1;
  123.  
  124.   // Path where new picture will be saved in SD Card
  125.   String path = "/picture" + String(pictureNumber) +".jpg";
  126.  
  127.   fs::FS &fs = SD_MMC;
  128.   Serial.printf("Picture file name: %s\n", path.c_str());
  129.  
  130.   File file = fs.open(path.c_str(), FILE_WRITE);
  131.   if(!file){
  132.     Serial.println("Failed to open file in writing mode");
  133.   }
  134.   else {
  135.     file.write(fb->buf, fb->len); // payload (image), payload length
  136.     Serial.printf("Saved file to path: %s\n", path.c_str());
  137.     EEPROM.write(0, pictureNumber);
  138.     EEPROM.commit();
  139.   }
  140.   file.close();
  141.   esp_camera_fb_return(fb);
  142.  
  143.   delay(1000);
  144.  
  145.   // Turns off the ESP32-CAM white on-board LED (flash) connected to GPIO 4
  146.   pinMode(4, OUTPUT);
  147.   digitalWrite(4, LOW);
  148.   rtc_gpio_hold_en(GPIO_NUM_4);
  149.  
  150.   esp_sleep_enable_ext0_wakeup(GPIO_NUM_13, 0);
  151.  
  152.   Serial.println("Going to sleep now");
  153.   delay(1000);
  154.   esp_deep_sleep_start();
  155.   Serial.println("This will never be printed");
  156. }
  157.  
  158. void loop() {
  159.  
  160. }
  1. /*********
  2.   Rui Santos
  3.   Complete project details at https://RandomNerdTutorials.com/esp32-cam-ov2640-camera-settings/
  4. *********/
  5.  
  6. #include "esp_camera.h"
  7. #include "FS.h"                // SD Card ESP32
  8. #include "SD_MMC.h"            // SD Card ESP32
  9. #include "soc/soc.h"           // Disable brownout problems
  10. #include "soc/rtc_cntl_reg.h"  // Disable brownout problems
  11. #include "driver/rtc_io.h"
  12.  
  13. // Pin definition for CAMERA_MODEL_AI_THINKER
  14. // Change pin definition if you're using another ESP32 with camera module
  15. #define PWDN_GPIO_NUM     32
  16. #define RESET_GPIO_NUM    -1
  17. #define XCLK_GPIO_NUM      0
  18. #define SIOD_GPIO_NUM     26
  19. #define SIOC_GPIO_NUM     27
  20. #define Y9_GPIO_NUM       35
  21. #define Y8_GPIO_NUM       34
  22. #define Y7_GPIO_NUM       39
  23. #define Y6_GPIO_NUM       36
  24. #define Y5_GPIO_NUM       21
  25. #define Y4_GPIO_NUM       19
  26. #define Y3_GPIO_NUM       18
  27. #define Y2_GPIO_NUM        5
  28. #define VSYNC_GPIO_NUM    25
  29. #define HREF_GPIO_NUM     23
  30. #define PCLK_GPIO_NUM     22
  31.  
  32. // Keep track of number of pictures
  33. unsigned int pictureNumber = 0;
  34.  
  35. //Stores the camera configuration parameters
  36. camera_config_t config;
  37.  
  38. void setup() {
  39.   WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector
  40.  
  41.   Serial.begin(115200);
  42.  
  43.   //Initialize the camera  
  44.   Serial.print("Initializing the camera module...");
  45.   configInitCamera();
  46.   Serial.println("Ok!");
  47.  
  48.   //Initialize MicroSD
  49.   Serial.print("Initializing the MicroSD card module... ");
  50.   initMicroSDCard();
  51. }
  52.  
  53. void loop() {
  54.   //Path where new picture will be saved in SD Card
  55.   String path = "/picture" + String(pictureNumber) +".jpg";  
  56.   Serial.printf("Picture file name: %s\n", path.c_str());
  57.  
  58.   //Take and Save Photo
  59.   takeSavePhoto(path);
  60.   pictureNumber++;
  61.   delay(10000);
  62. }
  63.  
  64. void configInitCamera(){
  65.   config.ledc_channel = LEDC_CHANNEL_0;
  66.   config.ledc_timer = LEDC_TIMER_0;
  67.   config.pin_d0 = Y2_GPIO_NUM;
  68.   config.pin_d1 = Y3_GPIO_NUM;
  69.   config.pin_d2 = Y4_GPIO_NUM;
  70.   config.pin_d3 = Y5_GPIO_NUM;
  71.   config.pin_d4 = Y6_GPIO_NUM;
  72.   config.pin_d5 = Y7_GPIO_NUM;
  73.   config.pin_d6 = Y8_GPIO_NUM;
  74.   config.pin_d7 = Y9_GPIO_NUM;
  75.   config.pin_xclk = XCLK_GPIO_NUM;
  76.   config.pin_pclk = PCLK_GPIO_NUM;
  77.   config.pin_vsync = VSYNC_GPIO_NUM;
  78.   config.pin_href = HREF_GPIO_NUM;
  79.   config.pin_sscb_sda = SIOD_GPIO_NUM;
  80.   config.pin_sscb_scl = SIOC_GPIO_NUM;
  81.   config.pin_pwdn = PWDN_GPIO_NUM;
  82.   config.pin_reset = RESET_GPIO_NUM;
  83.   config.xclk_freq_hz = 20000000;
  84.   config.pixel_format = PIXFORMAT_JPEG; //YUV422,GRAYSCALE,RGB565,JPEG
  85.  
  86.   // Select lower framesize if the camera doesn't support PSRAM
  87.   if(psramFound()){
  88.     config.frame_size = FRAMESIZE_UXGA; // FRAMESIZE_ + QVGA|CIF|VGA|SVGA|XGA|SXGA|UXGA
  89.     config.jpeg_quality = 10; //10-63 lower number means higher quality
  90.     config.fb_count = 2;
  91.   } else {
  92.     config.frame_size = FRAMESIZE_SVGA;
  93.     config.jpeg_quality = 12;
  94.     config.fb_count = 1;
  95.   }
  96.  
  97.   // Initialize the Camera
  98.   esp_err_t err = esp_camera_init(&config);
  99.   if (err != ESP_OK) {
  100.     Serial.printf("Camera init failed with error 0x%x", err);
  101.     return;
  102.   }
  103.  
  104.   sensor_t * s = esp_camera_sensor_get();
  105.   s->set_brightness(s, 0);     // -2 to 2
  106.   s->set_contrast(s, 0);       // -2 to 2
  107.   s->set_saturation(s, 0);     // -2 to 2
  108.   s->set_special_effect(s, 0); // 0 to 6 (0 - No Effect, 1 - Negative, 2 - Grayscale, 3 - Red Tint, 4 - Green Tint, 5 - Blue Tint, 6 - Sepia)
  109.   s->set_whitebal(s, 1);       // 0 = disable , 1 = enable
  110.   s->set_awb_gain(s, 1);       // 0 = disable , 1 = enable
  111.   s->set_wb_mode(s, 0);        // 0 to 4 - if awb_gain enabled (0 - Auto, 1 - Sunny, 2 - Cloudy, 3 - Office, 4 - Home)
  112.   s->set_exposure_ctrl(s, 1);  // 0 = disable , 1 = enable
  113.   s->set_aec2(s, 0);           // 0 = disable , 1 = enable
  114.   s->set_ae_level(s, 0);       // -2 to 2
  115.   s->set_aec_value(s, 300);    // 0 to 1200
  116.   s->set_gain_ctrl(s, 1);      // 0 = disable , 1 = enable
  117.   s->set_agc_gain(s, 0);       // 0 to 30
  118.   s->set_gainceiling(s, (gainceiling_t)0);  // 0 to 6
  119.   s->set_bpc(s, 0);            // 0 = disable , 1 = enable
  120.   s->set_wpc(s, 1);            // 0 = disable , 1 = enable
  121.   s->set_raw_gma(s, 1);        // 0 = disable , 1 = enable
  122.   s->set_lenc(s, 1);           // 0 = disable , 1 = enable
  123.   s->set_hmirror(s, 0);        // 0 = disable , 1 = enable
  124.   s->set_vflip(s, 0);          // 0 = disable , 1 = enable
  125.   s->set_dcw(s, 1);            // 0 = disable , 1 = enable
  126.   s->set_colorbar(s, 0);       // 0 = disable , 1 = enable
  127. }
  128.  
  129. void initMicroSDCard(){
  130.   // Start Micro SD card
  131.   Serial.println("Starting SD Card");
  132.   if(!SD_MMC.begin()){
  133.     Serial.println("SD Card Mount Failed");
  134.     return;
  135.   }
  136.   uint8_t cardType = SD_MMC.cardType();
  137.   if(cardType == CARD_NONE){
  138.     Serial.println("No SD Card attached");
  139.     return;
  140.   }
  141. }
  142.  
  143. void takeSavePhoto(String path){
  144.   // Take Picture with Camera
  145.   camera_fb_t  * fb = esp_camera_fb_get();
  146.  
  147.  
  148.  
  149.   if(!fb) {
  150.     Serial.println("Camera capture failed");
  151.     return;
  152.   }
  153.  
  154.   // Save picture to microSD card
  155.   fs::FS &fs = SD_MMC;
  156.   File file = fs.open(path.c_str(), FILE_WRITE);
  157.   if(!file){
  158.     Serial.println("Failed to open file in writing mode");
  159.   }
  160.   else {
  161.     file.write(fb->buf, fb->len); // payload (image), payload length
  162.     Serial.printf("Saved file to path: %s\n", path.c_str());
  163.   }
  164.   file.close();
  165.  
  166.   //return the frame buffer back to the driver for reuse
  167.   esp_camera_fb_return(fb);
  168. }

Who is online

Users browsing this forum: No registered users and 80 guests