{"action":"created","issue":{"url":"https://api.github.com/repos/espressif/arduino-esp32/issues/2501","repository_url":"https://api.github.com/repos/espressif/arduino-esp32","labels_url":"https://api.github.com/repos/espressif/arduino-esp32/issues/2501/labels{/name}","comments_url":"https://api.github.com/repos/espressif/arduino-esp32/issues/2501/comments","events_url":"https://api.github.com/repos/espressif/arduino-esp32/issues/2501/events","html_url":"https://github.com/espressif/arduino-esp32/issues/2501","id":413808401,"node_id":"MDU6SXNzdWU0MTM4MDg0MDE=","number":2501,"title":"ESP32 WiFi.begin works only every second time - workaround","user":{"login":"DIRR70","id":37394644,"node_id":"MDQ6VXNlcjM3Mzk0NjQ0","avatar_url":"https://avatars1.githubusercontent.com/u/37394644?v=4","gravatar_id":"","url":"https://api.github.com/users/DIRR70","html_url":"https://github.com/DIRR70","followers_url":"https://api.github.com/users/DIRR70/followers","following_url":"https://api.github.com/users/DIRR70/following{/other_user}","gists_url":"https://api.github.com/users/DIRR70/gists{/gist_id}","starred_url":"https://api.github.com/users/DIRR70/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/DIRR70/subscriptions","organizations_url":"https://api.github.com/users/DIRR70/orgs","repos_url":"https://api.github.com/users/DIRR70/repos","events_url":"https://api.github.com/users/DIRR70/events{/privacy}","received_events_url":"https://api.github.com/users/DIRR70/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":19,"created_at":"2019-02-24T10:49:26Z","updated_at":"2019-10-31T17:28:22Z","closed_at":null,"author_association":"NONE","body":"### Hardware:\r\nBoard:\t\t\t\t\t\t\tESP32-WROOM-32 Dev Module \r\nCore Installation version:\t\t\t\t1.0.1 (installed using board manager feb/23)\r\nIDE name:\t\t\t\t\t\tArduino IDE 1.88\r\nCPU Frequency:\t\t\t\t\t80MHz\r\nFlash Frequency:\t\t\t\t\t40Mhz\r\nFlash Mode:\t\t\t\t\t\tQIO\r\nFlash Size:\t\t\t\t\t\t2MB (16MB)\r\nPartition Scheme:\t\t\t\t\tStandard\r\nPSRAM enabled:\t\t\t\t\tno\r\nUpload Speed:\t\t\t\t\t\t921600\r\nComputer OS:\t\t\t\t\t\tWindows 10\r\n\r\n### Description:\r\nWith 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).\r\n\r\nI 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.\r\n\r\nIf it doesn't work I get the following output with Debug Level set to verbose:\r\n[W][WiFiGeneric.cpp:357] _eventCallback(): Reason: 202 - AUTH_FAIL\r\n\r\nMy 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.\r\n\r\nI 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.\r\n\r\nAbout the whole working sketch below I have several questions:\r\n1. Is that \"works/doesn't work\" behaviour a firmware bug, a bug in arduino or this SDK or a router problem?\r\n2. 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)?\r\n3. I never got \"WL_CONNECTION_LOST\" as Wifi status. Do I need to consider that? And if so, how?\r\n\r\nPlease advise! Thank's!\r\n\r\n### Sketch:\r\n```cpp\r\n#include <WiFi.h>\r\n\r\nconst char* ssid = \"...\";\r\nconst char* password = \"...\";\r\n\r\nbool myWiFiFirstConnect = true;\r\n\r\nvoid myWiFiTask(void *pvParameters) {\r\n  wl_status_t state;\r\n\r\n  while (true) {\r\n    state = WiFi.status();\r\n    if (state != WL_CONNECTED) {  // We have no connection\r\n      if (state == WL_NO_SHIELD) {  // WiFi.begin wasn't called yet\r\n        Serial.println(\"Connecting WiFi\");\r\n\r\n        WiFi.mode(WIFI_STA);\r\n        WiFi.begin(ssid, password);\r\n\r\n      } else if (state == WL_CONNECT_FAILED) {  // WiFi.begin has failed (AUTH_FAIL)\r\n        Serial.println(\"Disconnecting WiFi\");\r\n\r\n        WiFi.disconnect(true);\r\n\r\n      } else if (state == WL_DISCONNECTED) {  // WiFi.disconnect was done or Router.WiFi got out of range\r\n        if (!myWiFiFirstConnect) {  // Report only once\r\n          myWiFiFirstConnect = true;\r\n\r\n          Serial.println(\"WiFi disconnected\");\r\n        }\r\n      }\r\n\r\n      vTaskDelay (250); // Check again in about 250ms\r\n\r\n    } else { // We have connection\r\n      if (myWiFiFirstConnect) {  // Report only once\r\n        myWiFiFirstConnect = false;\r\n\r\n        Serial.print(\"Connected to \");\r\n        Serial.println(ssid);\r\n        Serial.print(\"IP address: \");\r\n        Serial.println(WiFi.localIP());\r\n        Serial.println(\"\");\r\n      }\r\n\r\n      vTaskDelay (5000); // Check again in about 5s\r\n    }\r\n  }\r\n}\r\n\r\nvoid setup() {\r\n  delay(1000); // Power up\r\n\r\n  Serial.begin(115200);\r\n\r\n  // Create a connection task with 8kB stack on core 0\r\n  xTaskCreatePinnedToCore(myWiFiTask, \"myWiFiTask\", 8192, NULL, 3, NULL, 0);\r\n}\r\n\r\nvoid loop() {\r\n}\r\n```\r\n\r\n### Debug Messages:\r\n```\r\nIf WiFi.begin works:\r\n[D][WiFiGeneric.cpp:342] _eventCallback(): Event: 0 - WIFI_READY\r\n[D][WiFiGeneric.cpp:342] _eventCallback(): Event: 2 - STA_START\r\n....[D][WiFiGeneric.cpp:342] _eventCallback(): Event: 4 - STA_CONNECTED\r\n[D][WiFiGeneric.cpp:342] _eventCallback(): Event: 7 - STA_GOT_IP\r\n[D][WiFiGeneric.cpp:385] _eventCallback(): STA IP: 192.168.178.21, MASK: 255.255.255.0, GW: 192.168.178.1\r\n\r\nIf it doesn't work:\r\n[D][WiFiGeneric.cpp:342] _eventCallback(): Event: 0 - WIFI_READY\r\n[D][WiFiGeneric.cpp:342] _eventCallback(): Event: 2 - STA_START\r\n[D][WiFiGeneric.cpp:342] _eventCallback(): Event: 5 - STA_DISCONNECTED\r\n[W][WiFiGeneric.cpp:357] _eventCallback(): Reason: 202 - AUTH_FAIL\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/espressif/arduino-esp32/issues/comments/548484502","html_url":"https://github.com/espressif/arduino-esp32/issues/2501#issuecomment-548484502","issue_url":"https://api.github.com/repos/espressif/arduino-esp32/issues/2501","id":548484502,"node_id":"MDEyOklzc3VlQ29tbWVudDU0ODQ4NDUwMg==","user":{"login":"HendrikHo","id":17598620,"node_id":"MDQ6VXNlcjE3NTk4NjIw","avatar_url":"https://avatars3.githubusercontent.com/u/17598620?v=4","gravatar_id":"","url":"https://api.github.com/users/HendrikHo","html_url":"https://github.com/HendrikHo","followers_url":"https://api.github.com/users/HendrikHo/followers","following_url":"https://api.github.com/users/HendrikHo/following{/other_user}","gists_url":"https://api.github.com/users/HendrikHo/gists{/gist_id}","starred_url":"https://api.github.com/users/HendrikHo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/HendrikHo/subscriptions","organizations_url":"https://api.github.com/users/HendrikHo/orgs","repos_url":"https://api.github.com/users/HendrikHo/repos","events_url":"https://api.github.com/users/HendrikHo/events{/privacy}","received_events_url":"https://api.github.com/users/HendrikHo/received_events","type":"User","site_admin":false},"created_at":"2019-10-31T17:28:22Z","updated_at":"2019-10-31T17:28:22Z","author_association":"NONE","body":"Not fine, but this seems to work for me:\r\n\r\n    while (WiFi.status() != WL_CONNECTED) {\r\n        WiFi.begin(c_wifi_ssid, c_wifi_psk);\r\n        delay(1000);\r\n        Serial.print(\".\")\r\n\r\n"}}
{"actor":{"display_login":"HendrikHo"}}