-->
Page 1 of 1

Problem connecting to WiFi network from SPIFFS

PostPosted: Sun Dec 24, 2017 1:58 am
by NickLD
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

Re: Problem connecting to WiFi network from SPIFFS

PostPosted: Thu Jan 04, 2018 2:45 pm
by mrburnette
Maybe a "canned" example of credentials stored in eeprom will be helpful:

https://github.com/esp8266/Arduino/blob/master/libraries/DNSServer/examples/CaptivePortalAdvanced/credentials.ino

Ray

Re: Problem connecting to WiFi network from SPIFFS

PostPosted: Sat Feb 03, 2018 7:21 pm
by NickLD
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.

Re: Problem connecting to WiFi network from SPIFFS

PostPosted: Sat Aug 11, 2018 7:09 pm
by apicquot
I a having the same issue: using file.readString() or file.readuntil('\n') seem to create the failure. May it be because both call the yield() ?