Skip to content

Update ESP8266WiFiSTAClass::waitForConnectResult() for the same behavior with ESP32. #4985

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
3 changes: 1 addition & 2 deletions cores/esp8266/WString.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,7 @@ class String {
void toLowerCase(void);
void toUpperCase(void);
void trim(void);

// parsing/conversion

long toInt(void) const;
float toFloat(void) const;

Expand Down
6 changes: 4 additions & 2 deletions libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,12 +369,14 @@ bool ESP8266WiFiSTAClass::getAutoReconnect() {
* returns the status reached or disconnect if STA is off
* @return wl_status_t
*/
uint8_t ESP8266WiFiSTAClass::waitForConnectResult() {
uint8_t ESP8266WiFiSTAClass::waitForConnectResult(uint32_t wait_secs) {
//1 and 3 have STA enabled
if((wifi_get_opmode() & 1) == 0) {
return WL_DISCONNECTED;
}
while(status() == WL_DISCONNECTED) {
int i = 0;
int try_times = wait_secs*10;
while((!status() || status() >= WL_DISCONNECTED) && ((wait_secs==0)? true : (i++ < try_times))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is difficult to read.

  • !status() tests whether the status is non-zero, but status is an enum and it’s unobvious which value is zero.
  • The part after && is really complicated.

How about:

for (int timeout = wait_secs * 10; wait_secs == 0 || timeout > 0; --timeout) {
  auto st = status();
  switch (st) {
    case WL_CONNECTED:
    case WL_NO_SSID_AVAIL:
    case WL_CONNECT_FAILED:
      return st;
    default:
      delay(100);
      break;
  }
}

  

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with @yoursunny . However:

I have a PR with a class that encapsulates the polled timeout. I suggest waiting a bit until it's merged, then using it, instead of reinventing the wheel for the nth time.
the switch should define cases for all enum values, and leave the default case for values not in the enum (error).

delay(100);
}
return status();
Expand Down
2 changes: 1 addition & 1 deletion libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class ESP8266WiFiSTAClass {
bool setAutoReconnect(bool autoReconnect);
bool getAutoReconnect();

uint8_t waitForConnectResult();
uint8_t waitForConnectResult(uint32_t wait_secs=0);

// STA network info
IPAddress localIP();
Expand Down