Serial read SSID/PASS for wifi connection (fail)

Hello,
I have a simple code, in which I use serial monitor to enter ssid and pass for wifi ant then I try to connect using these values. Reading is fine, it prints back what I enter, but the wifi just can not connect… Im using esp8266 nodemcu board, but Arduino IDE and Arduino language. See code bellow:

#include <ESP8266WiFi.h>

#define BAUDRATE 9600

char ssid[50];
char pass[50];

void setup() {
  Serial.begin(BAUDRATE);
  Serial.println();
  Serial.print("Enter your WiFi credentials.\n");
  Serial.print("SSID: ");
  while (Serial.available() == 0) {
    // wait
  }
  Serial.readBytesUntil(10, ssid, 50);
  Serial.println(ssid);

  Serial.print("PASS: ");
  while (Serial.available() == 0) {
    // wait
  }
  Serial.readBytesUntil(10, pass, 50);
  Serial.println(pass);

  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(200);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {

}

There must be problem in reading or could be there a extra character somewhere? IDK, Please help.

Can you check how many bytes were actually read into ssid and pass? Print the characters one by one in ASCII to see if you have not captured a CR since you read till LF (is your console set up to send CR/LF each time?)

There must be problem in reading or could be there a extra character somewhere?

You need to answer that question. Some reasonable effort at printing the data would be useful.

Serial.print("pass: [");
Serial.print(pass);
Serial.println("]");

If you see: pass: [whateverYourPasswordIs] you know one thing.

If you see: pass: [whateverYourPasswordIs ] you know another thing.

Just printing pass by itself tells you a lot less.

Well, thank you for ideas.
The

Serial.print("pass: [");
Serial.print(pass);
Serial.println("]");

prints out fine.

When I set Serial monitor window to both NL & CR it does not work, if I set up only newline, it works.

I wonder why tho… I’m planning to use this with some C# app so I want to understand this.

Any ideas how to control this?

When I set Serial monitor window to both NL & CR it does not work, if I set up only newline, it works.

Suppose you set the line ending to CR/NL, and send “1”. What is actually sent, then, is “1”. When you read everything up to the NL, the string contains “1”. When you then compare that to “1”, of course they do not match.

When you set the line ending to NL only, , and send “1”. What is actually sent, then, is “1”. When you read everything up to the NL, the string contains “1”. When you then compare that to “1”, of course they match.

Which brings us back to my post #1

Solutions - you don't send CR/LF - instead of reading till LF (10) you read till CR (13) and get rid of the following data in the serial buffer to prepare for the next read - you keep reading it like you do and clean up your string and replace the CR which is at the end by '\0' - you don't use readBytesUntil and implements your own Serial handling code

Plenty of options !

What about just hard coding the SSID and password in your sketch as a first step in problem solving. If even that fails, the problem lies elsewhere.

6v6gt:
What about just hard coding the SSID and password in your sketch as a first step in problem solving.
If even that fails, the problem lies elsewhere.

I cant hard code it. That the point :slight_smile:
With hard coded ssid/pass it work of course.
With newline setting in SM it works, Ill implement @J-M-L ideas to make it more “general”.

Now I have problem with the C# application sending these data. But that’s for another topic.

Thx guys.

jevniky: I cant hard code it. That the point :) With hard coded ssid/pass it work of course. With newline setting in SM it works, Ill implement @J-M-L ideas to make it more "general".

Now I have problem with the C# application sending these data. But that's for another topic.

Thx guys.

The suggestion of hard coding the credentials was intended only to isolate the problem, and if you had already got that far, it would have been ideal if you had mentioned it in the OP.

Incidentally, the normal way of getting WLAN credentials into an ESP8266 is through a web browser, with the ESP8266 temporarily in "Access Point" mode and running a web server. Something like this makes it quite painless https://tzapu.com/esp8266-wifi-connection-manager-library-arduino-ide/

6v6gt: Incidentally, the normal way of getting WLAN credentials into an ESP8266 is through a web browser, with the ESP8266 temporarily in "Access Point" mode and running a web server. Something like this makes it quite painless https://tzapu.com/esp8266-wifi-connection-manager-library-arduino-ide/

Thx for the idea! Ill try it out.