take_picture.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * This example takes a picture every 5s and print its size on serial monitor.
  3. */
  4. // =============================== SETUP ======================================
  5. // 1. Board setup (Uncomment):
  6. // #define BOARD_WROVER_KIT
  7. // #define BOARD_ESP32CAM_AITHINKER
  8. /**
  9. * 2. Kconfig setup
  10. *
  11. * If you have a Kconfig file, copy the content from
  12. * https://github.com/espressif/esp32-camera/blob/master/Kconfig into it.
  13. * In case you haven't, copy and paste this Kconfig file inside the src directory.
  14. * This Kconfig file has definitions that allows more control over the camera and
  15. * how it will be initialized.
  16. */
  17. /**
  18. * 3. Enable PSRAM on sdkconfig:
  19. *
  20. * CONFIG_ESP32_SPIRAM_SUPPORT=y
  21. *
  22. * More info on
  23. * https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/kconfig.html#config-esp32-spiram-support
  24. */
  25. // ================================ CODE ======================================
  26. #include <esp_log.h>
  27. #include <esp_system.h>
  28. #include <nvs_flash.h>
  29. #include <sys/param.h>
  30. #include <string.h>
  31. #include "freertos/FreeRTOS.h"
  32. #include "freertos/task.h"
  33. // support IDF 5.x
  34. #ifndef portTICK_RATE_MS
  35. #define portTICK_RATE_MS portTICK_PERIOD_MS
  36. #endif
  37. #include "esp_camera.h"
  38. #define BOARD_WROVER_KIT 1
  39. // WROVER-KIT PIN Map
  40. #ifdef BOARD_WROVER_KIT
  41. #define CAM_PIN_PWDN -1 //power down is not used
  42. #define CAM_PIN_RESET -1 //software reset will be performed
  43. #define CAM_PIN_XCLK 21
  44. #define CAM_PIN_SIOD 26
  45. #define CAM_PIN_SIOC 27
  46. #define CAM_PIN_D7 35
  47. #define CAM_PIN_D6 34
  48. #define CAM_PIN_D5 39
  49. #define CAM_PIN_D4 36
  50. #define CAM_PIN_D3 19
  51. #define CAM_PIN_D2 18
  52. #define CAM_PIN_D1 5
  53. #define CAM_PIN_D0 4
  54. #define CAM_PIN_VSYNC 25
  55. #define CAM_PIN_HREF 23
  56. #define CAM_PIN_PCLK 22
  57. #endif
  58. // ESP32Cam (AiThinker) PIN Map
  59. #ifdef BOARD_ESP32CAM_AITHINKER
  60. #define CAM_PIN_PWDN 32
  61. #define CAM_PIN_RESET -1 //software reset will be performed
  62. #define CAM_PIN_XCLK 0
  63. #define CAM_PIN_SIOD 26
  64. #define CAM_PIN_SIOC 27
  65. #define CAM_PIN_D7 35
  66. #define CAM_PIN_D6 34
  67. #define CAM_PIN_D5 39
  68. #define CAM_PIN_D4 36
  69. #define CAM_PIN_D3 21
  70. #define CAM_PIN_D2 19
  71. #define CAM_PIN_D1 18
  72. #define CAM_PIN_D0 5
  73. #define CAM_PIN_VSYNC 25
  74. #define CAM_PIN_HREF 23
  75. #define CAM_PIN_PCLK 22
  76. #endif
  77. static const char *TAG = "example:take_picture";
  78. static camera_config_t camera_config = {
  79. .pin_pwdn = CAM_PIN_PWDN,
  80. .pin_reset = CAM_PIN_RESET,
  81. .pin_xclk = CAM_PIN_XCLK,
  82. .pin_sccb_sda = CAM_PIN_SIOD,
  83. .pin_sccb_scl = CAM_PIN_SIOC,
  84. .pin_d7 = CAM_PIN_D7,
  85. .pin_d6 = CAM_PIN_D6,
  86. .pin_d5 = CAM_PIN_D5,
  87. .pin_d4 = CAM_PIN_D4,
  88. .pin_d3 = CAM_PIN_D3,
  89. .pin_d2 = CAM_PIN_D2,
  90. .pin_d1 = CAM_PIN_D1,
  91. .pin_d0 = CAM_PIN_D0,
  92. .pin_vsync = CAM_PIN_VSYNC,
  93. .pin_href = CAM_PIN_HREF,
  94. .pin_pclk = CAM_PIN_PCLK,
  95. //XCLK 20MHz or 10MHz for OV2640 double FPS (Experimental)
  96. .xclk_freq_hz = 20000000,
  97. .ledc_timer = LEDC_TIMER_0,
  98. .ledc_channel = LEDC_CHANNEL_0,
  99. .pixel_format = PIXFORMAT_RGB565, //YUV422,GRAYSCALE,RGB565,JPEG
  100. .frame_size = FRAMESIZE_QVGA, //QQVGA-UXGA Do not use sizes above QVGA when not JPEG
  101. .jpeg_quality = 12, //0-63 lower number means higher quality
  102. .fb_count = 1, //if more than one, i2s runs in continuous mode. Use only with JPEG
  103. .grab_mode = CAMERA_GRAB_WHEN_EMPTY,
  104. };
  105. static esp_err_t init_camera()
  106. {
  107. //initialize the camera
  108. esp_err_t err = esp_camera_init(&camera_config);
  109. if (err != ESP_OK)
  110. {
  111. ESP_LOGE(TAG, "Camera Init Failed");
  112. return err;
  113. }
  114. return ESP_OK;
  115. }
  116. void app_main()
  117. {
  118. if(ESP_OK != init_camera()) {
  119. return;
  120. }
  121. while (1)
  122. {
  123. ESP_LOGI(TAG, "Taking picture...");
  124. camera_fb_t *pic = esp_camera_fb_get();
  125. // use pic->buf to access the image
  126. ESP_LOGI(TAG, "Picture taken! Its size was: %zu bytes", pic->len);
  127. esp_camera_fb_return(pic);
  128. vTaskDelay(5000 / portTICK_RATE_MS);
  129. }
  130. }