camera.c 4.7 KB

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