Trying to get Wifi SSID and password dynamically from serial monitor and connecting to WiFi, tried the below program, but wifi is always in Connecting state only, it is not getting connected after receiving input from serial monitor.

#include <WiFi.h>

#define BAUDRATE 115200

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

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

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

  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
void loop()
  • And what results do you actually get on the serial monitor?
    – Majenko
    Apr 4, 2020 at 13:10
  • Hi Majenko, I got a result like this image.see this screenshot.. imgdb.in/gJ6K
    – Rangesh. s
    Apr 4, 2020 at 13:52
  • You really should learn to use println() so you don't get everything on one line. I suspect your problem is that you have your line ending set to CR & NL but you're only looking for NL (10) so you get extra character 13s in your strings.
    – Majenko
    Apr 4, 2020 at 13:55

1 Answer 1


Is your Serial Monitor setting set to "Both NL and CR"? Change it to "Newline" should works.

What happened is if your Serial Monitor has a setting of "Both NL and CR", when your press Enter, it generates a \r\n, the Serial.readBytesUntil(10, ssid, 50) picked up that \r at the end of the array, when you use it as SSID, it is not a valid SSID.

  • Thanks hcheung.. it worked.
    – Rangesh. s
    Apr 6, 2020 at 6:40
  • Glad I this help. Appreciated if you could accept the answer by click on the tick on the left side of the answer.
    – hcheung
    Apr 6, 2020 at 9:42

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.