【问题标题】:ESP8266: How to get MAC address of client in station mode (STA_MODE)?ESP8266:如何在工作站模式(STA_MODE)下获取客户端的 MAC 地址?
【发布时间】:2018-02-20 21:23:21
【问题描述】:

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

您可以在下面找到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 下工作?

【问题讨论】:

    标签: c++ esp8266 arduino-esp8266


    【解决方案1】:

    提示:

    - 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 网络中或通过物理(电缆;)连接。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-24
      • 2017-10-28
      • 1970-01-01
      • 2021-03-18
      • 1970-01-01
      • 1970-01-01
      • 2019-07-12
      • 2013-11-07
      相关资源
      最近更新 更多