赞
踩
ESP32Cam自带了一个SD卡模块
但是在这里SD卡的连线并没有体现出SPI协议。
一张Micro SD卡引脚图如下:
一张micro SD卡有8个引脚。下表描述了每个引脚的功能。
引脚序号 | 名称 | 功能说明 | ESP32cam原理图 |
---|---|---|---|
1 | NC | 未连接 | HS2_DATA2(GPIO12) |
2 | CS | 片选/从选择(SS) | HS2_DATA3(GPI13) |
3 | DI | 主输出/从输入(MOSI) | HS2_CMD(GPIO15) |
4 | VDD | 电源电压 | VDD |
5 | CLK | 时钟(SCK) | HS2_CLK(GPIO14) |
6 | VSS | 电源电压地 | VSS |
7 | DO | 主输入/从输出(MISO) | HS2_DATA0(GPIO2) |
8 | RSV | 保留 | 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");
}
然后全部失败!
可笑的是,在亚马逊卖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); } }
按键设置为: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; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。