I can get the client mac-address in Access Point mode (AP_MODE), see suGetClientMacAddress()
function below.Found this code on the internet. I try to figure out the MAC-address of clients in WiFi.mode(STA_MODE);
, however cannot find any useful info on this, impossible? And when it seems it is impossible, why is it impossible? Or is there still a 'trick' to get it working?
Below you can find the working function/version in AP_MODE
The library I am using the default ESP (v. 2.4.0) library and the Async web/sockets library: https://github.com/me-no-dev/ESPAsyncWebServer
#define SU_WCFG_SERVER_ASYNC
........
........
#ifdef SU_WCFG_SERVER_ASYNC
#define SUWebSocketServer AsyncWebSocket
#define SUWebServerRequest AsyncWebServerRequest
#else
......
#endif
extern "C" {
#include "user_interface.h" // Required for some low level wifi functions
}
............
............
String suIpAddressToString( IPAddress ipAddress )
{
return String( ipAddress[0] )+"."+
String( ipAddress[1] )+"."+
String( ipAddress[2] )+"."+
String( ipAddress[3] );
}
IPAddress suStringToIpAddress( String ipAddress )
{
IPAddress ip = IPAddress( 0, 0, 0, 0 );
char* pStr = (char*)ipAddress.c_str();
uint8_t i = strlen( pStr );
uint8_t iPos = 4;
// Test before start
if( i > 6 && pStr[i-1] != '.' )
{
while( i-- && iPos )
{
if( pStr[i] == '.' || i == 0 )
{
ip[--iPos] = String( (char*)&pStr[i+(i>0)] ).toInt();
pStr[i] = 0; // set null termination to truncate
}
}
}
return ip;
}
String suGetClientIp( SUWebServerRequest* request )
{
if( request )
{ return suIpAddressToString( IPAddress( request->client()->remoteIP() )); }
return "";
}
The function:
String suGetClientMacAddress( SUWebServerRequest* request )
{
if( request )
{
struct ip_addr* IPaddress;
IPAddress ip;
struct station_info* stat_info = wifi_softap_get_station_info();
String sClientIp = suGetClientIp( request );
Serial.print( "Client ip: " );
Serial.println( sClientIp );
if( sClientIp.length() > 0 )
{
while( stat_info )
{
IPaddress = &stat_info->ip;
ip = IPaddress->addr;
if( suIpAddressToString( ip ) == sClientIp )
{
// Kind of rude and ugly to exit a loop and function this way
// however it works with less 'check' overhead. OK to me.
return String( stat_info->bssid[0], HEX )+":"+
String( stat_info->bssid[1], HEX )+":"+
String( stat_info->bssid[2], HEX )+":"+
String( stat_info->bssid[3], HEX )+":"+
String( stat_info->bssid[4], HEX )+":"+
String( stat_info->bssid[5], HEX );
}
stat_info = STAILQ_NEXT(stat_info, next);
}
}
}
return "";
}
Any hints to get it working in STA_MODE?