Advertisement
RuCat

ESP32CAMtest

Aug 26th, 2024
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.14 KB | Source Code | 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.  
  11. // define the number of bytes you want to access
  12. #define EEPROM_SIZE 1
  13.  
  14. // Pin definition for CAMERA_MODEL_AI_THINKER
  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.  
  21. #define Y9_GPIO_NUM       35
  22. #define Y8_GPIO_NUM       34
  23. #define Y7_GPIO_NUM       39
  24. #define Y6_GPIO_NUM       36
  25. #define Y5_GPIO_NUM       21
  26. #define Y4_GPIO_NUM       19
  27. #define Y3_GPIO_NUM       18
  28. #define Y2_GPIO_NUM        5
  29. #define VSYNC_GPIO_NUM    25
  30. #define HREF_GPIO_NUM     23
  31. #define PCLK_GPIO_NUM     22
  32. int pictureNumber = 0;
  33. int data;
  34. int enddata;
  35.  
  36. void setup() {
  37.   WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector;
  38.   Serial.begin(9600);
  39.   //Serial.setDebugOutput(true);
  40.   //Serial.println();
  41.  
  42.   camera_config_t config;
  43.   config.ledc_channel = LEDC_CHANNEL_0;
  44.   config.ledc_timer = LEDC_TIMER_0;
  45.   config.pin_d0 = Y2_GPIO_NUM;
  46.   config.pin_d1 = Y3_GPIO_NUM;
  47.   config.pin_d2 = Y4_GPIO_NUM;
  48.   config.pin_d3 = Y5_GPIO_NUM;
  49.   config.pin_d4 = Y6_GPIO_NUM;
  50.   config.pin_d5 = Y7_GPIO_NUM;
  51.   config.pin_d6 = Y8_GPIO_NUM;
  52.   config.pin_d7 = Y9_GPIO_NUM;
  53.   config.pin_xclk = XCLK_GPIO_NUM;
  54.   config.pin_pclk = PCLK_GPIO_NUM;
  55.   config.pin_vsync = VSYNC_GPIO_NUM;
  56.   config.pin_href = HREF_GPIO_NUM;
  57.   config.pin_sscb_sda = SIOD_GPIO_NUM;
  58.   config.pin_sscb_scl = SIOC_GPIO_NUM;
  59.   config.pin_pwdn = PWDN_GPIO_NUM;
  60.   config.pin_reset = RESET_GPIO_NUM;
  61.   config.xclk_freq_hz = 20000000;
  62.   config.pixel_format = PIXFORMAT_JPEG;
  63.   if(psramFound()){
  64.     config.frame_size = FRAMESIZE_VGA; // FRAMESIZE_ + QVGA|CIF|VGA|SVGA|XGA|SXGA|UXGA
  65.     config.jpeg_quality = 10;
  66.     config.fb_count = 2;
  67.   } else {
  68.     config.frame_size = FRAMESIZE_SVGA;
  69.     config.jpeg_quality = 12;
  70.     config.fb_count = 1;
  71.   }
  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.   sensor_t * s = esp_camera_sensor_get();
  81.   s->set_brightness(s, 1);     // -2 to 2
  82.   s->set_whitebal(s, 1);       // 0 = disable , 1 = enable
  83.   s->set_awb_gain(s, 1);       // 0 = disable , 1 = enable
  84.   s->set_wb_mode(s, 0);
  85.   s->set_hmirror(s, 1);        // 0 = disable , 1 = enable
  86.   s->set_vflip(s, 1);
  87.  
  88.  
  89.   pinMode(4, OUTPUT);
  90.   //Serial.println("Starting SD Card");
  91.   if(!SD_MMC.begin()){
  92.     Serial.println("SD Card Mount Failed");
  93.     return;
  94.   }
  95.  
  96.   uint8_t cardType = SD_MMC.cardType();
  97.   if(cardType == CARD_NONE){
  98.     Serial.println("No SD Card attached");
  99.     return;
  100.   }
  101.   esp_sleep_enable_timer_wakeup(10 * 1000000);
  102.    
  103.  
  104. }
  105.  
  106. void loop() {
  107.   if(Serial.available() > 0){
  108.    
  109.   data = Serial.read();
  110.   enddata = data - 48;
  111.   camera_fb_t * fb = NULL;
  112.  
  113.   // Take Picture with Camera
  114.   fb = esp_camera_fb_get();  
  115.   if(!fb) {
  116.     Serial.println("Camera capture failed");
  117.     return;
  118.   }
  119.   // initialize EEPROM with predefined size
  120.   EEPROM.begin(EEPROM_SIZE);
  121.   pictureNumber = EEPROM.read(0) + 1;
  122.   // Path where new picture will be saved in SD Card
  123.   String path = "/picture" + String(enddata) +".jpg";
  124.   fs::FS &fs = SD_MMC;
  125.   Serial.printf("Picture file name: %s\n", path.c_str());
  126.    
  127.   File file = fs.open(path.c_str(), FILE_WRITE);
  128.   if(!file){
  129.     Serial.println("Failed to open file in writing mode");
  130.   }
  131.   else {
  132.     file.write(fb->buf, fb->len); // payload (image), payload length
  133.     Serial.printf("Saved file to path: %s\n", path.c_str());
  134.     EEPROM.write(0, pictureNumber);
  135.     EEPROM.commit();
  136.   }
  137.   file.close();
  138.   esp_camera_fb_return(fb);
  139.      
  140.   // Turns off the ESP32-CAM white on-board LED (flash) connected to GPIO 4
  141.      
  142.   digitalWrite(4, LOW);
  143.   rtc_gpio_hold_en(GPIO_NUM_4);
  144.   delay(2000);
  145.   esp_deep_sleep_start();
  146.    
  147.  
  148.   }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement