简体   繁体   中英

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

I have gotten my ESP32 to create a wireless access point. 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. 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. If I connect a device, it should increase to 1 however it doesn't.

With just the default installation of the ESP32 board (from the boards manager) on verbose output the "error" I get is:

dhcps: send_offer>>udp_sendto result 0

Googling this brings up quite a few issues on this, mainly mentioning this github issue. The issue was opened all the way back in 7th Jan 2019. It's latest response was a few weeks ago. 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. I removed the one from the boards manager and installed the latest github release. 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.

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.

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. It has nothing to do with clients connecting to the wifi network that softAP creates. Instead it creates a server listening on the TCP port number that you gave it when you created the object (in this case, 48899). It should really be called TCPServer but sadly, it's not, and we all just have to live with it.

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

This is incorrect. If you connect a device to the wifi network it's not the same thing as connecting to a 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. 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.

For instance,

Serial.println(WiFi.softAPgetStationNum());

will print how many devices are connected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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