欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ESP32-Cam--获取图像定时写入SD卡

程序员文章站 2022-07-12 10:55:43
...
#include <esp_event_loop.h>
#include <esp_log.h>
#include <esp_system.h>
#include <nvs_flash.h>
#include <sys/param.h>

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#include "driver/sdmmc_host.h"
#include "driver/sdmmc_defs.h"
#include "sdmmc_cmd.h"
#include "esp_vfs_fat.h"

#include "esp_camera.h"

static const char *TAG = "example:sd_jpg";

static camera_config_t camera_config = {
    .pin_pwdn = -1,
    .pin_reset = CONFIG_RESET,
    .pin_xclk = CONFIG_XCLK,
    .pin_sscb_sda = CONFIG_SDA,
    .pin_sscb_scl = CONFIG_SCL,

    .pin_d7 = CONFIG_D7,
    .pin_d6 = CONFIG_D6,
    .pin_d5 = CONFIG_D5,
    .pin_d4 = CONFIG_D4,
    .pin_d3 = CONFIG_D3,
    .pin_d2 = CONFIG_D2,
    .pin_d1 = CONFIG_D1,
    .pin_d0 = CONFIG_D0,
    .pin_vsync = CONFIG_VSYNC,
    .pin_href = CONFIG_HREF,
    .pin_pclk = CONFIG_PCLK,

    //XCLK 20MHz or 10MHz
    .xclk_freq_hz = CONFIG_XCLK_FREQ,
    .ledc_timer = LEDC_TIMER_0,
    .ledc_channel = LEDC_CHANNEL_0,

    .pixel_format = PIXFORMAT_JPEG, //YUV422,GRAYSCALE,RGB565,JPEG
    .frame_size = FRAMESIZE_UXGA,   //QQVGA-UXGA Do not use sizes above QVGA when not JPEG

    .jpeg_quality = 12, //0-63 lower number means higher quality
    .fb_count = 1       //if more than one, i2s runs in continuous mode. Use only with JPEG
};

static esp_err_t init_camera()
{
  //initialize the camera
  esp_err_t err = esp_camera_init(&camera_config);
  if (err != ESP_OK)
  {
    ESP_LOGE(TAG, "Camera Init Failed");
    return err;
  }

  return ESP_OK;
}

static void init_sdcard()
{
  esp_err_t ret = ESP_FAIL;
  sdmmc_host_t host = SDMMC_HOST_DEFAULT();
  sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
  esp_vfs_fat_sdmmc_mount_config_t mount_config = {
      .format_if_mount_failed = false,
      .max_files = 3,
  };
  sdmmc_card_t *card;

  ESP_LOGI(TAG, "Mounting SD card...");
  ret = esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card);

  if (ret == ESP_OK)
  {
    ESP_LOGI(TAG, "SD card mount successfully!");
  }
  else
  {
    ESP_LOGE(TAG, "Failed to mount SD card VFAT filesystem. Error: %s", esp_err_to_name(ret));
  }
}

void app_main()
{
  init_sdcard();
  init_camera();

  while (1)
  {
    ESP_LOGI(TAG, "Taking picture...");
    camera_fb_t *pic = esp_camera_fb_get();
    int64_t timestamp = esp_timer_get_time();

    char *pic_name = malloc(17 + sizeof(int64_t));
    sprintf(pic_name, "/sdcard/pic_%lli.jpg", timestamp);
    FILE *file = fopen(pic_name, "w");
    if (file != NULL)
    {
      size_t err = fwrite(pic->buf, 1, pic->len, file);
      ESP_LOGI(TAG, "File saved: %s", pic_name);
    }
    else
    {
      ESP_LOGE(TAG, "Could not open file =(");
    }
    fclose(file);
    free(pic_name);

    vTaskDelay(20000 / portTICK_RATE_MS);
  }
}

这是引脚配置文件:

config XCLK_FREQ
  int "XCLK Frequency"
  default "20000000"
  help
      The XCLK Frequency in Herz.
      
menu "Pin Configuration"
  config D0
    int "D0"
    default "5"
  config D1
    int "D1"
    default "18"
  config D2
    int "D2"
    default "19"
  config D3
    int "D3"
    default "21"
  config D4
    int "D4"
    default "36"
  config D5
    int "D5"
    default "39"
  config D6
    int "D6"
    default "34"
  config D7
    int "D7"
    default "35"
  config XCLK
    int "XCLK"
    default "0"
  config PCLK
    int "PCLK"
    default "22"
  config VSYNC
    int "VSYNC"
    default "25"
  config HREF
    int "HREF"
    default "23"
  config SDA
    int "SDA"
    default "26"
  config SCL
    int "SCL"
    default "27"
  config RESET
    int "RESET"
    default "32"
endmenu
	
endmenu