user47872: I'm using an esp8266 12-E nodemcu
I wrote a code that scans the available WiFi networks, print them through serial communication,ask the user which network he wants to connect to and then the password for that network. The problem is that the nodemcu won't connect to that network. I think that the problem is when I convert the strings buffer_pw and buffer_ssid to the char arrays password and ssid. Why does it give problems to connect?
#include <ESP8266WiFi.h>

int num_ap; //number of access points 
String buffer_ssid = "empty"; //temporary stores the ssid in here because then it has to be converted in a char array
String buffer_pw = "empty"; //temporary stores password and same thing as^^
bool askPW = false; //"Did I already ask the password?"
bool connesso = false; //"Did I already connect?"


void setup()
{
  Serial.begin(9600);
  WiFi.mode(WIFI_STA); //put the nodemcu in sta mode so that it can connect to access points
  WiFi.disconnect();   //it disconnects from any access point it was connected to
  Serial.println("Setup completo"); 
  num_ap = WiFi.scanNetworks(); //scans for access points
  if (num_ap == 0)   //if there aren't any access points available
  {
    Serial.println("Nessun Access Point rilevato");
  }
  else //if there are some access points available
  {
    Serial.println("Access Point rilevati:");
    delay(200);
    for (int i = 0; i < num_ap; i++)
    {
      Serial.println(" ");
      Serial.print(i + 1);
      Serial.print(") ");
      Serial.print(WiFi.SSID(i)); //print access point's ssid
      Serial.print(" ");
      Serial.print("(Potenza segnale: ");
      Serial.print(WiFi.RSSI(i)); //prints access point's rssi(strength of signal)
      Serial.print(")");
      delay(50);
    }
    delay(500);
    Serial.println(" ");
    Serial.println(" ");
    Serial.println("A quale Access Point ti vuoi connettere?");

  }
}



void loop()
{
  if (buffer_ssid == "empty") //if the variable has the standard value "empty" so if the user has already given an ssid. I need this to execute  this piece of code once
   {
    if (Serial.available() > 0)
     {
      buffer_ssid = Serial.readString();  //stores user input(the ssid) in buffer_ssid
      if (buffer_ssid != "empty") //if the variable isn't empty(if the code has successfully saved the ssid) prints the ssid
       {
        Serial.println(buffer_ssid);
       }
     }
   }

  if(buffer_ssid != "empty") //if the user has given an ssid
  {
   if(askPW == false) //if the code has asked the password(I need this in order to execute this piece of code once)
    {
     Serial.println("Inserire la password per connettersi all'Access Point, se non serve premere spazio e invio"); //"insert the password"
     askPW = true;
    }
    if (Serial.available() > 0)
    {
      buffer_pw = Serial.readString(); //saves user input(the password) in the variable buffer_pw
      if(buffer_pw != "empty") //if the code has successfully saved the password so it hasn't the standard value "empty"
      {
        Serial.println(buffer_pw);
      }
    }
  }

  if (buffer_pw != "empty" && buffer_ssid != "empty") //if buffer_ssid and buffer_pw aren't "empty", so if the user inserted them
  {
    if(connesso == false) //in order to execute this piece of code once
    {
     //converts the strings buffer_ssid and buffer_pw in char arrays(I have to do this because WiFi.begin() doesn't accept strings)
     char ssid[buffer_ssid.length()];
     buffer_ssid.toCharArray(ssid,buffer_ssid.length());
     char password[buffer_pw.length()];
     buffer_pw.toCharArray(password,buffer_pw.length());
     //start connecting
     Serial.println("Connessione in corso");
     Serial.println(ssid); //prints ssid
     Serial.println(password);//prints password so I know that they were converted succesffully
     WiFi.begin(ssid,password);//start connecting
     while(WiFi.status() != WL_CONNECTED) //while it's not connected
     {
      Serial.print(".");
      delay(500); 
     }
      Serial.println("Connesso!"); //"Connected!"
      Serial.println("Il mio ip locale: ");
      Serial.print(WiFi.localIP()); //nodemcu's local IP

    }
    connesso = true;  //set connesso to true so that this piece of code doesn't loop
  }
}


user37622: Problem is that you set to toCharArray the length of the String. But the second parameter is the maximum allowed length of the output and there is then no space for the terminating zero and string is cut at last character. 
The function toCharArray knows the size of the String. The second parameter should be the size of the char array buffer_ssid.toCharArray(ssid, sizeof(ssid));
And next problem could be that you copy the CR/LF characters from input into ssid and password. You can see it in debug printouts.

47872: <p><strong>I'm using an esp8266 12-E nodemcu</strong>
I wrote a code that scans the available WiFi networks, print them through serial communication,ask the user which network he wants to connect to and then the password for that network. The problem is that the nodemcu won't connect to that network. I think that the problem is when I convert the strings <strong>buffer_pw</strong> and <strong>buffer_ssid</strong> to the char arrays <strong>password</strong> and <strong>ssid</strong>. Why does it give problems to connect?</p>

<pre><code>#include &lt;ESP8266WiFi.h&gt;

int num_ap; //number of access points 
String buffer_ssid = "empty"; //temporary stores the ssid in here because then it has to be converted in a char array
String buffer_pw = "empty"; //temporary stores password and same thing as^^
bool askPW = false; //"Did I already ask the password?"
bool connesso = false; //"Did I already connect?"


void setup()
{
  Serial.begin(9600);
  WiFi.mode(WIFI_STA); //put the nodemcu in sta mode so that it can connect to access points
  WiFi.disconnect();   //it disconnects from any access point it was connected to
  Serial.println("Setup completo"); 
  num_ap = WiFi.scanNetworks(); //scans for access points
  if (num_ap == 0)   //if there aren't any access points available
  {
    Serial.println("Nessun Access Point rilevato");
  }
  else //if there are some access points available
  {
    Serial.println("Access Point rilevati:");
    delay(200);
    for (int i = 0; i &lt; num_ap; i++)
    {
      Serial.println(" ");
      Serial.print(i + 1);
      Serial.print(") ");
      Serial.print(WiFi.SSID(i)); //print access point's ssid
      Serial.print(" ");
      Serial.print("(Potenza segnale: ");
      Serial.print(WiFi.RSSI(i)); //prints access point's rssi(strength of signal)
      Serial.print(")");
      delay(50);
    }
    delay(500);
    Serial.println(" ");
    Serial.println(" ");
    Serial.println("A quale Access Point ti vuoi connettere?");

  }
}



void loop()
{
  if (buffer_ssid == "empty") //if the variable has the standard value "empty" so if the user has already given an ssid. I need this to execute  this piece of code once
   {
    if (Serial.available() &gt; 0)
     {
      buffer_ssid = Serial.readString();  //stores user input(the ssid) in buffer_ssid
      if (buffer_ssid != "empty") //if the variable isn't empty(if the code has successfully saved the ssid) prints the ssid
       {
        Serial.println(buffer_ssid);
       }
     }
   }

  if(buffer_ssid != "empty") //if the user has given an ssid
  {
   if(askPW == false) //if the code has asked the password(I need this in order to execute this piece of code once)
    {
     Serial.println("Inserire la password per connettersi all'Access Point, se non serve premere spazio e invio"); //"insert the password"
     askPW = true;
    }
    if (Serial.available() &gt; 0)
    {
      buffer_pw = Serial.readString(); //saves user input(the password) in the variable buffer_pw
      if(buffer_pw != "empty") //if the code has successfully saved the password so it hasn't the standard value "empty"
      {
        Serial.println(buffer_pw);
      }
    }
  }

  if (buffer_pw != "empty" &amp;&amp; buffer_ssid != "empty") //if buffer_ssid and buffer_pw aren't "empty", so if the user inserted them
  {
    if(connesso == false) //in order to execute this piece of code once
    {
     //converts the strings buffer_ssid and buffer_pw in char arrays(I have to do this because WiFi.begin() doesn't accept strings)
     char ssid[buffer_ssid.length()];
     buffer_ssid.toCharArray(ssid,buffer_ssid.length());
     char password[buffer_pw.length()];
     buffer_pw.toCharArray(password,buffer_pw.length());
     //start connecting
     Serial.println("Connessione in corso");
     Serial.println(ssid); //prints ssid
     Serial.println(password);//prints password so I know that they were converted succesffully
     WiFi.begin(ssid,password);//start connecting
     while(WiFi.status() != WL_CONNECTED) //while it's not connected
     {
      Serial.print(".");
      delay(500); 
     }
      Serial.println("Connesso!"); //"Connected!"
      Serial.println("Il mio ip locale: ");
      Serial.print(WiFi.localIP()); //nodemcu's local IP

    }
    connesso = true;  //set connesso to true so that this piece of code doesn't loop
  }
}
</code></pre>
 37622: <p>Problem is that you set to <code>toCharArray</code> the length of the String. But the second parameter is the maximum allowed length of the output and there is then no space for the terminating zero and string is cut at last character. </p>

<p>The function toCharArray knows the size of the String. The second parameter should be the size of the char array <code>buffer_ssid.toCharArray(ssid, sizeof(ssid));</code></p>

<p>And next problem could be that you copy the CR/LF characters from input into ssid and password. You can see it in debug printouts.</p>
