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()
{}
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– Rangesh. s04/06/2020 06:40:13Commented 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– hcheung04/06/2020 09:42:06Commented Apr 6, 2020 at 9:42
println()
so you don't get everything on one line. I suspect your problem is that you have your line ending set toCR & NL
but you're only looking forNL
(10) so you get extra character 13s in your strings.