So here is the well-known WiFi connection code:
if (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, pass);
}
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
}
Serial.print("Connected...");
The problem is if it can't connect to the hotspot for some reason, it will end up in an infinite while loop. I was thinking on some solution with the millis() function, but then I found the WiFi.waitForConnectResult() fuction and I came up with this:
if (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, pass);
}
if (WiFi.waitForConnectResult() == WL_CONNECTED) {
Serial.println("connected");
}
else {
Serial.println("not connected");
}
Is this a good solution, to avoid infinite loop? If the ESP can't connect, it still can do the other things which does not need WiFi.