当前位置:   article > 正文

ESP32cam--保存JPEG格式图片到SD卡(Arduino IDE)_esp32-cam拍照并保存到microsd卡

esp32-cam拍照并保存到microsd卡

在这里插入图片描述
ESP32Cam模块的原理图

ESP32Cam自带了一个SD卡模块
在这里插入图片描述但是在这里SD卡的连线并没有体现出SPI协议。
在这里插入图片描述

一张Micro SD卡引脚图如下:

在这里插入图片描述
一张micro SD卡有8个引脚。下表描述了每个引脚的功能。

引脚序号名称功能说明ESP32cam原理图
1NC未连接HS2_DATA2(GPIO12)
2CS片选/从选择(SS)HS2_DATA3(GPI13)
3DI主输出/从输入(MOSI)HS2_CMD(GPIO15)
4VDD电源电压VDD
5CLK时钟(SCK)HS2_CLK(GPIO14)
6VSS电源电压地VSS
7DO主输入/从输出(MISO)HS2_DATA0(GPIO2)
8RSV保留HS2_DATA1 (GPIO4)

 由原理图是已经可以知道各个引脚的连接,但是,基于Arduino的尿性,我还是不知道该怎么配置。
 然后我就一直在纠结怎么在SPI初始化的时候自定义引脚就像这样:

SPI.begin(14,2,15,13);//SCK,MISO,MOSI,ss
delay(10);
if (!SD.begin(13, SPI, 40000000, "/sd")) {
	Serial.println("Card Mount Failed");
}
  • 1
  • 2
  • 3
  • 4
  • 5

然后全部失败!

希望

 可笑的是,在亚马逊卖ESP32cam模块的界面找到了,它的使用源码并且找到了一个人写的:https://github.com/v12345vtm/CameraWebserver2SD/blob/master/CameraWebserver2SD/CameraWebserver2SD.ino

这哥们用的是SD_MMC模块来写入SD卡的,最后终于搞定!

核心代码:

   //acquire a frame
 camera_fb_t * fb = esp_camera_fb_get();
 if (!fb) 
 {
     Serial.print( "Camera capture failed");
 } 
 else 
 {
     if(fb->format != PIXFORMAT_JPEG)
     {
           Serial.print("jpeg error");
           esp_camera_fb_return(fb);
      } 
      else 
      {
           const char * path = "/capture.jpg";
           fs::FS &fs = SD_MMC; 
           Serial.printf("Writing file: %s\n", path);
           File file = fs.open(path, FILE_WRITE);
           if(!file)
           {
               Serial.println("Failed to open file for SDwriting547");
           } 
           else
           {
              file.write(fb->buf , fb->len); //payload , lengte vd payload
              Serial.println("succes to open file for SDwriting552");
           }
          esp_camera_fb_return(fb); 
      }
 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

最终的完整代码

按键设置为:GPIO16

#include "esp_camera.h"
#include "FS.h"
#include <SPI.h>
#include <SD.h>
#include "SD_MMC.h"

int buttonState = 0;
int btnHold = 0;

#define KEY_PIN       16

#define PWDN_GPIO_NUM     32
#define RESET_GPIO_NUM    -1
#define XCLK_GPIO_NUM      0
#define SIOD_GPIO_NUM     26
#define SIOC_GPIO_NUM     27

#define Y9_GPIO_NUM       35
#define Y8_GPIO_NUM       34
#define Y7_GPIO_NUM       39
#define Y6_GPIO_NUM       36
#define Y5_GPIO_NUM       21
#define Y4_GPIO_NUM       19
#define Y3_GPIO_NUM       18
#define Y2_GPIO_NUM        5
#define VSYNC_GPIO_NUM    25
#define HREF_GPIO_NUM     23
#define PCLK_GPIO_NUM     22

static camera_config_t camera_config = {
    .pin_pwdn = PWDN_GPIO_NUM,
    .pin_reset = RESET_GPIO_NUM,
    .pin_xclk = XCLK_GPIO_NUM,
    .pin_sscb_sda = SIOD_GPIO_NUM,
    .pin_sscb_scl = SIOC_GPIO_NUM,
    
    .pin_d7 = Y9_GPIO_NUM,
    .pin_d6 = Y8_GPIO_NUM,
    .pin_d5 = Y7_GPIO_NUM,
    .pin_d4 = Y6_GPIO_NUM,
    .pin_d3 = Y5_GPIO_NUM,
    .pin_d2 = Y4_GPIO_NUM,
    .pin_d1 = Y3_GPIO_NUM,
    .pin_d0 = Y2_GPIO_NUM,
    .pin_vsync = VSYNC_GPIO_NUM,
    .pin_href = HREF_GPIO_NUM,
    .pin_pclk = PCLK_GPIO_NUM,
    
    .xclk_freq_hz = 20000000,
    .ledc_timer = LEDC_TIMER_0,
    .ledc_channel = LEDC_CHANNEL_0,
    
    .pixel_format = PIXFORMAT_JPEG,
    .frame_size = FRAMESIZE_XGA,
    .jpeg_quality = 12,
    .fb_count = 1,
};

esp_err_t camera_init() {
    //initialize the camera
    esp_err_t err = esp_camera_init(&camera_config);
    if (err != ESP_OK) {
        Serial.print("Camera Init Failed");
        return err;
    }
    sensor_t * s = esp_camera_sensor_get();
    //initial sensors are flipped vertically and colors are a bit saturated
    if (s->id.PID == OV2640_PID) {
    //        s->set_vflip(s, 1);//flip it back
    //        s->set_brightness(s, 1);//up the blightness just a bit
    //        s->set_contrast(s, 1);
    }
    
    Serial.print("Camera Init OK");
    return ESP_OK;
}

void sd_init(void)
{
    Serial.print("Initializing SD card...");
    
    //SD card init
    if (!SD_MMC.begin()) {
        Serial.println("Card Mount Failed");
        return;
    }
    uint8_t cardType = SD_MMC.cardType();
    
    if (cardType == CARD_NONE) {
        Serial.println("No SD_MMC card attached");
        return;
    }
    
    Serial.print("SD_MMC Card Type: ");
    if (cardType == CARD_MMC) {
        Serial.println("MMC");
    } else if (cardType == CARD_SD) {
        Serial.println("SDSC");
    } else if (cardType == CARD_SDHC) {
        Serial.println("SDHC");
    } else {
        Serial.println("UNKNOWN");
    }
    
    if (SD_MMC.exists("test.txt")) {
        Serial.println("exist");
    }
    else 
    {
        Serial.println("doesn't exist");
    }
}

void setup() {
    Serial.begin(115200);
    
    camera_init();
    
    sd_init();

    pinMode(KEY_PIN, INPUT_PULLUP); //设置按键管脚输入上拉模式

    Serial.println("sys is running!");
}

void loop() 
{
    buttonState = digitalRead(KEY_PIN); //读取按键返回状态值
    if (buttonState == LOW && btnHold == 0)
    { // 若按键被按下
        delay(100); //等待跳过按键抖动的不稳定过程
        if (buttonState == LOW) // 若按键被按下
        {
            Serial.print("click\r\n");
            btnHold = 1;
        
            //acquire a frame
            camera_fb_t * fb = esp_camera_fb_get();
            if (!fb)
            {
                Serial.print( "Camera capture failed");
            }
            else
            { 
                const char * path = "/capture.jpg";
                fs::FS &fs = SD_MMC;
                Serial.printf("Writing file: %s\n", path);
                
                File file = fs.open(path, FILE_WRITE);
                if (!file) 
                {
                    Serial.println("Failed to open file for SDwriting547");
                }
                else
                {
                    file.write(fb->buf , fb->len); //payload , lengte vd payload
                    Serial.println("succes to open file for SDwriting552");
                }
                //return the frame buffer back to the driver for reuse
                esp_camera_fb_return(fb);
            }
        }
    }
      
    if (buttonState == HIGH)
    {
        btnHold = 0;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/511990
推荐阅读
相关标签
  

闽ICP备14008679号