ESP8266 AP模式下WiFi.status()始终返回WL_DISCONNECTED(6)问题咨询
问题解析与解决思路
这其实不是ESP8266库2.4.0的bug——而是你误解了WiFi.status()函数的设计用途!
为什么WiFi.status()在AP模式下一直返回WL_DISCONNECTED(6)?
WiFi.status()这个函数从一开始就是为Station模式设计的:它的作用是查询ESP8266作为客户端(连接到其他路由器)时的连接状态。当ESP8266工作在AP模式时,它本身是热点提供者,不需要连接其他路由器,所以这个函数的返回值没有实际意义,始终返回WL_DISCONNECTED是完全正常的行为。
如何正确监控AP模式下的设备连接状态?
你需要使用专门针对AP模式的状态查询函数,这里有两种常用方案:
1. 快速查询连接设备数量
使用WiFi.softAPgetStationNum()函数,它会直接返回当前连接到ESP8266 AP的设备总数。这是最简单的监控方式,示例代码如下:
#include <ESP8266WiFi.h> const char* ssid = "YourAPName"; const char* password = "YourAPPassword"; void setup() { Serial.begin(115200); WiFi.softAP(ssid, password); Serial.println("AP started"); } void loop() { int connectedCount = WiFi.softAPgetStationNum(); Serial.print("Connected devices: "); Serial.println(connectedCount); if (connectedCount > 0) { Serial.println("There are devices connected to the AP"); } else { Serial.println("No devices connected"); } delay(1000); }
2. 获取连接设备的详细信息(MAC/IP)
如果需要知道具体是哪些设备连接进来,可以使用WiFi.softAPgetStationInfo()函数,它会返回一个包含所有连接设备信息的链表。示例代码:
#include <ESP8266WiFi.h> const char* ssid = "YourAPName"; const char* password = "YourAPPassword"; void printConnectedStations() { struct station_info* stationList = WiFi.softAPgetStationInfo(); Serial.println("\n--- Connected Stations ---"); while (stationList != nullptr) { // 打印MAC地址 Serial.print("MAC: "); for (int i = 0; i < 6; i++) { if (i > 0) Serial.print(":"); Serial.print(stationList->bssid[i], HEX); } // 打印IP地址 Serial.print(" | IP: "); Serial.println(IPAddress(stationList->ip)); // 遍历下一个设备 stationList = STAILQ_NEXT(stationList, next); } } void setup() { Serial.begin(115200); WiFi.softAP(ssid, password); Serial.println("AP started"); } void loop() { printConnectedStations(); delay(5000); }
关于AP示例使用WiFi.status()的疑问
有些AP示例里确实会调用WiFi.status(),但通常只是用来验证AP是否成功初始化(比如确认没有意外切换到Station模式),并不是用来监控设备连接状态的。真正要判断有没有设备接入,还是得用上面提到的AP专用函数。
内容的提问来源于stack exchange,提问作者Codebeat
相关产品推荐
相关产品推荐

