ESP32 无法识别任何设备连接到其接入点

ESP32 not recognising that any device is connected to its access point

我已经用我的 ESP32 创建了一个无线接入点。它在任何设备上都显示正常,但是,无论我尝试连接什么设备(iPhone 6s,iPhone 8,Windows 桌面),ESP32 只是说没有连接设备. 我的代码是:

#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>

const char *ssid = "testAP";
const char *password = "0000000000";

WiFiServer server(48899);


void setup() {

  Serial.begin(115200);
  delay(5000);
  Serial.println();
  Serial.println("Configuring access point...");

  WiFi.softAP(ssid, password);
  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);
  server.begin();

  Serial.println("Server started");
}

void loop() {
  WiFiClient client = server.available();   // listen for incoming clients

  if (client) {
    Serial.println(client);// if you get a client,
    Serial.println("New Client.");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        //Serial.write(c);                    // print it out the serial monitor
      }
    }
  }
  // close the connection:
  //client.stop();
  Serial.println(client); //this will print "0" no matter how many clients are connected

  }

现在,它会一遍又一遍地打印“0”,因为这是连接的客户端数量。如果我连接一个设备,它应该增加到 1,但它没有。

只有 ESP32 开发板的默认安装(来自开发板管理器)的详细输出 "error" 我得到的是:

dhcps: send_offer>>udp_sendto result 0

谷歌搜索这方面提出了很多问题,主要提到 this github 问题。这个问题从 2019 年 1 月 7 日开始一直打开。最新的回复是几周前。直到今天,还没有发布修复程序。很多人说从 github(不是板管理器)更新到最新的 ESP32 版本会起作用,所以我试了一下。 我从董事会管理器中删除了那个并安装了最新的 github 版本。即使在详细输出时,它也不再给我 'dhcps: send_offer>>udp_sendto result 0' 消息,但它仍然无法识别任何设备已连接。

在我的代码中,我尝试使用 Arduino 库版本: #include <Wifi.h> 还有 ESP 库版本: #include "Wifi.h" 也没有运气。

任何人都可以指出正确的方向来解决这个问题吗?

这不是它的工作原理。您的程序运行正常;你的期望是错误的。

WiFiServer 命名不当。它与连接到 softAP 创建的 wifi 网络的客户端无关。相反,它会创建一个服务器,监听您在创建对象时提供给它的 TCP 端口号(在本例中为 48899)。它真的应该被称为 TCPServer,但遗憾的是,它不是,我们都不得不忍受它。

If I connect a device, it should increase to 1 however it doesn't.

这是不正确的。如果您将设备连接到 wifi 网络,这与连接到 WiFiServer.

不同

为了使您的代码正常工作,每个连接到 ESP32 提供的 wifi 网络的设备 必须是 运行 上面的一个程序,可以打开一个TCP 连接到 ESP32 上的端口 48899。否则循环永远不会看到可用的客户端,因为会有 none.

如果您想知道是否有任何设备连接到 WiFi 接入点,您可以使用 WiFi.softAPgetStationNum() - 它会 return 当前连接的设备数。

例如,

Serial.println(WiFi.softAPgetStationNum());

将打印连接的设备数量。