Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By gr0b
#20483 Hello,

I am wanting to know how can you get the MAC address of the STA and SoftAP interface in code?

Looking in the code I can see I can set them with the following, but did not find a way to get them.
uint8_t* macAddress(uint8_t* mac);
uint8_t* softAPmacAddress(uint8_t* mac);
Last edited by gr0b on Tue Jun 16, 2015 7:39 pm, edited 1 time in total.
User avatar
By HermannSW
#20505 The comments in header file say that both functions do read the mac addresses.

And ESP8266WiFi.cpp confirms that:
Code: Select alluint8_t* ESP8266WiFiClass::macAddress(uint8_t* mac)
{
    wifi_get_macaddr(STATION_IF, mac);
    return mac;
}

uint8_t* ESP8266WiFiClass::softAPmacAddress(uint8_t* mac)
{
    wifi_get_macaddr(SOFTAP_IF, mac);
    return mac;
}


Hermann.
User avatar
By gr0b
#20643 OK got it working. Here is a code snippet, it is a bit ugly but works.

Code: Select alluint8_t  MAC_softAP[]          = {0,0,0,0,0,0};                      //not sure why we have to pass a MAC address to get a MAC address.
uint8_t  MAC_STA[]                = {0,0,0,0,0,0};

void setup() {
    Serial.begin(115200);
    Serial.println();
   
    Serial.print("MAC[SoftAP]");
    uint8_t* MAC  = WiFi.softAPmacAddress(MAC_softAP);                   //get MAC address of softAP interface
    for (int i = 0; i < sizeof(MAC)+2; ++i){                                                          //this line needs cleaning up.
         Serial.print(":");
         Serial.print(MAC[i],HEX);
         MAC_softAP[i] = MAC[i];                                         //copy back to global variable
    }
    Serial.println();
    Serial.print("MAC[STA]");
    MAC  = WiFi.macAddress(MAC_STA);                   //get MAC address of STA interface
    for (int i = 0; i < sizeof(MAC)+2; ++i){
         Serial.print(":");
         Serial.print(MAC[i],HEX);
         MAC_STA[i] = MAC[i];                                            //copy back to global variable
    }
    Serial.println();
}
User avatar
By saltdog
#30781 This is a slightly prettier way of doing things

Code: Select all#include <ESP8266WiFi.h>

uint8_t MAC_array[6];
char MAC_char[18];

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

    WiFi.macAddress(MAC_array);
    for (int i = 0; i < sizeof(MAC_array); ++i){
      sprintf(MAC_char,"%s%02x:",MAC_char,MAC_array[i]);
    }
 
    Serial.println(MAC_char);
}

void loop() {
 
}