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

Conversation

ShinDaiIchi
Copy link

Add counter i for trying WIFi-connection 100 times

@d-a-v
Copy link
Collaborator

d-a-v commented Jul 30, 2018

Ref: #4983

You seem to have committed something else along with this proposal. Please do another PR for this.

To not break very precious backward compatibility,
what do you think of adding an optional parameter that would not change behavior for existing sketches ?

uint8_t ESP8266WiFiSTAClass::waitForConnectResult(uint8_t maxSeconds = 0) { // 0 is infinity

You'd propose the same fix to esp-idf with 10 instead of 0.
@me-no-dev what do you think ?

@@ -781,6 +781,57 @@ void String::trim(void) {
buffer[len] = 0;
}

/************************************************************/
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please remove this String addition.
On a personal note, the implementation makes me want to tear my eyes out. Why didn't the author use a C++ reference instead of a double pointer?

Copy link
Author

Choose a reason for hiding this comment

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

Sorry, I just tried something on String addition but not for this PR.
Already remove them.

@@ -237,7 +237,8 @@ class String {
void toLowerCase(void);
void toUpperCase(void);
void trim(void);


int td_split(String delimiter, String** str_array); // v2.3 split String to String Array by multi-delimiters
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please remove this change.

Copy link
Author

Choose a reason for hiding this comment

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

Already removed this String addition change.

@@ -374,7 +374,8 @@ uint8_t ESP8266WiFiSTAClass::waitForConnectResult() {
if((wifi_get_opmode() & 1) == 0) {
return WL_DISCONNECTED;
}
while(status() == WL_DISCONNECTED) {
int i = 0;
while((!status() || status() >= WL_DISCONNECTED) && i++ < 100) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a max of 10s time. Some connections could take longer, especially under poor signal conditions. Some users could not want to spend so much time trying to connect and want to give up sooner. I suggest taking an argument that defaults to this when not set.

Copy link
Author

Choose a reason for hiding this comment

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

If default time trying to connect when not set is 15 secs. What do you think ?

Copy link
Collaborator

@d-a-v d-a-v Jul 30, 2018

Choose a reason for hiding this comment

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

What would happen with a default non infinite value when user's network connectivity / the router is for any reason powered down or disconnected for a while (a longer while than this default value), or takes long to power on, or whatever ?

My point is that usually sketches patiently wait for connectivity. With this change, all sketches would need to be updated to loop on the connectivity loop.

I don't question this PR, only the historical default behavior / backward compatibility.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oops @d-a-v is correct: either we maintain backwards compatibility and merge now, or we keep this breaking change and wait until release 3.0.0 to merge. I prefer the former.
So:

  1. the function should take a timeout argument, in ms, which defaults to some value that disables the timeout (std::numeric_limits?)
  2. If the user wants a timeout, he can specify a value for the argument.
  3. In a future release we'll change the default to some value like 10 or 15s.

@ShinDaiIchi Could you please implement the changes and update the PR?

Copy link
Author

Choose a reason for hiding this comment

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

Already update with WiFi connecting's wait-timeout with backwards compatibility.

@@ -374,7 +374,8 @@ uint8_t ESP8266WiFiSTAClass::waitForConnectResult() {
if((wifi_get_opmode() & 1) == 0) {
return WL_DISCONNECTED;
}
while(status() == WL_DISCONNECTED) {
int i = 0;
while((!status() || status() >= WL_DISCONNECTED) && i++ < 100) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Oops @d-a-v is correct: either we maintain backwards compatibility and merge now, or we keep this breaking change and wait until release 3.0.0 to merge. I prefer the former.
So:

  1. the function should take a timeout argument, in ms, which defaults to some value that disables the timeout (std::numeric_limits?)
  2. If the user wants a timeout, he can specify a value for the argument.
  3. In a future release we'll change the default to some value like 10 or 15s.

@ShinDaiIchi Could you please implement the changes and update the PR?

Copy link
Collaborator

@devyte devyte left a comment

Choose a reason for hiding this comment

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

Minor changes requested. Will merge once fixed.

//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)? (i++ < try_times): true )) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

don't use inverted logic please:
... && ((wait_secs==0) ? true : (i++ < try_times) )

//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;
Copy link
Collaborator

Choose a reason for hiding this comment

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

PLease declare/init try_times on its own line

Copy link
Author

@ShinDaiIchi ShinDaiIchi left a comment

Choose a reason for hiding this comment

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

Update

@@ -374,7 +374,8 @@ uint8_t ESP8266WiFiSTAClass::waitForConnectResult() {
if((wifi_get_opmode() & 1) == 0) {
return WL_DISCONNECTED;
}
while(status() == WL_DISCONNECTED) {
int i = 0;
while((!status() || status() >= WL_DISCONNECTED) && i++ < 100) {
Copy link
Author

Choose a reason for hiding this comment

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

Already update with WiFi connecting's wait-timeout with backwards compatibility.

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).

@devyte
Copy link
Collaborator

devyte commented Nov 6, 2019

@ShinDaiIchi the polledTimeout class has been available for a while now. Could you please resolve conflicts and update with that?

@earlephilhower earlephilhower added the merge-conflict PR has a merge conflict that needs manual correction label Dec 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merge-conflict PR has a merge conflict that needs manual correction
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants