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

时间:2018-02-20 21:23:22

标签: c++ esp8266 arduino-esp8266

我可以在接入点模式(AP_MODE)中获取客户端mac-address,请参阅下面的suGetClientMacAddress()函数。在Internet上发现此代码。我试图找出WiFi.mode(STA_MODE);中客户端的MAC地址,但是找不到任何有用的信息,这是不可能的?当看起来不可能时,为什么是不可能的?或者还有一个“技巧”让它运作起来吗?

您可以在下面找到AP_MODE

中的工作函数/版本

库我正在使用默认的ESP(v.2.4.0)库和异步Web /套接字库: 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中运行?

1 个答案:

答案 0 :(得分:0)

提示:

- stations (clients) connect to an AP (Access Point - server)
- an AP generally hosts RF (radio) mechanisms for the connections of stations

ESP8266可以是station或AP,两者都不是。

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