I've implemented a small WiFi connection procedure into my ESP32 project. The available networks get printed with index, then I'm entering the index and retrieve the network name from it. The password is entered via serial in clear text.

Before connecting, I'm printing the credentials and thus could confirm they are read into const char * strings properly.

Unfortunately when using this procedure the connection to my AP times out. I've also tried to override the strings with hardcoded values and then the connection works as expected.

while (!Serial.available()) {
    // (timeout is implemented here)
String index_s = Serial.readStringUntil('\n');
long index = index_s.toInt();

// { same while as above }
String passw = Serial.readStringUntil('\n');

const char *ssid = WiFi.SSID(index).c_str();
const char *pass = passw.c_str();

// with this it works:
// ssid = "my_network";
// pass = "my_password";

Serial.printf("connecting to %s using %s", ssid, pass);
WiFi.begin(ssid, pass);
  • removing comments now, as they are not necessary :p Mar 15, 2019 at 5:46
  • 1
    Possible duplicate of String compare when using Serial
    – Juraj
    Mar 15, 2019 at 7:53
  • As you can see, I don't do string compare. So what do you think how I could've found the question you mentioned? Yes, it's the same reason. No, it's not the same issue. Mar 15, 2019 at 16:01

1 Answer 1


The answer was quite simple but of the kind you need some time get find out. The issue was a hiding carriage return.


Did the trick. String.trim() removes the \r and thus the password gets accepted.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.