WiFi.begin to Strings?

Hey Im making a cool item. The thing that has stopped me to continue is that I cant use string as the ssid and password for wifi.begin!

I really wanna use string for it becuase it will be hard to convert string to const char*.
So if any of you geniuses know how I can do it?

This is the error:

no matching function for call to 'ESP8266WiFiClass::begin(String&, String&)'

The section of it:

      if (myFile.find("SSID: \"")) {
        String ssidName = myFile.readStringUntil('"');
        Serial.println("SSID: " + ssidName);

        if (myFile.find("Password: \"")) {
          String passwordName = myFile.readStringUntil('"');
          Serial.println("Password: " + passwordName);
          
          Serial.println();
          Serial.print("Connecting to ");
          Serial.println(ssidName);

         WiFi.begin(ssidName, passwordName);
          
          while (WiFi.status() != WL_CONNECTED) {
            digitalWrite(redPin, LOW);
            digitalWrite(greenPin, HIGH);
            delay(166);
            digitalWrite(greenPin, LOW);
            digitalWrite(yellowPin, HIGH);
            delay(166);
            digitalWrite(yellowPin, LOW);
            digitalWrite(redPin, HIGH);
            delay(166);
            Serial.print(".");
          }

        }


      }

Thank you so much! Or you can tell me how I make it into const char* :slight_smile:

I would not use String in a Arduino (8 bit) environment, but that is another story.

If you insist on using String, at least read the reference once.

There is a function for the conversion you miss so badly c_str().

1 Like

Whandall:
I would not use String in a Arduino (8 bit) environment, but that is another story.

If you insist on using String, at least read the reference once.

There is a function for the conversion you miss so badly c_str().

Cool. But do you know any way to convert string to const char* tho?

Why not try

WiFi.begin(ssidName.c_str(), passwordName.c_str());

// or with a cast (I doubt it's neccessary)

WiFi.begin((const char*)ssidName.c_str(), (const char*)passwordName.c_str());
1 Like

Whandall:
Why not try

WiFi.begin(ssidName.c_str(), passwordName.c_str());

// or with a cast (I doubt it's neccessary)

WiFi.begin((const char*)ssidName.c_str(), (const char*)passwordName.c_str());

Thanks the top one works :slight_smile:
Now it works! Connection to the wifi perfect :wink:

Whandall:
Why not try

WiFi.begin(ssidName.c_str(), passwordName.c_str());

// or with a cast (I doubt it's neccessary)

WiFi.begin((const char*)ssidName.c_str(), (const char*)passwordName.c_str());

Hey do you know how I can make a String into a "global variable"? Thanks!

1 Like
String globalString;

void setup() {
  globalString = "I'm global";
}
void loop() {}