Closed sonastyle closed 6 years ago
reduce your test code and use code fence 3 ticks
The above code is working now with addition of 100mS delay before WiFi.begin(esid, epass) and the scenario is reproducible. Please let me know the technical significance of this.
Not sure, but I have a 200 ms delay after enableSta to allow for it to start, because I had the same problems
If you have solved your issue, please close it.
Hardware:
Board: NODE32S 4MB Flash Core Installation/update date: 11/jul/2017 IDE name: Arduino IDE / Visual Studio (vMicro) Flash Frequency: 40Mhz Upload Speed: 921600
Description:
First time when the code starts I call the wifi connect function and it connects to the specified AP and works well. If I call the WiFi.disconnect(true) method it always return no AP not found as WiFi.status() = 1 while that same AP is visible to all other device as well as its showing on return of scan_hotspot() method. This thing sometimes happen after a soft restart also and only can be retrieved via hard restart. Please let me know the fault points.
Sketch:
include
include
const char esid = "MCL"; // hotspot ssid const char epass = "MCL@1234"; // ssid password
void webService(){ server.on("/", HTTP_GET, rootGet); } void rootGet(AsyncWebServerRequest request){ request->send(200, "text/html", "This is index page"); } // wifi reconnect void wifi_reconnect(int count) { Serial.println(String(F("Wifi status before try: ")) + WiFi.status() + F(". SSID: ") + esid + F(". Pw: ") + epass); int wifi_retry = 0; while ((WiFi.status() != WL_CONNECTED) && (wifi_retry < count) && WiFi.getMode() == WIFI_MODE_APSTA) { Serial.println(String(F("Ssid: ")) + esid + F(". Pw: ") + epass + F(". Mode: ") + WiFi.getMode()); WiFi.begin(esid, epass); int concode = WiFi.waitForConnectResult(); wifi_retry++; Serial.print(String(F("Retried: ")) + wifi_retry + F(" times.\t")); Serial.println(String(F("Wifi status trying to connect: ")) + concode); } } Serial.println(WiFi.status()); } // hotspot scan void scan_hotspot() { Serial.println("Hotspot scanning called"); send_feedback(msg_scan_sotspot); int n = WiFi.scanNetworks(); Serial.println(F("scan done")); String _st=""; for (int i = 0; i < n; ++i) { // Print SSID and RSSI for each network found _st += " (WiFi.RSSI(i) + 100); _st += "% )"; // % symbol in html >> % _st += (WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? " Open" : " Secure"; _st += ""; } Serial.println(_st); }
void setup() { Serial.begin(115200); WiFi.mode(WIFI_AP_STA); WiFi.softAP(ssid, passphrase); Serial.println(F("Soft AP started")); IPAddress softapip = WiFi.softAPIP(); String lapip = String(softapip[0]) + '.' + String(softapip[1]) + '.' + String(softapip[2]) + '.' + String(softapip[3]); Serial.println(String(F("AP Ip: ")) + lapip); webService(); // webserver routing method server.begin(); // webserver begin Serial.println(F("web service called")); wifi_reconnect(10); // Try 10 times }
void loop() { if(WiFi.status() != 3){ wifi_reconnect(); } if(Serial.available() >0){ String a = Serial.readStringUntil('\n'); if(a== "discon"){ WiFi.disconnect(true); // on demand disconnect wifi }else if(a== "scan"){ scan_hotspot(); // on demand scan hotspot } } }
Debug Messages: Serial output
code does not crashed, just can not reconnect wifi on loop