0

我有 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 的书,但没有成功。我对此很陌生,感觉被卡住了。

4

3 回答 3

1

我所要做的只是将 WiFi 库从 ESP8266wifi.h 更改为与 ESP32 兼容的 Wifi.h。

来源

从代码示例中只需更改代码以满足您的特定需求。

于 2018-07-03T01:53:47.443 回答
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);
}
于 2019-04-17T14:00:42.870 回答
-1

尝试将客户端代码的IP更改为:

常量字符 * 主机 = "192.168.4.1"

提示:我在Android或iOS中使用Finger应用程序扫描网络,可以很好地查看连接的每个设备的IP。(在您的情况下,您需要在使用 Finger 之前将移动设备连接到您的 Esp32 的热点)

于 2018-05-06T14:31:43.787 回答