我有 2 个 ESP32 板,我想在 Arduino IDE 中使它们成为服务器/客户端。只有两块板,中间没有路由器。
到目前为止,我已经按照教程进行操作,并且已经能够从手机连接到 ESP32。
#include <WiFi.h>
WiFiServer server;
const char *ssid = "Zupa";
const char *password = "12345678";
void setup() {
Serial.begin(115200);
Serial.println();
Serial.print("Configuring access point...");
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
}
void loop() {
}
但是,我无法从其他 ESP32 连接。代码如下:
#include <WiFi.h>
#include <WiFiMulti.h>
WiFiMulti WiFiMulti;
void setup()
{
Serial.begin(115200);
delay(10);
enter code here
// We start by connecting to a WiFi network
WiFiMulti.addAP("Zupa", "12345678");
Serial.println();
Serial.println();
Serial.print("Wait for WiFi... ");
while(WiFiMulti.run() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
delay(500);
}
void loop()
{
const uint16_t port = 80;
const char * host = "192.168.1.4"; // ip
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
if (!client.connect(host, port)) {
Serial.println("connection failed");
Serial.println("wait 5 sec...");
delay(5000);
return;
}
// This will send the request to the server
client.print("Send this data to server");
//read back one line from server
String line = client.readStringUntil('\r');
client.println(line);
Serial.println("closing connection");
client.stop();
Serial.println("wait 5 sec...");
delay(5000);
}
发生的事情是它只是说它无法连接。IP地址是默认的,我在服务器端仔细检查了!为什么我可以通过手机而不是 ESP32 连接?
此外,我将如何在两者之间进行通信?我尝试在线阅读,但似乎每个人都在做电话到 ESP 的通信,而不是 ESP 到 ESP。我也尝试阅读 Kolbans 先生关于 ESP32 的书,但没有成功。我对此很陌生,感觉被卡住了。