How to get MAC address of interfaces [Solved + Example]
Posted:
Sun Jun 14, 2015 6:22 pm
by gr0b
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);
Re: How to get MAC address of interfaces?
Posted:
Mon Jun 15, 2015 8:08 am
by HermannSW
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.
Re: How to get MAC address of interfaces?
Posted:
Tue Jun 16, 2015 7:38 pm
by gr0b
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();
}
Re: How to get MAC address of interfaces [Solved + Example]
Posted:
Wed Oct 07, 2015 7:03 am
by saltdog
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() {
}