Advertisement
RuCat

photo_non-functional_version

Aug 11th, 2024
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. #include "esp_camera.h"
  2. #include "Arduino.h"
  3. #include "FS.h" // SD Card ESP32
  4. #include "SD_MMC.h" // SD Card ESP32
  5. #include "soc/soc.h" // Disable brownour problems
  6. #include "soc/rtc_cntl_reg.h" // Disable brownour problems
  7. #include "driver/rtc_io.h"
  8. #include <EEPROM.h> // read and write from flash memory
  9.  
  10. // define the number of bytes you want to access
  11. #define EEPROM_SIZE 1
  12.  
  13. // Pin definition for CAMERA_MODEL_AI_THINKER
  14. #define PWDN_GPIO_NUM 32
  15. #define RESET_GPIO_NUM -1
  16. #define XCLK_GPIO_NUM 0
  17. #define SIOD_GPIO_NUM 26
  18. #define SIOC_GPIO_NUM 27
  19.  
  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. int pictureNumber = 0;
  33.  
  34. void setup() {
  35. WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector
  36.  
  37. Serial.begin(115200);
  38. //Serial.setDebugOutput(true);
  39. //Serial.println();
  40.  
  41. camera_config_t config;
  42. config.ledc_channel = LEDC_CHANNEL_0;
  43. config.ledc_timer = LEDC_TIMER_0;
  44. config.pin_d0 = Y2_GPIO_NUM;
  45. config.pin_d1 = Y3_GPIO_NUM;
  46. config.pin_d2 = Y4_GPIO_NUM;
  47. config.pin_d3 = Y5_GPIO_NUM;
  48. config.pin_d4 = Y6_GPIO_NUM;
  49. config.pin_d5 = Y7_GPIO_NUM;
  50. config.pin_d6 = Y8_GPIO_NUM;
  51. config.pin_d7 = Y9_GPIO_NUM;
  52. config.pin_xclk = XCLK_GPIO_NUM;
  53. config.pin_pclk = PCLK_GPIO_NUM;
  54. config.pin_vsync = VSYNC_GPIO_NUM;
  55. config.pin_href = HREF_GPIO_NUM;
  56. config.pin_sscb_sda = SIOD_GPIO_NUM;
  57. config.pin_sscb_scl = SIOC_GPIO_NUM;
  58. config.pin_pwdn = PWDN_GPIO_NUM;
  59. config.pin_reset = RESET_GPIO_NUM;
  60. config.xclk_freq_hz = 20000000;
  61. config.pixel_format = PIXFORMAT_JPEG;
  62. sensor_t * s = esp_camera_sensor_get();
  63. s->set_brightness(s, 1); // -2 to 2
  64. if(psramFound()){
  65. config.frame_size = FRAMESIZE_UXGA; // FRAMESIZE_ + QVGA|CIF|VGA|SVGA|XGA|SXGA|UXGA
  66. config.jpeg_quality = 10;
  67. config.fb_count = 2;
  68. } else {
  69. config.frame_size = FRAMESIZE_SVGA;
  70. config.jpeg_quality = 12;
  71. config.fb_count = 1;
  72. }
  73.  
  74. // Init Camera
  75. esp_err_t err = esp_camera_init(&config);
  76. if (err != ESP_OK) {
  77. Serial.printf("Camera init failed with error 0x%x", err);
  78. return;
  79. }
  80.  
  81. //Serial.println("Starting SD Card");
  82. if(!SD_MMC.begin()){
  83. Serial.println("SD Card Mount Failed");
  84. return;
  85. }
  86.  
  87. uint8_t cardType = SD_MMC.cardType();
  88. if(cardType == CARD_NONE){
  89. Serial.println("No SD Card attached");
  90. return;
  91. }
  92.  
  93. camera_fb_t * fb = NULL;
  94.  
  95. // Take Picture with Camera
  96. fb = esp_camera_fb_get();
  97. if(!fb) {
  98. Serial.println("Camera capture failed");
  99. return;
  100. }
  101. // initialize EEPROM with predefined size
  102. EEPROM.begin(EEPROM_SIZE);
  103. pictureNumber = EEPROM.read(0) + 1;
  104.  
  105. // Path where new picture will be saved in SD Card
  106. String path = "/picture" + String(pictureNumber) +".jpg";
  107.  
  108. fs::FS &fs = SD_MMC;
  109. Serial.printf("Picture file name: %s\n", path.c_str());
  110.  
  111. File file = fs.open(path.c_str(), FILE_WRITE);
  112. if(!file){
  113. Serial.println("Failed to open file in writing mode");
  114. }
  115. else {
  116. file.write(fb->buf, fb->len); // payload (image), payload length
  117. Serial.printf("Saved file to path: %s\n", path.c_str());
  118. EEPROM.write(0, pictureNumber);
  119. EEPROM.commit();
  120. }
  121. file.close();
  122. esp_camera_fb_return(fb);
  123.  
  124. // Turns off the ESP32-CAM white on-board LED (flash) connected to GPIO 4
  125. pinMode(4, OUTPUT);
  126. digitalWrite(4, LOW);
  127. rtc_gpio_hold_en(GPIO_NUM_4);
  128.  
  129. delay(2000);
  130. Serial.println("Going to sleep now");
  131. delay(2000);
  132. esp_deep_sleep_start();
  133. Serial.println("This will never be printed");
  134. }
  135.  
  136. void loop() {
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement