我正在使用 ESP32CAM 板,AI THINKER,我只是尝试访问 SD 卡并删除其所有内容(基本上在一段时间后,我希望 esp32 删除该卡,这样它就不会变满,符合逻辑...是吗?)
无论如何,我花了过去 2 天的时间来获得任何结果,并且甚至向 chatgpt 寻求帮助......这是以下代码:
#include <FS.h>
#include <SD_MMC.h>
#include <SPI.h>
#include "soc/soc.h" // Disable brownout problems
#include "soc/rtc_cntl_reg.h" // Disable brownout problems
#define SD_CS 13
void setup() {
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector
Serial.begin(115200); while (!Serial);
if (!SD_MMC.begin()) {
Serial.println("SD Card Mount Failed");
return;
}
// Open the root directory
File root = SD_MMC.open("/");
if (!root) {
Serial.println("Failed to open root directory");
return;
}
// Delete all files in the root directory
while (true) {
File file = root.openNextFile();
if (!file) {
break; // No more files
}
Serial.print("Deleting file: ");
Serial.println(file.name());
file.close();
file.flush();
SD_MMC.remove(file.name()); // Use SD_MMC.remove() to delete the file
}
root.close(); Serial.println("All files deleted");
}
void loop() {
// Nothing to do here
}
如果文件直接在根目录下,串口监视器会列出所有照片,实际上显示如下:
15:21:22.312 -> load:0x3fff0030,len:1344
15:21:22.312 -> load:0x40078000,len:13964
15:21:22.312 -> load:0x40080400,len:3600
15:21:22.312 -> entry 0x400805f0
15:21:23.122 -> Deleting file: System Volume Information
15:21:23.122 -> Deleting file: pictureCamera1_20230805_145836_23186.jpg
15:21:23.122 -> Deleting file: pictureCamera1_20230805_145836_23537.jpg
15:21:23.122 -> Deleting file: pictureCamera1_20230805_145836_23883.jpg
15:21:23.122 -> Deleting file: pictureCamera1_20230805_145836_24258.jpg
15:21:23.122 -> Deleting file: pictureCamera1_20230805_145836_24605.jpg
15:21:23.122 -> Deleting file: pictureCamera1_20230805_145844_31100.jpg
15:21:23.122 -> Deleting file: pictureCamera1_20230805_145844_31465.jpg
15:21:25.160 -> Deleting file: pictureCamera1_20230805_161045_105800.jpg
15:21:25.249 -> Deleting file: pictureCamera1_20230805_161045_106138.jpg
15:21:25.297 -> Deleting file: pictureCamera1_20230805_161045_106476.jpg
15:21:25.375 -> Deleting file: pictureCamera1_20230805_161045_106815.jpg
15:21:25.375 -> All files deleted
15:25:39.790 -> ets Jun 8 2016 00:22:57
但是,当我取出SD卡并将其放在PC上读取内容时,所有文件仍然存在,并且没有损坏,我可以完全打开并查看其内容。
有什么建议吗?
编辑1:
这可能是我编写jpeg文件的方式吗?
if (!motion.update())
return;
if (motion.detect()) {
debug("INFO", String("Motion detected in ") + motion.getExecutionTimeInMicros() + " us");
time_t now = time(nullptr);
Serial.print("Current time: ");
Serial.println(ctime(&now));
/** ftp upload of caputred image */
/** get current time as file name */
//time_t now = time(nullptr); // Get the current time from the system clock
struct tm* timeinfo;
timeinfo = localtime(&now);
//for (int i = 1; i <= 1; i++) {
char filename[50];
snprintf(filename, sizeof(filename), "c_%04d%02d%02d_%02d%02d%02d_%lu",timeinfo->tm_year + 1900, timeinfo->tm_mon + 1, timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, millis());
// this for loop indentifies how many photos taken at a time
// Path where new picture will be saved in SD Card
String path = "/mydir1/picture" + String(filename) +".jpeg";
//String path = "/mydir1/picture.jpg";
//fs::FS &fs = SD_MMC;
Serial.printf("Picture file name: %s\n", path.c_str());
File file = SD_MMC.open(path.c_str(), FILE_WRITE);
if(!file){
Serial.println("Failed to open file in writing mode");
}
else {
delay(400);
file.write(camera.getBuffer(), camera.getFileSize()); // payload (image), payload length
file.close();
Serial.printf("Saved file to path: %s\n", path.c_str());
//EEPROM.write(0, pictureNumber);
//EEPROM.commit();
}
file.close();
SD_MMC.remove("/"+ file.name());