- 
                Notifications
    You must be signed in to change notification settings 
- Fork 7.7k
Description
Hardware:
Board:							ESP32-WROOM-32 Dev Module
Core Installation version:				1.0.1 (installed using board manager feb/23)
IDE name:						Arduino IDE 1.88
CPU Frequency:					80MHz
Flash Frequency:					40Mhz
Flash Mode:						QIO
Flash Size:						2MB (16MB)
Partition Scheme:					Standard
PSRAM enabled:					no
Upload Speed:						921600
Computer OS:						Windows 10
Description:
With a portable device I need to ensure that WiFi connection is beeing established as soon as the router is in range. Also I need to do some stuff as soon the connection is established or lost (in my sketch below just a serial output).
I have the problem that "WiFi.begin(ssid, pass)" works only every second time. If it doesn't work, I have to press the reset button and then it works. If I press reset again it won't work. Pressing reset again and it works, and so on.
If it doesn't work I get the following output with Debug Level set to verbose:
[W][WiFiGeneric.cpp:357] _eventCallback(): Reason: 202 - AUTH_FAIL
My router is a Fritz! Box 6490 cable and even though I've checked the router's options I can't find anything wrong. Regarding to familiar posts I even have tried with a regulated power supply at 5V (current limit 2.5A) - but no change.
I came up with a workaround with a task checking the connection and that works pretty well. In case of "WiFi.status" turns to "WL_CONNECT_FAILED" I need to call "WiFi.disconnect(true)" and a bit later "WiFi.begin" again.
About the whole working sketch below I have several questions:
- Is that "works/doesn't work" behaviour a firmware bug, a bug in arduino or this SDK or a router problem?
- In my sketch I'm using "xTaskCreatePinnedToCore" for the connection watching task. How large does the stack for that task need to be (seems to work with 8kB) and what priority does such a task need to have (I just took 3 to be higher than IDLE)?
- I never got "WL_CONNECTION_LOST" as Wifi status. Do I need to consider that? And if so, how?
Please advise! Thank's!
Sketch:
#include <WiFi.h>
const char* ssid = "...";
const char* password = "...";
bool myWiFiFirstConnect = true;
void myWiFiTask(void *pvParameters) {
  wl_status_t state;
  while (true) {
    state = WiFi.status();
    if (state != WL_CONNECTED) {  // We have no connection
      if (state == WL_NO_SHIELD) {  // WiFi.begin wasn't called yet
        Serial.println("Connecting WiFi");
        WiFi.mode(WIFI_STA);
        WiFi.begin(ssid, password);
      } else if (state == WL_CONNECT_FAILED) {  // WiFi.begin has failed (AUTH_FAIL)
        Serial.println("Disconnecting WiFi");
        WiFi.disconnect(true);
      } else if (state == WL_DISCONNECTED) {  // WiFi.disconnect was done or Router.WiFi got out of range
        if (!myWiFiFirstConnect) {  // Report only once
          myWiFiFirstConnect = true;
          Serial.println("WiFi disconnected");
        }
      }
      vTaskDelay (250); // Check again in about 250ms
    } else { // We have connection
      if (myWiFiFirstConnect) {  // Report only once
        myWiFiFirstConnect = false;
        Serial.print("Connected to ");
        Serial.println(ssid);
        Serial.print("IP address: ");
        Serial.println(WiFi.localIP());
        Serial.println("");
      }
      vTaskDelay (5000); // Check again in about 5s
    }
  }
}
void setup() {
  delay(1000); // Power up
  Serial.begin(115200);
  // Create a connection task with 8kB stack on core 0
  xTaskCreatePinnedToCore(myWiFiTask, "myWiFiTask", 8192, NULL, 3, NULL, 0);
}
void loop() {
}Debug Messages:
If WiFi.begin works:
[D][WiFiGeneric.cpp:342] _eventCallback(): Event: 0 - WIFI_READY
[D][WiFiGeneric.cpp:342] _eventCallback(): Event: 2 - STA_START
....[D][WiFiGeneric.cpp:342] _eventCallback(): Event: 4 - STA_CONNECTED
[D][WiFiGeneric.cpp:342] _eventCallback(): Event: 7 - STA_GOT_IP
[D][WiFiGeneric.cpp:385] _eventCallback(): STA IP: 192.168.178.21, MASK: 255.255.255.0, GW: 192.168.178.1
If it doesn't work:
[D][WiFiGeneric.cpp:342] _eventCallback(): Event: 0 - WIFI_READY
[D][WiFiGeneric.cpp:342] _eventCallback(): Event: 2 - STA_START
[D][WiFiGeneric.cpp:342] _eventCallback(): Event: 5 - STA_DISCONNECTED
[W][WiFiGeneric.cpp:357] _eventCallback(): Reason: 202 - AUTH_FAIL
Metadata
Metadata
Assignees
Labels
Type
Projects
Status