How to read the WiFi AP MAC address after connection?

I want to log the MAC address of the access point my ESP-8266 connects to in order to verify its data. But I have not found any command to do this...

I have scanned the ESP8266 WiFi documentation about how to retrieve the AP MAC address here.

But the command WiFi.macAddress() returns the MAC of the ESP itself, which is not what I want...

Is there some way to read the MAC address of the AccessPoint itself to which the ESP is connected?

When starting this thread I found a link to a similar question but the reply was to "read the docs" and no other help provided...

However in that linked reply the advice was to use WiFi.BSSID(), for which the doc states:

Return the mac address of the access point to which the ESP module was directed to connect to.
This address is formally called Basic Service Set Identification (BSSID).
The returned pointer is what the user configured when calling begin() with a bssid argument. It does not necessarily reflect the mac address of the access point to which the ESP module’s station interface is currently connected to.

So as you can see it is not really what is asked for....

ARP -A or /A

BSSID is the MAC address of the AP

Return the mac address of the access point to which the ESP module was directed to connect to.

Thanks,

But what about the following reference doc sentence then? It seems to say the opposite....

The returned pointer is what the user configured when calling begin() with a bssid argument. It does not necessarily reflect the mac address of the access point to which the ESP module’s station interface is currently connected to

Or does this comment ONLY apply when using the WiFi.Begin() method with the 3rd argument bssid?

I.e. if one uses WiFi.Begin(ssid, password) THEN the command WiFi.BSSID() will return the actual MAC of the AP?

Not clear to me..

it is specification of BSSID in the WiFi standard versus reality.

OK, so I have added this to the code into the setup() procedure after WiFi is started:

  String topic;  
  //Send WiFi Access Point MAC address
  topic = String(mqtt_topic) + "/wifiapmac";
  mqtt_client.publish(topic.c_str(), WiFi.BSSIDstr().c_str());

Not yet finished so cannot test right now...

Final report
I have now updated the IoT f/w with the code shown and it does log the correct WiFi AP MAC address via MQTT after it connects!
I have not tried the WiFi.Begin(ssid, passwd, APmacaddr) command since I changed the SSID on the AccessPoint for it to be unique.

Case closed! :cowboy_hat_face: :grinning:

REOPENING the thread since I ran into trouble later...

The code works fine as is with a WiFi.begin(MySSID, MyPasswd); command to connect.
But it turns out that it takes more than 4 seconds to connect and this is not quick enough for my use...
The device is supposed to quickly perform some simple reporting action when reset and then go to deep sleep, and I need the total time to be around a second. Right now it takes about 4.5 seconds and I have read somewhere that by specifying the bssid MAC address in the call it can be dropped to below a second.

So I am looking at the version of begin that specifies the AP by MAC address:
WiFi.begin(MySSID, MyPasswd, myap_bssid);

I have tried several ways to set the extra parameter based on stuff I have read here, but it won't compile!

What is wrong with this piece of code?

int Start_WiFi()
{  
    //Some initialization
    const byte myap_bssid[] = { 0x7C, 0x10, 0xC9, 0xAF, 0x4C, 0x88 };
    WiFi.mode(WIFI_STA);
    WiFi.setPhyMode(WIFI_PHY_MODE_11G);
    WiFi.hostname(MyHostname);
    WiFi.begin(MySSID, MyPasswd, myap_bssid); // <== Fails compiling this!!!
   //Some finalization

What happens when I build the code is that it fails complaining about line 150, which is the WiFi.Begin command:

src\main.cpp:150:44: error: invalid conversion from 'const byte* {aka const unsigned char*}' to 'int32_t {aka int}' [-fpermissive]
     WiFi.begin(MySSID, MyPasswd, myap_bssid);

By removing the 3rd argument to WiFi.begin() the code successfully builds!

I am not versed enough in C++ to know what to do about such a message....

how long needs your phone to connect to the same WiFi?

I do not understand what you ask...
I am not using a phone but an ESP-01 device (based on ESP8266).
It is supposed to be idling in deep sleep running on battery power until an event happens and then wake up connect and send an MQTT message then go back to deep sleep.
The wakeup is done by using the reset pin since the real wake from deep sleep pin is not exposed on an ESP-01.

I do read other people needing quick connects and getting it via use of bssid as 3rd argument to Wifi.begin(). They even mention a few hundred milliseconds.

I thought that upwards of 10 seconds would be acceptable between reactions but I found out yesterday that the events may trigger with an interval sometimes even below 4 seconds. So the entire process may not take longer of course.

use static IP. DHCP takes time

So you think it is the DHCP lookup that takes time in the router?
I made an attempt to configure such that DHCP is not used:

int Start_WiFi()
 {  
    //Hardcode WiFi connection here
    IPAddress static_ip(192, 168, 117, 33); //Define a fixed address here
    IPAddress static_gw(192, 168, 117, 1);  //Define a fixed address here
    IPAddress static_sn(255, 255, 255, 0 );  //Define a fixed address here

    String MsgDbg = "";  //Used for debug messages
    String MyChipID = String(GetChipIdentifier(), HEX);
    String MyHostname = "esp-" + MyChipID;
    int res = 0;
    
    WiFi.config(static_ip, static_gw, static_sn); //Test to bypass DHCP
    WiFi.mode(WIFI_STA);
    WiFi.setPhyMode(WIFI_PHY_MODE_11G);
    WiFi.hostname(MyHostname);
    WiFi.begin(MySSID, MyPasswd);
    .. connection tests follow ..

This was used in place of the old (simpler) code without definitions of WiFi.config.

The result was that the device took exactly the same time to connect as before (some 4500 ms).

I still want to understand what is wrong with the use of the bssid argument as shown above.

According to other posts I have scanned it should work but the code they have shown throws a compilation error for me, why?

I have been at this for hours now and cannot make in or out of how to formulate the setting for the bssid argument so the compiler will understand!

PLEASE-PLEASE!
Help me defining the bssid argument to WiFi.begin()!

I have tried all of these without any success:

    const uint8_t[6] myap_bssid = {0x7C, 0x10, 0xC9, 0xAF, 0x4C, 0x88};
    //uint8_t[6] myap_bssid = {0x7C, 0x10, 0xC9, 0xAF, 0x4C, 0x88};
    //const int8_t myap_bssid[]     = {0x7C, 0x10, 0xC9, 0xAF, 0x4C, 0x88};
    //const byte myap_bssid[]     = {0x7C, 0x10, 0xC9, 0xAF, 0x4C, 0x88};
    //const uint8_t myap_bssid[6] = {0x7C, 0x10, 0xC9, 0xAF, 0x4C, 0x88};

Used like this:

    WiFi.mode(WIFI_STA);
    WiFi.setPhyMode(WIFI_PHY_MODE_11G);// Available Modes: WIFI_PHY_MODE_11B, WIFI_PHY_MODE_11G, WIFI_PHY_MODE_11N
    WiFi.hostname(MyHostname);
    WiFi.begin(MySSID, MyPasswd, myap_bssid);

I always get an error complaining about the problem looking like this:

src\main.cpp: In function 'int Start_WiFi()':
src\main.cpp:136:19: error: expected identifier before numeric constant
  136 |     const uint8_t[6] myap_bssid = {0x7C, 0x10, 0xC9, 0xAF, 0x4C, 0x88};
      |                   ^
src\main.cpp:136:19: error: expected ']' before numeric constant
  136 |     const uint8_t[6] myap_bssid = {0x7C, 0x10, 0xC9, 0xAF, 0x4C, 0x88};
      |                   ^
      |                   ]
src\main.cpp:136:18: error: structured binding declaration cannot have type 'const uint8_t' {aka 'const unsigned char'}
  136 |     const uint8_t[6] myap_bssid = {0x7C, 0x10, 0xC9, 0xAF, 0x4C, 0x88};
      |                  ^
src\main.cpp:136:18: note: type must be cv-qualified 'auto' or reference to cv-qualified 'auto'
src\main.cpp:136:18: error: empty structured binding declaration
src\main.cpp:136:22: error: expected initializer before 'myap_bssid'
  136 |     const uint8_t[6] myap_bssid = {0x7C, 0x10, 0xC9, 0xAF, 0x4C, 0x88};
      |                      ^~~~~~~~~~
src\main.cpp:162:34: error: 'myap_bssid' was not declared in this scope; did you mean 'apbssid'?
  162 |     WiFi.begin(MySSID, MyPasswd, myap_bssid);
      |                                  ^~~~~~~~~~
      |                                  apbssid
src\main.cpp:136:18: warning: unused structured binding declaration [-Wunused-variable]
  136 |     const uint8_t[6] myap_bssid = {0x7C, 0x10, 0xC9, 0xAF, 0x4C, 0x88};

If I use this to begin WiFi instead all works fine (except I cannot control the AP connection):

    WiFi.begin(MySSID, MyPasswd);
const uint8_t[6] myap_bssid = {0x7C, 0x10, 0xC9, 0xAF, 0x4C, 0x88};

should be

const uint8_t myap_bssid[6] = {0x7C, 0x10, 0xC9, 0xAF, 0x4C, 0x88};

Well when i do that I get the error for the line

WiFi.begin(MySSID, MyPasswd, myap_bssid);

With error description

src\main.cpp: In function 'int Start_WiFi()':
src\main.cpp:162:34: error: invalid conversion from 'const uint8_t*' {aka 'const unsigned char*'} to 'int32_t' {aka 'int'} [-fpermissive]
  162 |     WiFi.begin(MySSID, MyPasswd, myap_bssid);
      |                                  ^~~~~~~~~~
      |                                  |
      |                                  const uint8_t* {aka const unsigned char*}
In file included from C:\Users\Bosse\.platformio\packages\framework-arduinoespressif8266\libraries\ESP8266WiFi\src/ESP8266WiFi.h:34,
                 from src\main.cpp:2:
  bool connect(const char *ssid, const char *passphrase = NULL, int32_t channel = 0, const uint8_t *bssid = NULL, bool connect = true);

OK thanks Juraj!
I guess that what you are showing is the underlying library function that gets called with WiFi.begin()....

So what I was missing is that the AP MAC address item is #4 rather than #3...
When I changed the command by adding a 0 for the channel entry so it reads like this it seems to work (compile without errors):

    const uint8_t myap_bssid[6] = {0x7C, 0x10, 0xC9, 0xAF, 0x4C, 0x88};
...
    WiFi.begin(MySSID, MyPasswd, 0, myap_bssid);

I will have to check this on the hardware.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.