Only First Line of SPIFFS works on WiFi.begin() - ESP8266-12E

780 Views Asked by At

When I run the code below only one line will work. If I comment out the SSID write/read portion the password will work. It I comment out the password portion the SSID will work. But, if I leave both in the WiFi never connects. I hard coded both the SSID and Password in the WiFi.begin() as a sanity check and sure enough it connects. So it must have something to do with the way I am either writing or reading more than one line using SPIFFS. I do however get both lines back correctly, so it seems at least, if I Serial.println() them. I am also up for any suggested alternatives to SPIFFS since I am not too thrilled with using functions that return Strings.. I would rather have a function that returns a char* of course but my C is pretty rusty. I also tried various terminators \r then \0 at the writing and reading level, nothing has worked so far, also tried trimming the string. I left out the basics but I do a SPIFFS.begin() and format (not in that order of course) which seems to be working as I seem to get the Serial.println() back seemingly correct. I have the latest arduino IDE and SPIFFS installed, using win10 x64. Here is the code:

if (SPIFFS.begin())
{
    Serial.println("SPIFFS.begin()...");

    File f = SPIFFS.open(file, "w");//write...

    if (!f)
    {
        Serial.println("failed to open.");
    }
    else//yes, file exists...
    {
        //found file write to it..
        f.println("SomeSSID");//ssid...
        f.println("MyPassword");//password...

        f.close();

        //now lets read the file..
        f = SPIFFS.open(file, "r");

        if (!f)
        {
            Serial.println("file failed to open..");
        }
        else
        {
            //read file...          
            String SSID = f.readStringUntil('\n');              
            String PASSWORD = f.readStringUntil('\n');

            f.close();

            //this didn't help
            /*SSID.trim();
            PASSWORD.trim();*/

            ssid = (char*)SSID.c_str();
            pwd = (char*)PASSWORD.c_str();

            Serial.println(ssid);
            Serial.println(pwd);
        }
    }       
}

//does not work..
WiFi.begin(ssid, pwd);
1

There are 1 best solutions below

0
On

@dandavis - thanks! I ended up just changing it to: const char* ssid = f.readStringUntil('\n').c_str();