ESP8266:如何在站点模式下获取客户端的MAC地址(STA_MODE)?

问题描述 投票:1回答:1

我可以在访问点模式(AP_MODE)中获得客户端的MAC地址,请参阅下面的suGetClientMacAddress()函数。在Internet上找到此代码。我试图找出WiFi.mode(STA_MODE);中客户端的MAC地址,但是找不到关于此的任何有用信息,不可能吗?当看起来不可能时,why不可能吗?还是仍然有一个“技巧”来使它起作用?

下面您可以在AP_MODE中找到工作功能/版本。>

我正在使用默认ESP(v。2.4.0)库和Async Web / sockets库的库: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 "";  
}

功能:

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 "";  
}

是否有任何提示使其可以在STA_MODE下运行?

我可以在访问点模式(AP_MODE)中获取客户端mac地址,请参见下面的suGetClientMacAddress()函数。在Internet上找到此代码。我试图找出WiFi.mode(...)中客户端的MAC地址。

提示:
- stations (clients) connect to an AP (Access Point - server) - an AP generally hosts RF (radio) mechanisms for the connections of stations
ESP8266既可以是站点,也可以是AP,两者都不是。

[WiFi站(作为客户端)通常不直接相互通信,除非做出特殊规定,例如在mesh / Ad Hoc网络中或通过物理(电缆;)连接。

c++ esp8266 arduino-esp8266
1个回答
0
投票
ESP8266既可以是站点,也可以是AP,两者都不是。
© www.soinside.com 2019 - 2024. All rights reserved.