Save AP mac address into variable

Hi guys. I want to scan WiFi with esp8266 then save the mac address of the strangest AP into a variable. I wrote an sketch for this but getting error on compilation.

here the code:

#include <ESP8266WiFi.h>

int num_networks;
int strongest_signal_strength = -100;
int strongest_signal_index;
uint8_t strongest_signal_mac[6];

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

void loop() {
  num_networks = WiFi.scanNetworks();
  Serial.print("Number of networks found: ");
  Serial.println(num_networks);

  for (int i = 0; i < num_networks; i++) {
    int signal_strength = WiFi.RSSI(i);
    if (signal_strength > strongest_signal_strength) {
      strongest_signal_strength = signal_strength;
      strongest_signal_index = i;
    }
  }
  Serial.print("Strongest signal strength: ");
  Serial.println(strongest_signal_strength);
  WiFi.BSSID(strongest_signal_index, strongest_signal_mac);
  Serial.print("Strongest signal MAC address: ");
  for (int i = 0; i < 6; i++) {
    Serial.print(strongest_signal_mac[i], HEX);
    if (i < 5) Serial.print(":");
  }
  Serial.println();
  delay(5000);
}

error:

Compilation error: no matching function for call to 'ESP8266WiFiClass::BSSID(int&, uint8_t [6])'

thanks.

Can someone help please?

There's no function in the WiFi class that takes two parameters. Try this for that line:
strongest_signal_mac = WiFi.BSSID(strongest_signal_index);

And in addition to that, you might need 13
char array[13] to store that address (if it comes as string).
With 6 int array you wil write outside array boundaries.

Good catch! No doubt he/she would have been back again with another problem shortly. :slight_smile:

Thanks for reply, I solved the problem with the code below

uint8_t target_mac[BSSID_LEN];

  int strongest_signal_index = -1;
  int strongest_signal_strength = -100;

  for (int i = 0; i < n; i++) {
    // Get the signal strength of the i-th access point
    int signal_strength = WiFi.RSSI(i);

    // Get the BSSID of the i-th access point
    uint8_t* bssid = WiFi.BSSID(i);

    // Check if the signal strength of the i-th access point is stronger than the strongest signal
    if (signal_strength > strongest_signal_strength) {
      strongest_signal_index = i;
      strongest_signal_strength = signal_strength;
      memcpy(target_mac, bssid, BSSID_LEN);
    }
  }

Who owns the memory that bssid points to?
You or the WiFi class?
Is this the way you are supposed to access bssid? Did you follow a library example?
To me it seems ugly. And asking for trouble.
Did you get any compiler warnings?
Like you are supposed to declare bssid as a pointer to const uint8_t?
Or const char*...

here the full code. This code searches for nearby Wi-Fi networks and stores the MAC address of the nearest access point to this variable uint8_t target_mac[BSSID_LEN];


#include <ESP8266WiFi.h>

// Define the length of the BSSID array
#define BSSID_LEN 6

uint8_t target_mac[BSSID_LEN];

// ===== Setup ===== //
void setup() {
  Serial.begin(115200);  // Start serial communication
  WiFi.disconnect();                    // Disconnect from any saved or active WiFi connections
  wifi_set_opmode(STATION_MODE);        // Set device to client/station mode

  int n = WiFi.scanNetworks();
  Serial.println("Scan completed");

  // Print the number of available networks
  Serial.print(n);
  Serial.println(" networks found");

  // Find the access point with the strongest signal
  int strongest_signal_index = -1;
  int strongest_signal_strength = -100;

  for (int i = 0; i < n; i++) {
    // Get the signal strength of the i-th access point
    int signal_strength = WiFi.RSSI(i);

    // Get the BSSID of the i-th access point
    uint8_t* bssid = WiFi.BSSID(i);

    // Check if the signal strength of the i-th access point is stronger than the strongest signal
    if (signal_strength > strongest_signal_strength) {
      strongest_signal_index = i;
      strongest_signal_strength = signal_strength;
      memcpy(target_mac, bssid, BSSID_LEN);
    }
  }

  // Print the BSSID of the access point with the strongest signal
  Serial.print("BSSID: ");
  for (int i = 0; i < BSSID_LEN; i++) {
    Serial.print(target_mac[i], HEX);
    if (i < BSSID_LEN - 1) {
      Serial.print(":");
    }
  }
  Serial.println();
  Serial.println(WiFi.SSID(strongest_signal_index));
  Serial.println("Started \\o/");
}

void loop() {

}

Out of curiosity, what is the project intention? I've been following WAP2 encryption cracking for my own security purposes so I'm curious if you're looking for AP's that try to hijack or inject packets, wardriving, internal security, etc.

ESP32 || RP2040 Pico W

#if defined(ESP32)
      Serial.print(WiFi.BSSIDstr(i));
#else
      uint8_t bssid1[6];
      WiFi.BSSID(i, bssid1);
      Serial.print(macToString(bssid1));
#endif

This way prevents messing with pointers to class members. :+1:

Yes, to monitor deauthenticated packets on the nearest AP (my Home WiFi Router) and ignore all other APs.

If you already know the SSID, then why are you searching for it?

this is a standard hacker procedure for obtaining an access password to gain access to WIFI by creating a clone by SSID

But that is not what the OP said he was doing.

I thought TS wants to know if someone is trying to access his WIFI network or not, apparently I was wrong.

SSID is not important at all. In this code, the MAC address of the access point that closest to ESP8266 is automatically used to monitor packets, and the rest of the access points are ignored except your access point.

And you have to scan for the Mac of your access point? Why? Couldn't you just hard code it? It is static after all.

Good question, why scan and look for what is already known

I change the MAC address of the Wi-Fi router every day (with a script that runs every day and sets the generated random MAC address), the router I use is Mikrotik, which has a number of Internet of Things gadgets connected to it. As a network administrator, I prefer to use this method.