我有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。我也曾尝试在ESP32上阅读Kolbans先生的书,但没有成功。我对此很陌生,感到困惑。
答案 0 :(得分:1)
答案 1 :(得分:0)
#include <WiFi.h>
#include <WiFiClient.h>
void setup()
{
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
WiFi.mode(WIFI_STA); //Set wifi mode as station
WiFi.begin("Zupa", "12345678");//Connect to other ESP32
Serial.println();
Serial.println();
Serial.print("Wait for WiFi... ");
//Wait for WiFi to connect
while(WiFi.waitForConnectResult() != WL_CONNECTED){
Serial.print(".");
delay(1000);
}
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);
}
答案 2 :(得分:-1)
尝试将客户端代码的IP更改为:
const char * host =&#34; 192.168.4.1&#34;
提示:我使用Android或iOS中的Finger应用程序扫描网络, 很高兴看到每台设备的IP连接。 (在您的情况下,您需要在使用手指之前将移动设备连接到Esp32的热点)