-
Notifications
You must be signed in to change notification settings - Fork 7.6k
WiFi.scanNetworks() not working on ESP32 and no viable workaround yet? #3294
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
Comments
After disconnecting from a station why are you again connecting? |
please decode your backtrace as I can't reproduce this behavior. |
@copercini @tablatronix @me-no-dev @tobozo for help on this issue they have already experienced. Yes, this clears out any previous connection to WiFi, then re-connects from 0 using WiFi.begin(). I use the disconnect function along with autoConnect and autoReconnect on ESP8266 and it works fine. The scanNetworks() example provided by WiFiscan.ino only has a disconnect() but the issue #758 states the reconnection to an unexisting WiFi network as a "workaround" to solve the scanNetworks() issue. In reality, the device is not connected to anything, that's why it has to scan for networks in the first place. But in this particular case of the ESP32, the fatal error is produced with or with this line added to the WiFiScan.ino program. |
@atanisoft Original Backtrace codeBacktrace: 0x4008c7e0:0x3ffbe160 0x4008ca11:0x3ffbe180 0x400da01b:0x3ffbe1a0 0x40081789:0x3ffbe1c0 0x40133552:0x3ffb5290 0x401335a1:0x3ffb52b0 0x40133a51:0x3ffb52d0 0x40130959:0x3ffb5370 0x40130c2e:0x3ffb53f0 0x40131696:0x3ffb5440 0x401324ec:0x3ffb5460 0x401329a4:0x3ffb5480 0x400d991e:0x3ffb5540 0x400d9bce:0x3ffb5570 0x4010d829:0x3ffb55a0 0x4010d92e:0x3ffb55d0 0x4010dc2a:0x3ffb5600 0x4010ddc7:0x3ffb5630 0x4008f8cc:0x3ffb5650 0x4008877d:0x3ffb5690 Backtrace decoded:Decoding stack results |
It isn't even getting to the wifi scan when it fails. It is very likely failing due to a bad partition table or corrupted entry in nvs. I'd suggest erase flash and reupload |
Ditto, also what lib version ? definitely erase NVS |
Reproduced, no crash but scan start 99% of the time this stuff breaks wifi
you cannot disconnect then begin, there are asynchronous things that happen ( which needs to be fixed probably ) add delays in there and I bet it works. |
ok looks like it has more to do with the ap not found background connecting maybe. [D][WiFiGeneric.cpp:336] _eventCallback(): Event: 5 - STA_DISCONNECTED
[W][WiFiGeneric.cpp:351] _eventCallback(): Reason: 201 - NO_AP_FOUND
[D][WiFiGeneric.cpp:336] _eventCallback(): Event: 0 - WIFI_READY
[D][WiFiGeneric.cpp:336] _eventCallback(): Event: 2 - STA_START
[D][WiFiGeneric.cpp:336] _eventCallback(): Event: 2 - STA_START
[D][WiFiGeneric.cpp:336] _eventCallback(): Event: 5 - STA_DISCONNECTED
[W][WiFiGeneric.cpp:351] _eventCallback(): Reason: 201 - NO_AP_FOUND
[D][WiFiGeneric.cpp:336] _eventCallback(): Event: 0 - WIFI_READY
[D][WiFiGeneric.cpp:336] _eventCallback(): Event: 2 - STA_START
[D][WiFiGeneric.cpp:336] _eventCallback(): Event: 2 - STA_START
[D][WiFiGeneric.cpp:336] _eventCallback(): Event: 5 - STA_DISCONNECTED
[W][WiFiGeneric.cpp:351] _eventCallback(): Reason: 201 - NO_AP_FOUND
[D][WiFiGeneric.cpp:336] _eventCallback(): Event: 0 - WIFI_READY
[D][WiFiGeneric.cpp:336] _eventCallback(): Event: 2 - STA_START
[D][WiFiGeneric.cpp:336] _eventCallback(): Event: 2 - STA_START
[D][WiFiGeneric.cpp:336] _eventCallback(): Event: 5 - STA_DISCONNECTED
[W][WiFiGeneric.cpp:351] _eventCallback(): Reason: 201 - NO_AP_FOUND
[D][WiFiGeneric.cpp:336] _eventCallback(): Event: 0 - WIFI_READY
[D][WiFiGeneric.cpp:336] _eventCallback(): Event: 2 - STA_START
[D][WiFiGeneric.cpp:336] _eventCallback(): Event: 2 - STA_START
scan done
no networks found
[D][WiFiGeneric.cpp:336] _eventCallback(): Event: 5 - STA_DISCONNECTED
[W][WiFiGeneric.cpp:351] _eventCallback(): Reason: 201 - NO_AP_FOUND
[D][WiFiGeneric.cpp:336] _eventCallback(): Event: 0 - WIFI_READY
[D][WiFiGeneric.cpp:336] _eventCallback(): Event: 2 - STA_START
[D][WiFiGeneric.cpp:336] _eventCallback(): Event: 2 - STA_START
[D][WiFiGeneric.cpp:336] _eventCallback(): Event: 5 - STA_DISCONNECTED
[W][WiFiGeneric.cpp:351] _eventCallback(): Reason: 201 - NO_AP_FOUND
[D][WiFiGeneric.cpp:336] _eventCallback(): Event: 0 - WIFI_READY
[D][WiFiGeneric.cpp:336] _eventCallback(): Event: 2 - STA_START
[D][WiFiGeneric.cpp:336] _eventCallback(): Event: 2 - STA_START |
SCAN_DONE event never fires |
Here you go
This error should be lowered in logging to a lower level as I think it occurs in SDK, so we ned to catch this |
Is this related? |
I have reuploaded the file on both chips several times. Is there any other way to clear the NVS? or do you mean format the chip somehow? |
Not sure. I cannot really get into scanning, an the espressif/esp-idf#2125 talks about AP not disconnecting after being powered off. |
you have to disconnect before scanning you can reconnect after
|
you should be checking int N for #define WIFI_SCAN_RUNNING (-1) int n = WiFi.scanNetworks();
Serial.println("scan done");
if(n == WIFI_SCAN_FAILED) Serial.println("Scan Failed!");
if (n = 0) {
Serial.println("no networks found");
} else if(n>0) {
Serial.print(n);
Serial.println(" networks found"); |
Ok, but I think the fatal error occurrs before being able to produce any meaningful output (reaching the WiFi.scanNetworks() line). Added the code to search for scan failed and result is the same. Backtrace is: Decoding stack results |
Yeah I cannot reproduce that part. |
@atanisoft |
If you skip the call to WiFi.begin() it should work. It is 100% untrue that you must disconnect() for scanNetworks() to work. The only part that is critical is that you are not actively connecting to an SSID as the scan will be aborted and an error returned. |
If you are connecting a non existent ap then yeah you have to disconnect first.. I thought that was what we are talking about??? Because it is always connecting.. |
If you are trying to connect to a network (existing or otherwise) you WILL need a disconnect IF the connection process has not completed. If you skip the call to WiFi.begin() entirely then it won't be trying to connect to a network and therefore the WiFi.scanNetworks() call WILL work as expected. The problem with the code in this issue is that it is forcing a connect to a non-existing network and after 10seconds (default timeout inside WiFi.begin() call) it is still not connected and it keeps trying to connect in the background since it can't find the requested network. Therefore the call to WiFi.scanNetworks() WILL fail and SHOULD return an error code accordingly, since the code does no error checking whatsoever (WiFi.begin() will return the connection status!) the code is missing the returned error codes. The for loop in loop() is being short circuited as the return code from WiFi.scanNetworks() is less than zero (ref). The loop() code is only explicitly checking for ZERO networks found and otherwise assuming that something was found which is incorrect since the scan request failed. |
I skip the WiFi.begin() and just leave the disconnect(), skip the WiFimode as well, skip everything including WiFi.disconnect(), and the results are the same. The system aborts before I can get an error code. This code below considers the following conditions: I re-arranged the example loops for more clarity. You can check that the -2 condition is inspected on the 1st loop therefore there shouldn't be an exception thrown for negatives. The backtrace is attached at the end:
I even removed the if conditionals altogether, same situation. See this code:
EXCEPTION DETAILscan start Backtrace: 0x4008c7e0:0x3ffbe160 0x4008ca11:0x3ffbe180 0x400d9e4f:0x3ffbe1a0 0x40081789:0x3ffbe1c0 0x40133386:0x3ffb5290 0x401333d5:0x3ffb52b0 0x40133885:0x3ffb52d0 0x4013078d:0x3ffb5370 0x40130a62:0x3ffb53f0 0x401314ca:0x3ffb5440 0x40132320:0x3ffb5460 0x401327d8:0x3ffb5480 0x400d9752:0x3ffb5540 0x400d9a02:0x3ffb5570 0x4010d65d:0x3ffb55a0 0x4010d762:0x3ffb55d0 0x4010da5e:0x3ffb5600 0x4010dbfb:0x3ffb5630 0x4008f8cc:0x3ffb5650 0x4008877d:0x3ffb5690 ###STACK DECODING |
You must call WiFi.mode() to setup the call to scanNetworks(). Also you need to check for the other error code as well. Basically if scanNetworks () returns less than zero an error occurred. Also your backtrace points to a wifi system init failure. Possibly power related even. How are you powering the esp32? It will require at least 600mA during wifi init. |
@atanisoft
You may have a point on power. I am using a PC serial port to power the ESP32, but before I had it hooked to a Lab regulated DC Power supply at the Vin and 6.5V of power, and was doing the same. |
@atanisoft I damaged the chip going up to 11 volts on the power using the ESP32's +5V input. I know, I took the risk. Had another one available so I gave it a go. What I am sure at this point its that the WiFi.h has a problem to run on EPS32. The ESP32 chip runs fine if I disable everything but WiFi.disconnect(). As soon as I use WiFi.begin(), WiFi.mode() or WiFi.scan Networks() it starts faulting faulting like crazy. Setup()WiFi.begin() or WiFi.mode(WIFI_STA) <-- first LED 50 blink test at setup() doesn't happen Loop()WiFi.scanNetworks() <-- Everything falls apart when this method is reached..... @me-no-dev @tablatronix @copercini @tobozo The problem in a nutshell: can't run WiFi.scanNetworks on ESP32.Setttings/Code/Stack trace added below.
Decoding stack results
|
Geez... I was looking at the last pictures I posted and I found the problem. Can you guys see what it is?I'll post it tomorrow in case anyone else wants to point it out. The module is working fine now.... |
I wonder if it is useful or even practical to have guards around WiFi.begin and setCpuFrequencyMHz to give some debug or error around this? |
@lbernstone I think it would be a good idea to add guards for that. It will save a lot of headaches of odd failures like this one. |
Do not use the dev board definition as the default for esp32, its is asking for trouble, too many options, I use the doit module |
This is an old issue but hope this still helps anyone, it is used right after the ESP32 begins. It just works the first time but its enough for me. I run the following code and works 100% of the times:
WiFi.disconnect( true ); |
I was going to test this systematically since I had 30+ aps, and my work replaced all aps and removed 2.4ghz.. shrug I have been changing my channel and change bandwidth to 20mhz and set country properly, you can try changing mode and power also , having much better luck at everything esp32 |
I have a nicely working captive portal that starts automatically when no wifi connection could be made. That means that a failed wifi.begin() preceded this, and that prevents the scanning. It also means that i cannot use wifi.disconnect() because when a grid powerfailure occurs, the esp starts up and cannot connect because the (slow) router has not been started yet. So it enters the accesspoint mode. After a few minutes the portal times out and then a reboot follows so that it can still connect when the router is "in the air". This would not work when there was a wifi.disconnect executed. So what i did was when entering the portal:
This seems to stop the processes that prevent the scanning. |
Thats interesting observation, so scannetworks is failing if wifi begin fails? But what is your failure mode? SSID_not avail? or some other bug like WL_DISCONNECT failure to assoc etc. I cannot think of anything that would be breaking scannetworks on assoc failure. Seems easy enough to test, ill mess with it |
Implemented command Connect (0x0d = 13), i.e. set WiFi credentials based on the index of the network in the last scan result and the passphrase supplied by the user. For this is was necessary to refactor Connection and the Scan command, since scanning can not be done while connection attempts are being made, and we formerly always tried to reconnect when receiving a disconnect event. It was somewhat pointless to keep trying to reconnect forever if the connection kept failing due to wrong credentials as well. Thus we now actively manage the automatic reconnect in Connection in the member variable m_reconnecting, and only set it to true once a connection is actually established. The disconnect() method now disconnects and sets m_reconnecting to false, so it's an explicit request to disconnect and *not* try to reconnect until a successive connect() call results in an actual connection. Implemented scanNetworks() in Connection to first disconnect(), call WiFi.scanNetworks() and then connect() again to make sure scanning for networks always works. There seem to be some quirks related to scanning and multiple contradicting workarounds are suggested here: espressif/arduino-esp32#3294 These workarounds involve manual delays, which seems a bit voodoo, so I have decided that simply disconnecting before a scan and reconnecting again once it is done is the most pragmatic approach here. Scan: Now use connection->scanNetworks() instead of directly calling WiFi.scanNetworks(). Also added responding with "no networks found" like the previous firmware does, additionally logging whether the scan failed in the first place or whether it succeeded and there are really no networks available (see comment). Data: Added method field(index) to access 0x01-separated fields. This is the equivalent to the getValue()-function in th previous firmware. Also renamed appendSeparated() to appendField().
Hardware:
Board: ESP32 Dev Module
Core Installation/update date:?
IDE name: Arduino IDE 1.8.5
Flash Frequency: 80Mhz
PSRAM enabled: no
Upload Speed: 921600
Computer OS: Windows 7 Ultimate SP1
Description:
Hi all,
I have bumped into a major problem in my project. When trying the WiFi.scanNetworks() operation it crashes the entire system. I tested it using the following chips:
ESP-WROOM-32
ESP-WROOM-32U
Before someone suggests visiting an existing thread/topic, I have visited all these and no one seems to have a clear workaround, plus all these are closed topics:
#758 Closed
#242 Closed
#791 Closed
#918 Closed
#1617 Closed
The closest work around is to run WiFiscan.ino adding a couple of lines of code that actually disconnects Wifi then calls a non-existent AP, but for me it doesn't work at all:
Sketch:
I keep getting a fatal watchdog error on core 0:
Debug Messages:
Please let me know if someone has tried a different workaround than the ones stated on the previous issues. I find it incredible that such a nice chip as the ESP32 has this major flaw that hinders its potential. I can get nothing from the Espressif forums on this, so I presume this is not a major problem for most people, the previous workarounds work fine for the majority, nobody uses the scan function (really?), or I am doing someting wrong.
I see from the previous postings that the issue started with an update on the libraries of the ESP and also that the threads go back to 2017 so its pretty worrisome that nobody is talking of this issue in 2019.
Thanks in advance
-EZ
The text was updated successfully, but these errors were encountered: