Advertisement
RuCat

photo_normal_version

Aug 11th, 2024
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. /*********
  2. Rui Santos
  3. Complete project details at https://RandomNerdTutorials.com/esp32-cam-take-photo-save-microsd-card
  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.  
  25. // define the number of bytes you want to access
  26. #define EEPROM_SIZE 1
  27.  
  28. // Pin definition for CAMERA_MODEL_AI_THINKER
  29. #define PWDN_GPIO_NUM 32
  30. #define RESET_GPIO_NUM -1
  31. #define XCLK_GPIO_NUM 0
  32. #define SIOD_GPIO_NUM 26
  33. #define SIOC_GPIO_NUM 27
  34.  
  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.  
  52. Serial.begin(115200);
  53. //Serial.setDebugOutput(true);
  54. //Serial.println();
  55.  
  56. camera_config_t config;
  57. config.ledc_channel = LEDC_CHANNEL_0;
  58. config.ledc_timer = LEDC_TIMER_0;
  59. config.pin_d0 = Y2_GPIO_NUM;
  60. config.pin_d1 = Y3_GPIO_NUM;
  61. config.pin_d2 = Y4_GPIO_NUM;
  62. config.pin_d3 = Y5_GPIO_NUM;
  63. config.pin_d4 = Y6_GPIO_NUM;
  64. config.pin_d5 = Y7_GPIO_NUM;
  65. config.pin_d6 = Y8_GPIO_NUM;
  66. config.pin_d7 = Y9_GPIO_NUM;
  67. config.pin_xclk = XCLK_GPIO_NUM;
  68. config.pin_pclk = PCLK_GPIO_NUM;
  69. config.pin_vsync = VSYNC_GPIO_NUM;
  70. config.pin_href = HREF_GPIO_NUM;
  71. config.pin_sscb_sda = SIOD_GPIO_NUM;
  72. config.pin_sscb_scl = SIOC_GPIO_NUM;
  73. config.pin_pwdn = PWDN_GPIO_NUM;
  74. config.pin_reset = RESET_GPIO_NUM;
  75. config.xclk_freq_hz = 20000000;
  76. config.pixel_format = PIXFORMAT_JPEG;
  77. sensor_t * s = esp_camera_sensor_get();
  78.  
  79. if(psramFound()){
  80. config.frame_size = FRAMESIZE_UXGA; // FRAMESIZE_ + QVGA|CIF|VGA|SVGA|XGA|SXGA|UXGA
  81. config.jpeg_quality = 10;
  82. config.fb_count = 2;
  83. } else {
  84. config.frame_size = FRAMESIZE_SVGA;
  85. config.jpeg_quality = 12;
  86. config.fb_count = 1;
  87. }
  88.  
  89. // Init Camera
  90. esp_err_t err = esp_camera_init(&config);
  91. if (err != ESP_OK) {
  92. Serial.printf("Camera init failed with error 0x%x", err);
  93. return;
  94. }
  95.  
  96. //Serial.println("Starting SD Card");
  97. if(!SD_MMC.begin()){
  98. Serial.println("SD Card Mount Failed");
  99. return;
  100. }
  101.  
  102. uint8_t cardType = SD_MMC.cardType();
  103. if(cardType == CARD_NONE){
  104. Serial.println("No SD Card attached");
  105. return;
  106. }
  107.  
  108. camera_fb_t * fb = NULL;
  109.  
  110. // Take Picture with Camera
  111. fb = esp_camera_fb_get();
  112. if(!fb) {
  113. Serial.println("Camera capture failed");
  114. return;
  115. }
  116. // initialize EEPROM with predefined size
  117. EEPROM.begin(EEPROM_SIZE);
  118. pictureNumber = EEPROM.read(0) + 1;
  119.  
  120. // Path where new picture will be saved in SD Card
  121. String path = "/picture" + String(pictureNumber) +".jpg";
  122.  
  123. fs::FS &fs = SD_MMC;
  124. Serial.printf("Picture file name: %s\n", path.c_str());
  125.  
  126. File file = fs.open(path.c_str(), FILE_WRITE);
  127. if(!file){
  128. Serial.println("Failed to open file in writing mode");
  129. }
  130. else {
  131. file.write(fb->buf, fb->len); // payload (image), payload length
  132. Serial.printf("Saved file to path: %s\n", path.c_str());
  133. EEPROM.write(0, pictureNumber);
  134. EEPROM.commit();
  135. }
  136. file.close();
  137. esp_camera_fb_return(fb);
  138.  
  139. // Turns off the ESP32-CAM white on-board LED (flash) connected to GPIO 4
  140. pinMode(4, OUTPUT);
  141. digitalWrite(4, LOW);
  142. rtc_gpio_hold_en(GPIO_NUM_4);
  143.  
  144. delay(2000);
  145. Serial.println("Going to sleep now");
  146. delay(2000);
  147. esp_deep_sleep_start();
  148. Serial.println("This will never be printed");
  149. }
  150.  
  151. void loop() {
  152.  
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement