Changing where details are saved: EEPROM to text file

Hey,
I'm currently working on the CaptivePortalAdvanced example provided by DNSServer. This provides a sketch named 'Credentials' where the credentials are saved in the EEPROM. But I want to save the details in a text file, so I decided to use FS.h for this. I developed a code for this, but it doesn't printout the necessary details, like it does in the original code. Any pointers to what I'm doing wrong or any advice on what I should do would be really appreciated!

This is the original code provided, where the details are saved in the EEPROM:

void loadCredentials() {
  EEPROM.begin(512);
  EEPROM.get(0, ssid);
  EEPROM.get(0 + sizeof(ssid), password);
  char ok[2 + 1];
  EEPROM.get(0 + sizeof(ssid) + sizeof(password), ok);
  EEPROM.end();
  if (String(ok) != String("OK")) {
    ssid[0] = 0;
    password[0] = 0;
  }
  Serial.println("Recovered credentials:");
  Serial.println(ssid);
  Serial.println(strlen(password) > 0 ? "********" : "<no password>");
}

void saveCredentials() {
  EEPROM.begin(512);
  EEPROM.put(0, ssid);
  EEPROM.put(0 + sizeof(ssid), password);
  char ok[2 + 1] = "OK";
  EEPROM.put(0 + sizeof(ssid) + sizeof(password), ok);
  EEPROM.commit();
  EEPROM.end();
}

This is the code that I developed to save the details in a text file:

void saveCredentials() {
  bool success = SPIFFS.begin();
  if (!success) {
    Serial.println("Error");
    return;
  }
  File file = SPIFFS.open("/file.txt", "w");
  char ssid1 = file.print(ssid);
  char password1 = file.print(password);
  file.close();
}


void loadCredentials() {
  File file2 = SPIFFS.open("/file.txt", "r");
  Serial.write(file2.read());
  Serial.println("Recovered credentials:");
  Serial.println(ssid);
  Serial.println(strlen(password) > 0 ? "********" : "<no password>");
  file2.close();
}

The code that I'm using is given here: DNSServer---esp32/CaptivePortalAdvanced.ino at master · zhouhan0126/DNSServer---esp32 · GitHub

I have only changed the credentials.ino sketch, which I've mentioned above.

Thank you
Miteshi

When saving to eeprom you dump the whole ssid or password buffer - all the 32 bytes including the trailing 0 and whatever padding is left in the buffer

/* Don't set this wifi credentials. They are configurated at runtime and stored on EEPROM */
char ssid[32] = "";
char password[32] = "";

so in eeprom you have

YOURSSID0******YOURPWD0*******OK0 (Stars representing padding up to 32 bytes)

When printing to the file neither the trailing null nor the padding is added and so you end up with

YOURSSIDYOURPWD

In the file and it’s impossible to read back as you have no separator (not to mention that your way to read back the content is not doing anything useful)

Dump everything in the file as binary in the same way so you’ll get known size to read back or add an end of field market like a new line

@J-M-L is this what you mean?

void saveCredentials() {
  bool success = SPIFFS.begin();
  if (!success) {
    Serial.println("Error");
    return;
  }
  File file = SPIFFS.open("/file.txt", "w");
  char ssid1 = file.print(ssid);
  file.print("\n");
  char password1 = file.print(password);
  file.close();

void loadCredentials() {
  File file2 = SPIFFS.open("/file.txt", "r");
  Serial.write(file2.read());
  Serial.println("Recovered credentials:");
  Serial.println(ssid);
  Serial.println(strlen(password) > 0 ? "********" : "<no password>");
  file2.close();
}
}

Something like this for writing

  File file = SPIFFS.open("/file.txt", "w"); // best to test if this was successful 
  file.println(ssid);
  file.println(password);
  file.close();

Reading back you need to read a full line into te buffer (making sure to not overflow). A simple read won’t cut it

Alternatively you could dump the full buffers as a binary write then it’s easier to read back and you know the number of bytes to get

Your brackets are off

@J-M-L So I changed the code to the following:

void saveCredentials() {
  bool success = SPIFFS.begin();
  if (!success) {
    Serial.println("Error in openning the file");
    return;
  }
  File fileWrite = SPIFFS.open("/file.txt", "w");
  if (!fileWrite) {
    Serial.println("Failed to open file for Writing");
    return;
  }

  fileWrite.println(ssid);
  fileWrite.println(password);
  fileWrite.close();

}


void loadCredentials() {
  bool success = SPIFFS.begin();
  if (!success) {
    Serial.println("Error in openning the file to read");
    return;
  }
  File fileRead = SPIFFS.open("/file.txt", "r");
  if (!fileRead) {
    Serial.println("Failed to open file for reading");
    return;
  }
  Serial.write(fileRead.read());
  Serial.println(fileRead.read());
  fileRead.close(); 
}

But in the serial monitor it says "Failed to open file for reading"

What do you expect from this???
read() gets only one byte. You need to read all the bytes until the CRLF (and not save those as part of the data)

I want to print the data saved in the text file, on the serial monitor. I'm quite new to this, will you be able to tell me what I am to try instead of it?

You need to read all the bytes that are in the file So a while as long as something is available() in the file to read()

while (fileRead.available()) Serial.write((char) fileRead.read());
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.