1

I am trying to scan wifi in sta_ap mode in my esp32 wroom 32 module but it is not scanning new wifi .Can anybody tell more about how does wifi mode works in this module and what's the problem in my code.Same code is working in esp8266.

CODE:

#include <WiFi.h>

const char *ap_ssid = "ESP32";
const char *ap_password = "";

const char *sta_ssid = "KKACT"; 
const char *sta_password = "kk25990418";
void setup() {
  Serial.begin(115200);
  
  WiFi.mode(WIFI_AP_STA);
  WiFi.disconnect();
  delay(100);
  Serial.print("Configuring access point...");
  WiFi.softAP(ap_ssid, ap_password);

  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);

  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(sta_ssid);
  WiFi.begin(sta_ssid, sta_password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
   Serial.println("Now scanning wifi continously");
   int n=WiFi.scanNetworks();
   for (int i = 0; i < n; ++i)
  {
      Serial.print(i + 1);
      Serial.print(": ");
      Serial.print(WiFi.SSID(i));
      Serial.print(" (");
      Serial.print(WiFi.RSSI(i));
      Serial.print(")");
    
  }
    delay(2000);
}
2
  • 1
    And if you disconnect before scanning?
    – Majenko
    Oct 30 '21 at 20:00
  • I tried disconnecting at the end of void main loop ,but the results were same i was getting the wifi list but new wifi are not being detected or maybe not getting scanned Oct 30 '21 at 20:59
1

This is a issue raised on github about this:

https://github.com/espressif/arduino-esp32/issues/3294

And the issue seems to be that a failed connect prevent the scanning and the solution is:

So what i did was when entering the portal:

WiFi.mode(WIFI_OFF); // otherwise Wifi.Scannetworks() fails

delay(5000);

This seems to stop the processes that prevent the scanning.

After that an immediate scan works without problems.

1
  • so where should i add this line ,at the end of void setup like this? Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); WiFi.mode(WIFI_OFF); delay(5000); Oct 31 '21 at 9:28
0

You are not doing it correctly. In order to perform WiFi scan:

  1. It must be in WIFI_STA mode;
  2. It has to be disconnect from the AP;
  3. After you done with the scanned SSID, BSSID and RSSI, you need to called WiFi.scanDelete() if you are going to do a scan again, otherwise you will get the same previous scanned result.

So in summary, here is the code:

#include <WiFi.h>

void setup() {
  Serial.begin(115200);
  Serial.println();
}

void loop() {
  Serial.println("Scanning WiFi Networks...");
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  int n = WiFi.scanNetworks();
  for (int i=0; i<n; i++) {
    Serial.printf("%2d SSID=%-20s RSSI=%d\n", i+1, WiFi.SSID(i).c_str(), WiFi.RSSI(i));
  }
  WiFi.scanDelete();

  delay(10000);
}
3
  • i tried WiFi.scanDelete(); but it didnt work ,I know i can use wifi_sta mode but esp32 has wifi_sta_ap which has both the station and access point mode ,so it should work in this mode too. I tried the same code in esp8266 and it worked,something in void setup is breaking the wifi scan. Oct 31 '21 at 15:43
  • Are you using your code by just adding WiFi.scanDelete() or you are using my example?
    – hcheung
    Nov 1 '21 at 3:19
  • i am using your code only but with Wifi.mode(WIFI_AP_STA ); Nov 1 '21 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.