试图从一个ESP32到另一个ESP32进行通信,一个作为ESP32,另一个充当ESP32,但似乎无法将esp客户端连接到esp AP,但是使用我的智能手机连接到AP可行。简单来说,我是esp32s和WiFI通讯的新手。
接入点代码
#include <WiFi.h>
const char* ssid = "ESP32-Access-Point";
const char* password = "SyedAhmedAli";
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-Access-Point";
const char* password = "SyedAhmedAli";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
scanNetworks();
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));
}
}
答案 0 :(得分:1)
正如@juraj所述,在ESP32的Arduino代码中,您无法在尝试进行连接到AP的情况下启动扫描。
在尝试连接之前(在scanNetworks()
之前,呼叫WiFi.begin(ssid, password);
。
或
在建立与AP的连接后(scanNetworks()
之后)呼叫while (WiFi.status() != WL_CONNECTED){}
。
无论如何尝试连接到已知的WiFi AP时,我都看不到扫描网络的任何地方。
答案 1 :(得分:0)
ESPnow(请参见示例here)很容易进行ESP到ESP的通信。 (还有ESP8266等)