0

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.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.print(ssid);
Serial.print("donessid");

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


  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print("Connecting....");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}
void loop()
{}
  • And what results do you actually get on the serial monitor? – Majenko Apr 4 at 13:10
  • Hi Majenko, I got a result like this image.see this screenshot.. imgdb.in/gJ6K – Rangesh. s Apr 4 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 at 13:55
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.

| improve this answer | |
  • Thanks hcheung.. it worked. – Rangesh. s Apr 6 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 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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