ESP32服务器看不到客户端

时间:2019-05-23 06:04:40

标签: wifi esp32

我正在尝试通过Wi-Fi连接两个ESP32模块并发送信息。但是,我被困在服务器端。客户端显示已连接到Wi-Fi,但服务器端未打印“ New Client”,因此看不到客户端。我想知道自己在做错什么,也许以前有人尝试过。

服务器端:

#include <WiFi.h>

const char* ssid     = "ESP32";
const char* password = "123";

 WiFiServer server(80);    

void setup() {
Serial.begin(115200);


Serial.println("Setting AP (Access Point)…");
WiFi.softAP(ssid, password);  



IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);
Serial.print("MAC address: ");
Serial.println(WiFi.softAPmacAddress());
server.begin();
}

void loop(){

WiFiClient client = server.available();   // Listen for incoming clients

if (client) 
{ Serial.println("New Client.");          

while (client.connected()) 
{           
Serial.println(client.connected());
Serial.println("Client connected.");
Serial.println("");

}
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}

客户端:

#include <WiFi.h>
 #include <SPI.h>
const char* ssid = "ESP32";
const char* password =  "123";



void setup() {

  Serial.begin(115200);
    scanNetworks();
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to WiFi..");
  }

   Serial.println("Connected to the WiFi network");

    }

void loop() {

}
void scanNetworks() {
  // scan for nearby networks:
  Serial.println("** Scan Networks **");
  byte numSsid = WiFi.scanNetworks();

  // print the list of networks seen:
  Serial.print("SSID List:");
  Serial.println(numSsid);
  // print the network number and name for each network found:
  for (int thisNet = 0; thisNet<numSsid; thisNet++) {
    Serial.print(thisNet);
    Serial.print(") Network: ");
    Serial.println(WiFi.SSID(thisNet));
  }
}

1 个答案:

答案 0 :(得分:0)

您需要将数据从客户端发送到服务器。 WiFiServerAvailable::available将在以下情况下返回内容:

  

获取连接到服务器并具有可用数据的客户端   阅读。

并且:

  

返回一个Client对象;如果没有客户可读取的数据,   该对象将在if语句中评估为false

按照documentation

无需在服务器端进行任何读取,但这将导致无限循环,因为WifiClient::connected将返回true,直到读取数据并关闭连接为止。按照documentation