Post topics, source code that relate to the Arduino Platform

User avatar
By Miteshi
#94738 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. Also, when I print it, the serial monitor it says "Failed to open file for reading". 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:
Code: Select allvoid 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:
Code: Select allvoid 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();
}


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

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

Thank you
Miteshi