简体   繁体   English

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

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

I have gotten my ESP32 to create a wireless access point.我已经让我的 ESP32 创建一个无线接入点。 It shows up fine on any device however, no matter what device I try and connect (iPhone 6s, iPhone 8, Windows desktop), the ESP32 just says that there is no device connected.然而,无论我尝试连接什么设备(iPhone 6s、iPhone 8、Windows 桌面),它在任何设备上都显示良好,ESP32 只是说没有连接设备。 My code is:我的代码是:

#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

  }

Right now, it will print '0' over and over again since that is the number of clients connected.现在,它会一遍又一遍地打印“0”,因为那是连接的客户端数量。 If I connect a device, it should increase to 1 however it doesn't.如果我连接一个设备,它应该增加到 1 但它没有。

With just the default installation of the ESP32 board (from the boards manager) on verbose output the "error" I get is:仅在详细 output 上默认安装 ESP32 板(来自板管理器),我得到的“错误”是:

dhcps: send_offer>>udp_sendto result 0 dhcps: send_offer>>udp_sendto 结果 0

Googling this brings up quite a few issues on this, mainly mentioning this github issue.谷歌搜索这带来了很多问题,主要提到这个github 问题。 The issue was opened all the way back in 7th Jan 2019. It's latest response was a few weeks ago.该问题早在 2019 年 1 月 7 日就已打开。最近的回复是几周前。 To this day, there hasn't been a fix posted.直到今天,还没有发布修复程序。 A lot of people say updating to the latest ESP32 release from github (not boards manager) will work, so I tried it.很多人说从 github(不是板管理器)更新到最新的 ESP32 版本会起作用,所以我试了一下。 I removed the one from the boards manager and installed the latest github release.我从板管理器中删除了一个并安装了最新的 github 版本。 It no longer gives me the 'dhcps: send_offer>>udp_sendto result 0' message even on verbose output, but it still doesn't recognise that any device is connected.即使在详细的 output 上,它也不再给我“dhcps:send_offer>>udp_sendto result 0”消息,但它仍然无法识别任何设备已连接。

In my code, I have tried using the Arduino library version: #include <Wifi.h> and also the ESP library version: #include "Wifi.h" No luck there either.在我的代码中,我尝试使用 Arduino 库版本: #include <Wifi.h>以及 ESP 库版本: #include "Wifi.h"那里也没有运气。

Could anyone point me in the right direction on how to fix this issue?谁能指出我如何解决这个问题的正确方向?

That's not how this works.这不是它的工作原理。 Your program is working properly;您的程序运行正常; your expectations are wrong.你的期望是错误的。

WiFiServer is poorly named. WiFiServer的名字很糟糕。 It has nothing to do with clients connecting to the wifi network that softAP creates.它与连接到softAP创建的 wifi 网络的客户端无关。 Instead it creates a server listening on the TCP port number that you gave it when you created the object (in this case, 48899).相反,它会创建一个服务器,侦听您在创建 object 时提供的 TCP 端口号(在本例中为 48899)。 It should really be called TCPServer but sadly, it's not, and we all just have to live with it.它真的应该被称为TCPServer ,但遗憾的是,它不是,我们都只能忍受它。

If I connect a device, it should increase to 1 however it doesn't.如果我连接一个设备,它应该增加到 1 但它没有。

This is incorrect.这是不正确的。 If you connect a device to the wifi network it's not the same thing as connecting to a WiFiServer .如果您将设备连接到 wifi 网络,则与连接到WiFiServer

For your code to work, each device that connects to the wifi network your ESP32 is providing will also have to be run a program on it that opens a TCP connection to port 48899 on the ESP32.为了使您的代码正常工作,连接到您的 ESP32 提供的 wifi 网络的每个设备必须在其上运行一个程序,该程序打开一个 TCP 连接到 ESP32 上的端口 48899。 Otherwise the loop will never see a client available because there will be none.否则循环将永远不会看到可用的客户端,因为将没有可用的客户端。

If you want to know whether any devices have connected to the WiFi access point, you can use WiFi.softAPgetStationNum() - it will return the number of currently connected devices.如果您想知道是否有任何设备连接到 WiFi 接入点,您可以使用WiFi.softAPgetStationNum() - 它会返回当前连接的设备数量。

For instance,例如,

Serial.println(WiFi.softAPgetStationNum());

will print how many devices are connected.将打印连接了多少设备。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM