Sunday, April 23, 2017

Get the WiFi MAC address of ESP32 Module/available WiFi network

This example show how to get the WiFi MAC address of ESP32 Module, using Arduino core for ESP32. In this example, I just want to know the MAC address only, without connect to any hotspot, so no ssid and password needed (or it will connect to last ssid).


ESP32_WiFi.ino
/*
 * ESP-32 Example, 
 * start WiFi (without ssid and password)
 * and print the MAC address to Serial.
 * http://arduino-er.blogspot.com/
 */

#include <WiFi.h>

byte mac[6];

void setup()
{
    Serial.begin(115200);
    delay(1000);

    Serial.println();
    Serial.print("Program Start...");

    WiFi.begin();
    Serial.println("WiFi began");
    WiFi.macAddress(mac);
    
    Serial.print("MAC: ");
    Serial.print(mac[0],HEX);
    Serial.print(":");
    Serial.print(mac[1],HEX);
    Serial.print(":");
    Serial.print(mac[2],HEX);
    Serial.print(":");
    Serial.print(mac[3],HEX);
    Serial.print(":");
    Serial.print(mac[4],HEX);
    Serial.print(":");
    Serial.println(mac[5],HEX);

}

void loop(){

}

It match with the scanned address of the wireless router.




Remark:
May be it's something wrong on my ESP32 board; sometimes the program halt on the code WiFi.begin() without return. Try to close Serial Monitor, disconnect and reconnect ESP32 USB cable, then run Serial Monitor.


added@2022-03-12

Get MAC address of available WiFi network

It's a WiFiScan example installed with arduino-esp32. It act as WIFI_STA, scan available WiFi network and print the SSID, RSSI...


To get the mac address, call BSSID(), return uint8_t *. Or call BSSIDstr(), return String. Basically, BSSID is its MAC address.


/*
 *  This sketch demonstrates how to scan WiFi networks.
 *  The API is almost the same as with the WiFi Shield library,
 *  the most obvious difference being the different file you need to include:
 */
#include "WiFi.h"

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

    // Set WiFi to station mode and disconnect from an AP if it was previously connected
    WiFi.mode(WIFI_STA);
    WiFi.disconnect();
    delay(100);

    Serial.println("Setup done");
}

void loop()
{
    Serial.println("scan start");

    // WiFi.scanNetworks will return the number of networks found
    int n = WiFi.scanNetworks();
    Serial.println("scan done");
    if (n == 0) {
        Serial.println("no networks found");
    } else {
        Serial.print(n);
        Serial.println(" networks found");
        for (int i = 0; i < n; ++i) {
            // Print SSID and RSSI for each network found
            Serial.print(i + 1);
            Serial.print(": ");
            Serial.print(WiFi.SSID(i));
            Serial.print(" (");
            Serial.print(WiFi.RSSI(i));
            Serial.print(")");
            Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN)?" ":"*");

            //get MAC address
            uint8_t * nBSSID = WiFi.BSSID(i);
            Serial.println(WiFi.BSSIDstr(i)); //BSSIDstr() return in String
            
            delay(10);
        }
    }
    Serial.println("");

    // Wait a bit before scanning again
    delay(5000);
}


No comments:

Post a Comment