1

I've got a microSD module adapter from the far east, and so far so good, and although it can create files using writeFile, it does not append messages. It returns true, i.e: message appended successfully but when you read the file on the computer or when ESP32 reads on reboot, it's blank.

This is how I call the function:

char* sdFileName = "/v.ea";
if (appendFile(SD, sdFileName, "Heyy")) {
//success
} else { 
//failed
}

This is the modified, appendFile function:

bool appendFile(fs::FS &fs, const char * path, const char * message) {
  bool appendSuccess = false;
  if (DEBUG == 1) {
    Serial.print("Appending to the file: ");
    Serial.println(path);
  }
  File file = fs.open(path, FILE_APPEND);
  if (!file) {
    appendSuccess = false;
    if (DEBUG == 1) {
      Serial.println("Failed opening the file.");
    }
  }
  if (file.print(message)) {
    appendSuccess = true;
    if (DEBUG == 1) {
      Serial.println("Appended succesfully.");
    }
  } else {
    appendSuccess = false;
    if (DEBUG == 1) {
      Serial.println("Appending failed.");
    }
  }
  file.close();
  if (appendSuccess) {
    return true;
  } else {
    return false;
  }
}

I've even tried the writeFile function with file.seek(EOF) but no luck. When calling appendFile for the first time, this appears:

[W][sd_diskio.cpp:149] sdCommand(): token error [17] 0x17
Appending succesfully.

When the appendFile is called for the second time, this appears in serial monitor:

[W][sd_diskio.cpp:149] sdCommand(): token error [17] 0x17
[W][sd_diskio.cpp:149] sdCommand(): token error [17] 0x5
[W][sd_diskio.cpp:149] sdCommand(): token error [17] 0x5
[E][vfs_api.cpp:265] VFSFileImpl(): fopen(/sd/v.ea) failed
Appending failed.

I am using two SPI devices: MFRC522, and before calling SD's functions, I do a digitalWrite(SD_SS, LOW) to "enable" the SD functions till it completes its operations and returns back to HIGH to disable it.

The example in SD(esp32) works fine - writing, appending, deleting, renaming, etc. all work well.

3
  • Did you initialize SD with SD.begin()? How do you handle the MFRC522? You have to disabled it, when the SD reader is active on the SPI bus. Where do you use writeFile? From the code I see, there's is nothing wrong, beside it is too complex. If there is a problem with the code, then you missed to show us the part where the problem is located. When you said the SD(esp32) works fine, do you mean example code for ESP32 where you don't use additional hardware? – Peter Paul Kiefer Jan 29 at 14:35
  • @PeterPaulKiefer, yes, it is obvious, otherwise, even the writeFile would fail, and it'd report that SD mount failed first and foremost. Also yes, as I said, when I disable MFRC522, I enable SD, complete operations, then disable SD and enable MFRC522. The problem, as I suspect is in appendFile, as Serial monitor shows the [W] and [E], respectively. And also yes, SD(esp32) from File > Examples works well for all operations. – The VaLo Jan 29 at 21:07
  • P.S.: I use writeFile if readFile returns false, that is, when the file does not exist, it invokes writeFile(SD, sdFileName, ""). Specifically, when fs.open(path) returns false, I instruct to create a new file by invoking writeFile. – The VaLo Jan 29 at 21:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.