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])'
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.
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.
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.
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.