Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By NickLD
#72675 Hi, I'm writing some code for the ESP8266 that needs to connect to a WiFi network stored in SPIFFS.
The ESP is able to connect to a WiFi network without a problem as long as the credentials are hard-coded.
The moment I write the code to pull values from SPIFFS and store it in a struct, it all comes crashing down, and refuses to connect.
I have verified that the values being pulled out from the file is verbatim what I am hard coding.

Below is the snippets of my code that are related to the issue:



Initial SPIFFS stuff:
Code: Select all  SPIFFS.begin();

struct Config { // Simple structure for storing various settings
  String WiFiSSID;
  String WiFiPass;
...other variables here that are unrelated...
} config;

if (!SPIFFS.exists("/config.cfg")) {
...other code...
} else {
 f = SPIFFS.open("/config.cfg", "r");
    if (!f) {
      Serial.println("file open failed");
    }
    Serial.println("Reading from SPIFFS file");

    config.WiFiSSID = f.readStringUntil('\n');
    config.WiFiPass = f.readStringUntil('\n');
    ... set other variables in struct unrelated to issue...
    connectWiFi(); // Calls method to connect to the Stored WiFi credentials
}


connectWiFi() method (Where I presume issue lies):
Code: Select allvoid connectWiFi() {
    Serial.print("Connecting to '");
    const char* wifissid = config.WiFiSSID.c_str(); // extract SSID from Struct
    const char* wifipass = config.WiFiPass.c_str(); // Extract PASS from Struct
    Serial.print(config.WiFiSSID.c_str());
    Serial.println("'");
    Serial.print("Pass:'");
    Serial.print(config.WiFiPass.c_str());
    Serial.println("'");
    int cntr = 0; // Counter for how long before connection should time-out
   // The below 3 lines are from me researching my issue for HOURS trying to figure out how to fix the issue, some said this helped them with similar issues. This has had no effect on the issue however.
    WiFi.disconnect();
    WiFi.setAutoConnect(true);
    WiFi.mode(WIFI_STA);

    WiFi.begin(wifissid, wifipass);
    while (WiFi.status() != WL_CONNECTED) { // Loop unless we are connected to a network
      delay(250);
      Serial.print(".");
      cntr++;
      if (cntr > 100) {
        Serial.println("Took too long to connect. breaking trying and starting access point..");
        WiFi.mode(WIFI_AP); //switching to AP mode
        WiFi.softAP("ESP8266");
        Serial.println("CONFIG MODE");
        break;
      }

    Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());

}



Does anyone have any thoughts or ideas on whats going on and how I can fix it?

I will add that I am running on the LATEST git version of the Arduino ESP package, as I also need to implement Enterprise WiFi connectivity options within my code, which only seems possible with the current GIT version.

Any help is greatly appreciated!
Thanks
User avatar
By NickLD
#73682 Super-Late update to the post, but just wanted to make it clear what I did to eventually get this working.

I ditched trying to roll my own parsing method to save very small amount of space, and decided to just go with ArduinoJSON for encoding and parsing configuration data. It's really the best option when you need more than a very small handful of parameters stored.