Saving DNSServer CaptivePortalAdvanced credentials on a 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();
}

Thank you

Miteshi

You don't post complete code. "ssid" and "password" seem to be global variables but we have no clue about the type of them.

You print the two variables to the file without a delimiter, so parsing is probably not possible. But in your code you don't do any parsing at all, you don't even try to load the two values from the file.

The SPIFFS.begin() call must go to the setup() routine. It must also be called before reading from the file system.

Sorry the code is quite long, it's mentioned here: DNSServer---esp32/examples/CaptivePortalAdvanced at master · zhouhan0126/DNSServer---esp32 · GitHub

The only part that I changed was the credentials sketch, which is mentioned here.

Did you read my complete post? Did you change the code to address the errors I mentioned?

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