我把我的 esp8266 作为接入点,以便移动设备可以连接到它。想要获取连接到它的设备的 macAddress。我怎么能得到它?
问问题
7917 次
3 回答
6
它有效
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
extern "C" {
#include<user_interface.h>
}
/* configuration wifi */
const char *ssid = "COblaster";
ESP8266WebServer server(80);
void handleRoot() {
server.send(200, "text/html", "<h1>You are connected</h1>");
String addy = server.client().remoteIP().toString();
Serial.println(addy);
}
void setup() {
delay(1000);
Serial.begin(115200);
Serial.println();
Serial.print("Configuring access point...");
WiFi.softAP(ssid);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.on("/", handleRoot);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
delay(5000);
client_status();
delay(4000);
}
void client_status() {
unsigned char number_client;
struct station_info *stat_info;
struct ip_addr *IPaddress;
IPAddress address;
int i=1;
number_client= wifi_softap_get_station_num();
stat_info = wifi_softap_get_station_info();
Serial.print(" Total connected_client are = ");
Serial.println(number_client);
while (stat_info != NULL) {
IPaddress = &stat_info->ip;
address = IPaddress->addr;
Serial.print("client= ");
Serial.print(i);
Serial.print(" ip adress is = ");
Serial.print((address));
Serial.print(" with mac adress is = ");
Serial.print(stat_info->bssid[0],HEX);
Serial.print(stat_info->bssid[1],HEX);
Serial.print(stat_info->bssid[2],HEX);
Serial.print(stat_info->bssid[3],HEX);
Serial.print(stat_info->bssid[4],HEX);
Serial.print(stat_info->bssid[5],HEX);
stat_info = STAILQ_NEXT(stat_info, next);
i++;
Serial.println();
}
delay(500);
}
于 2017-03-04T09:18:17.653 回答
0
如果我使用代码,则会出现此错误:
error: invalid conversion from 'ip4_addr*' to 'ip_addr*' [-fpermissive]
IPaddress = &stat_info->ip;
由于无效的类型转换,我解决了它:
ipv4_addr *IPaddress = &stat_info->ip;
address = IPaddress->addr;
Adrian McEwen 对核心 ESP8266 IPAdress.h 的更改也可以解决此问题。
于 2019-12-01T22:04:16.057 回答
0
@ABI 给出的答案有效,但存在内存泄漏问题。
wifi_softap_get_station_info()
应与wifi_softap_free_station_info()
以避免内存泄漏一起使用。
以下是没有内存泄漏问题的示例代码:
#include <ESP8266WiFi.h>
void showConnectedDevices()
{
auto client_count = wifi_softap_get_station_num();
Serial.printf("Total devices connected = %d\n", client_count);
auto i = 1;
struct station_info *station_list = wifi_softap_get_station_info();
while (station_list != NULL) {
auto station_ip = IPAddress((&station_list->ip)->addr).toString().c_str();
char station_mac[18] = {0};
sprintf(station_mac, "%02X:%02X:%02X:%02X:%02X:%02X", MAC2STR(station_list->bssid));
Serial.printf("%d. %s %s", i++, station_ip, station_mac);
station_list = STAILQ_NEXT(station_list, next);
}
wifi_softap_free_station_info();
}
可以包含以下行loop()
来监视空闲堆内存:
void loop()
{
Serial.printf("Free Heap: %d Bytes\n", ESP.getFreeHeap());
// other code
}
于 2020-08-29T21:44:13.217 回答