Michael Silva
Michael Silva

Reputation: 5

SD_MCC.remove() function not working on ESP32CAM

I am using an ESP32CAM board, AI THINKER, I'm simply trying to access the SD card and to remove all its contents (basically after some time, i would like the esp32 to delete the card so it doesn't get full, logical...yes?)

Anyhow, I've spent the last 2 days on getting any results and have turned to even chatgpt for help... here is the following code:

#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 
}

if the files are in the root directly, the serial monitor lists all the photos, in fact it shows the following:

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

however, when i remove the SD card and place it on a PC to read the contents, all the files still exist, and are not corrupted, i can fully open and view its contents.

any suggestions?

EDIT1:

could it be the way im writing the jpeg file?

  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();

Upvotes: 0

Views: 469

Answers (1)

علی روستایی
علی روستایی

Reputation: 21

SD_MMC.remove("/"+ String(file.name());

Upvotes: 0

Related Questions