Advertisement
MrLunk

ESP32-cam-Timelapse.ino

Feb 5th, 2021 (edited)
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 0 0
  1. // See this code in action: https://www.youtube.com/watch?v=CuzkY-w3BIQ
  2. // NOTE: The Ultra bright (Flash) LED on GPIO4 acts like a SSD read-write light.
  3. // Cover it with Black isolation tape to avoid reflections if you are using the camera.
  4.  
  5. #include "FS.h"
  6. #include "SD_MMC.h"
  7. #include "esp_camera.h"
  8. #include <EEPROM.h> // read and write from flash memory
  9.  
  10. // Select camera pin configuration
  11. #define CAMERA_MODEL_AI_THINKER
  12. #include "camera_pins.h"
  13.  
  14. // define number of bytes to set aside in persistant memory
  15. #define EEPROM_SIZE 1
  16. long pictureNumber = 0;
  17.  
  18. static esp_err_t get_image(fs::FS &fs, const char * path) {
  19. // Variable definitions for camera frame buffer
  20. camera_fb_t * fb = NULL;
  21. int64_t fr_start = esp_timer_get_time();
  22.  
  23. // Get the contents of the camera frame buffer
  24. fb = esp_camera_fb_get();
  25. if (!fb) {
  26. Serial.println("Camera capture failed");
  27. return ESP_FAIL;
  28. }
  29.  
  30. // Save image to file
  31. size_t fb_len = 0;
  32. fb_len = fb->len;
  33. File file = fs.open(path, FILE_WRITE);
  34. if(!file){
  35. Serial.println("Failed to open file in writing mode");
  36. } else {
  37. file.write(fb->buf, fb->len); // payload (image), payload length
  38. Serial.printf("Saved file to path: %s\n", path);
  39. }
  40. file.close();
  41.  
  42. // Release the camera frame buffer
  43. esp_camera_fb_return(fb);
  44. int64_t fr_end = esp_timer_get_time();
  45. Serial.printf("JPG: %uB %ums\n", (uint32_t)(fb_len), (uint32_t)((fr_end - fr_start)/1000));
  46. return ESP_OK;
  47. }
  48.  
  49. void setup() {
  50. Serial.begin(115200);
  51. // Serial.setDebugOutput(true);
  52. Serial.println();
  53.  
  54. // Initial camera configuration
  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; // This is very important for saving the frame buffer as a JPeg
  76.  
  77. //init with high specs to pre-allocate larger buffers
  78. if(psramFound()){
  79. config.frame_size = FRAMESIZE_UXGA;
  80. config.jpeg_quality = 10;
  81. config.fb_count = 2;
  82. } else {
  83. config.frame_size = FRAMESIZE_SVGA;
  84. config.jpeg_quality = 12;
  85. config.fb_count = 1;
  86. }
  87.  
  88. // camera init
  89. esp_err_t err = esp_camera_init(&config);
  90. if (err != ESP_OK) {
  91. Serial.printf("Camera init failed with error 0x%x", err);
  92. return;
  93. }
  94.  
  95. /*****
  96. You may need to adjust these settings to get a picture you like.
  97. Look in app_httpd.cpp in the CameraWebServer as an example on how these are used.
  98. sensor_t * s = esp_camera_sensor_get();
  99. int res = 0;
  100. if(!strcmp(variable, "framesize")) {
  101. if(s->pixformat == PIXFORMAT_JPEG) res = s->set_framesize(s, (framesize_t)val);
  102. }
  103. else if(!strcmp(variable, "quality")) res = s->set_quality(s, val);
  104. else if(!strcmp(variable, "contrast")) res = s->set_contrast(s, val);
  105. else if(!strcmp(variable, "brightness")) res = s->set_brightness(s, val);
  106. else if(!strcmp(variable, "saturation")) res = s->set_saturation(s, val);
  107. else if(!strcmp(variable, "gainceiling")) res = s->set_gainceiling(s, (gainceiling_t)val);
  108. else if(!strcmp(variable, "colorbar")) res = s->set_colorbar(s, val);
  109. else if(!strcmp(variable, "awb")) res = s->set_whitebal(s, val);
  110. else if(!strcmp(variable, "agc")) res = s->set_gain_ctrl(s, val);
  111. else if(!strcmp(variable, "aec")) res = s->set_exposure_ctrl(s, val);
  112. else if(!strcmp(variable, "hmirror")) res = s->set_hmirror(s, val);
  113. else if(!strcmp(variable, "vflip")) res = s->set_vflip(s, val);
  114. else if(!strcmp(variable, "awb_gain")) res = s->set_awb_gain(s, val);
  115. else if(!strcmp(variable, "agc_gain")) res = s->set_agc_gain(s, val);
  116. else if(!strcmp(variable, "aec_value")) res = s->set_aec_value(s, val);
  117. else if(!strcmp(variable, "aec2")) res = s->set_aec2(s, val);
  118. else if(!strcmp(variable, "dcw")) res = s->set_dcw(s, val);
  119. else if(!strcmp(variable, "bpc")) res = s->set_bpc(s, val);
  120. else if(!strcmp(variable, "wpc")) res = s->set_wpc(s, val);
  121. else if(!strcmp(variable, "raw_gma")) res = s->set_raw_gma(s, val);
  122. else if(!strcmp(variable, "lenc")) res = s->set_lenc(s, val);
  123. else if(!strcmp(variable, "special_effect")) res = s->set_special_effect(s, val);
  124. else if(!strcmp(variable, "wb_mode")) res = s->set_wb_mode(s, val);
  125. else if(!strcmp(variable, "ae_level")) res = s->set_ae_level(s, val);
  126. *****/
  127.  
  128. Serial.println("Camera initialized!");
  129.  
  130. if(!SD_MMC.begin()){
  131. Serial.println("Card Mount Failed");
  132. return;
  133. }
  134. uint8_t cardType = SD_MMC.cardType();
  135. if(cardType == CARD_NONE){
  136. Serial.println("No SD_MMC card attached");
  137. return;
  138. }
  139. Serial.println("SD Card Initialized");
  140.  
  141. // initialize EEPROM with predefined size
  142. EEPROM.begin(EEPROM_SIZE);
  143. pictureNumber = EEPROM.read(0) + 1;
  144.  
  145. }
  146.  
  147. void loop() {
  148.  
  149. pictureNumber = pictureNumber + 1;
  150. // Call function to capture the image and save it as a file
  151. String path = "/picture" + String(pictureNumber) + ".jpg";
  152. if(get_image(SD_MMC, path.c_str()) != ESP_OK ) {
  153. Serial.println("Failed to capture picture");
  154. }
  155. delay(5000); // Set the desired delay between images
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement