espressif / arduino-esp32

Arduino core for the ESP32
GNU Lesser General Public License v2.1
13.31k stars 7.35k forks source link

[2.0.2] softAP unstable when performing a WiFi.scanNetworks() - Windows 10 clients get disconnected. #4554

Closed gonzabrusco closed 2 years ago

gonzabrusco commented 3 years ago

Hardware:

Board: ESP32 Dev Module Core Installation version: 1.0.4 and 1.0.5 RC1/RC2 IDE name: Platform.io Flash Frequency: 40Mhz PSRAM enabled: no Upload Speed: 115200 Computer OS: Windows 10 and Android 9

Description:

softAP gets unstable when performing a wifi scan in latest 1.0.5 RC1 and RC2. In version 1.0.4 this can be done without problems. On 1.0.5 RC1/RC2 the AP goes down while scanning. This happens when I'm connected to the AP with a PC with Windows 10 (I tried two different laptops with Windows 10 and both have the same problem). Curiously, on two Android phones this worked on both versions.

Sketch:

The following is the WiFi AP example with minor modifications for showing scan results. This code compiled against 1.0.4 works perfectly but compiled against 1.0.5 RC1/RC2 does not work. For compiling I use PlatformIO but this issue happens also with Arduino IDE.

#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>

#define LED_BUILTIN 2   // Set the GPIO pin where you connected your test LED or comment this line out if your dev board has a built-in LED

// Set these to your desired credentials.
const char *ssid = "helloworld";
const char *password = "";

WiFiServer server(80);

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);

  Serial.begin(115200);
  Serial.println();
  Serial.println("Configuring access point...");

  // You can remove the password parameter if you want the AP to be open.
  WiFi.softAP(ssid, password);
  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);
  server.begin();

  Serial.println("Server started");
}

void loop() {
  WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,
    Serial.println("New Client.");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            // the content of the HTTP response follows the header:
            client.print("Starting scan now<br>");

            uint8_t numSsid = WiFi.scanNetworks(); // Scanning Networks
            client.printf("Number of available WiFi networks discovered: %d <br>", numSsid);
            for (int i = 0; i < numSsid; i++) {
              client.printf("%d: %s, Ch:%d (%ddBm) <br>", i + 1, WiFi.SSID(i).c_str(), WiFi.channel(i), WiFi.RSSI(i));
            }

            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;
          } else {    // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
      }
    }
    // close the connection:
    client.stop();
    Serial.println("Client Disconnected.");
  }
}

Debug Messages:

No error message. The PC gets disconnected and does not receive the rest of the webpage (the scan results).

me-no-dev commented 3 years ago

I have the feeling that this is caused by some new changes to the WiFi subsystem and scan parameters. Looks like briefly the radio was switched to other channels (so it can scan them) and computer detected that as disconnect. I will ask internally if someone has a clue how to get around this.

gonzabrusco commented 3 years ago

@me-no-dev any news about this?

me-no-dev commented 3 years ago

unfortunately nothing back yet. There are some updates that I will push probably tomorrow. Check back in RC3 :) I'll poke my colleagues again monday

gonzabrusco commented 3 years ago

I'm sorry to inform that the problem persists in RC3. When the scanning starts my Windows 10 PC gets disconnected from the AP.

YouDONG-ESP commented 3 years ago

Hi @gonzabrusco, thanks for reporting. Could you tell me, on the PC side, is there anything else you did besides connecting to wifi?

gonzabrusco commented 3 years ago

No. Just connect and browse to 182.168.4.1.

gonzabrusco commented 3 years ago

RC4. This keeps happening. Sorry. When a Windows 10 connect to the AP and the ESP32 starts the scan, the AP gets unstable and the Windows PC gets disconnected.

YouDONG-ESP commented 3 years ago

@gonzabrusco We had found the root cause: while scanning, our devices will be in different channels, causing AP to not send beacon when not in the home channel. We are discussing solutions. A temporary workaround is to Reduce the time spent on each channel WiFi.scanNetworks(false,false,false,100U);

gonzabrusco commented 3 years ago

Sorry but this still keeps happening. Even with 100u scan time between channels. With latest master as of today (RC6).

RafigRzayev commented 3 years ago

@YouDONG-ESP Could you please give approximation regarding when it will be fixed? I also have the same problem. Tried with 50, still got disconnection. I also use Windows 10, Arduino RC6

YouDONG-ESP commented 3 years ago

@RafigRzayev This problem is pretty complicated to fix. We don't have a perfect solution, the best fix we found is let esp32 goback to home channel after finish scanning each channel, but it will also make scan finish slower. We are doing test to see if it will affect the use.

RafigRzayev commented 3 years ago

@YouDONG-ESP Thank you for informing. @gonzabrusco Can you try WiFi.scanNetworks(false, false, true, 100) instead of WiFi.scanNetworks(false, false, false, 100). After switching to passive scanning my connection seems ok at the moment.

gonzabrusco commented 3 years ago

I can confirm this is still happening with latest framework version 1.0.6

stale[bot] commented 3 years ago

[STALE_SET] This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

stale[bot] commented 3 years ago

[STALE_DEL] This stale issue has been automatically closed. Thank you for your contributions.

gonzabrusco commented 2 years ago

I can confirm this issue still exists at 2.0.2. @me-no-dev @YouDONG-ESP

VojtechBartoska commented 2 years ago

@gonzabrusco Reopening and adding to the Roadmap. We will validate it. Thank you.

PilnyTomas commented 2 years ago

Hi, I just tried the sketch on ESP32-S2 (WROVER module) using Arduino IDE 1.8.15 I tried it with the following systems and all of them worked: Linux Fedora 34 64bit on ThinkPad E15 Android 9 on LG G6 iPhone 13 Windows 10 on HP EliteBook 855 @gonzabrusco do you still have those issues?

gonzabrusco commented 2 years ago

Sorry for the late response. This still happen to me but ONLY with laptops. With cellphones and with a desktop PC with a WiFi pci-e adapter, works fine. But with two different Laptops, this issue exists. Try it a couple of times, repeatedly, from a laptop, and you should see it.

PS: My board is an ESP32 devkit (ESP-WROOM-32)

PilnyTomas commented 2 years ago

Hi, I just tried it with WROOM-32D + laptop with Linux and it works ok for me. Tried to disconnect/connect and wait for a while. Cycled a few times. The connection was not lost. How far is your ESP from the computer? Same table/room, or somewhere further? Maybe the signal is weak. Do you have the ESP connected directly to the computer/power source (without USB hubs)? Maybe it is power related problem. To be honest I don't know what could be causing it.

gonzabrusco commented 2 years ago

Did you try with Windows 10?

This issue was reproduced by me an another collegue in remote locations with different hardware (different laptops, different devkits but same ESP32 version). Always connected to the USB of the laptop, very close to it.

PilnyTomas commented 2 years ago

Hi, I tried it with Win 10 and Win 7 using ESP32 WROVER-B and WROOM-32D and everything was ok. I'm sorry but I am unable to replicate your issue.

VojtechBartoska commented 2 years ago

Closing this one as we cannot Replicate it, if needed you can reopen it. Thanks!

gonzabrusco commented 2 years ago

Too bad this is happening only on my side. I will try another devkits. Maybe it's something about this ESP32. Thanks for taking your time to test this @PilnyTomas

VojtechBartoska commented 2 years ago

@gonzabrusco Good idea, it can be related to specific HW issue.

everslick commented 2 years ago

Just FYI, to whomever it is concerned: The issue seems to be the same, when the configured SSID is unavailable or out of range. Connecting to the ESP32 via softAP is practically impossible in that case, which IMHO is a common failure MODE, that SHOULD be handled by the WiFi stack, but isn't. The only workaround I know of is to call WiFi.enableSTA(false); if you detect that scenario and re-enable STA when the softAP gets disabled again.

Hichiii commented 2 years ago

I have the same problem with a c3 mini 1.

As soon as WiFi.scanNetworks() is executed, the Windows laptop flies out of the AP. It works with Android phones.

I have the WiFi.scanNetworks(false,false,false,100U); and WiFi.scanNetworks(false,false,true,100U);

tries.

But it doesn't help. is there a solution now?

I want to run Tasmote on the ESP32 c3. there WiFi.scanNetworks(); used

lg Hichi

roysG commented 1 year ago

In our side, We are using in ESP32, and run scanWifi and SoftAP to allow other device detect the device.

Sometime the softAP not start without any possible to detect it!

Is there any solution for this problem?

It looks that it hapenning when scanNetwork start before the softAP...

kapyaar commented 1 year ago

I faced the same issue on Windows 11 with a standard ESP32 Dev Module. Original scanNetwork was taking around 6-8 seconds, and windows will drop the connection and jump back to regular wifi.

For those who still face the issue, WiFi.scanNetworks(false,false,true,100U); helped in my case. Though it was still borderline with 100U. I reduced this to 50, and started showing better results. After playing around for a bit, I settled in at 75U. YMMV.

trullock commented 1 year ago

I have this exact problem with my Android client disconnecting from the SoftAP when doing a WiFi scan. I feel like the AP is stopping because it doesn't appear in the SSID list on my phone briefly during this time...

italocjs commented 6 months ago

WiFi.scanNetworks(false,false,true,75U); did help.

I think the core issue may be related to windows 10/11 testing the connection, with android phones the connections is very stable, the issue only seems to happen on windows. I tried to add an dns responder but did not see any improvements (or i didnt made it right, i'm new at wifi / web stuff)