简体   繁体   English

ESP32 转 ESP32 WiFi 服务器/客户端问题

[英]ESP32 to ESP32 WiFi Server/Client Problem

I've got one ESP32 acting as client and another ESP32 acting as an access-point for direct communication and outdoor use.我有一个 ESP32 作为客户端,另一个 ESP32 作为接入点,用于直接通信和户外使用。 I have set up a server on the AP end and would like the client to communicate with it but I can't seem to make this work.我已经在 AP 端设置了一个服务器,并希望客户端与其通信,但我似乎无法完成这项工作。

I would like to know two things:我想知道两件事:

  1. How do I send or write data to the server from the client?如何从客户端向服务器发送或写入数据?
  2. How do I read and display the data that was sent to the server from the client?如何读取和显示从客户端发送到服务器的数据?

I have attached the code below:我附上了下面的代码:

Code for AP/Server AP/服务器代码

//SERVER
//Load Wi-Fi library
#include <WiFi.h>

// Replace with your network credentials
const char* ssid     = "ESP32-Access-Point";
const char* password = "SyedAhmedAli";
 //Set web server port number to 80

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) 
  {                                          //If a new client connects,
    Serial.println("New Client.");           //print a message out in the serial port





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

     if (client.available() > 0) 
     {
      // read the bytes incoming from the client:
      char thisChar = client.read();
      // echo the bytes back to the client:
      server.write(thisChar);
      // echo the bytes to the server as well:
      Serial.write(thisChar);
     }


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

Code for Client客户端代码

//Client
#include <WiFi.h>

const char* ssid = "ESP32-Access-Point";
const char* password =  "SyedAhmedAli";


WiFiClient client;

IPAddress server(192, 168, 4, 1);    

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

  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
   while (WiFi.status() != WL_CONNECTED)
   {
    delay(500);
    Serial.print(".");
   }
    Serial.println(" connected");
     if(client.connect(server, 80))
      {
        Serial.println("connected to server");
        client.write("Data");
      }
     else
     {
        Serial.println("failed to connect to server");
     }

}


void loop()
{

}

Alternatively to the previous answer, you could use espnow as a protocol between various esp32.除了上一个答案,您可以使用 espnow 作为各种 esp32 之间的协议。 Here an example .这里有一个例子

You must implement some sort of protocol like TCP, UDP to exchange data.您必须实现某种协议,如 TCP、UDP 来交换数据。

Example Project using TCP https://www.instructables.com/id/WiFi-Communication-Between-Two-ESP8266-Based-MCU-T/使用 TCP 的示例项目https://www.instructables.com/id/WiFi-Communication-Between-Two-ESP8266-Based-MCU-T/

Example Project using UDP https://circuits4you.com/2018/01/01/esp-to-esp-communication/使用 UDP 的示例项目https://circuits4you.com/2018/01/01/esp-to-esp-communication/

Look at this very handy function:看看这个非常方便的功能:

void SetWifi(const char *name, const char *password) { // Turn on wifi with server
    Serial.println("Starting server");
    WiFi.disconnect();
    WiFi.softAP(name, password);
    delay(2000);
    IPAddress IP = WiFi.softAPIP();
    Serial.print("Server IP : ");
    Serial.println(IP);
    server.begin();
    server.setNoDelay(true);
    Serial.println("Server started");
}

You can write data with this function :您可以使用此功能写入数据:

void sendDataTCP(String message) {  // function to send message back to client
    if (client && client.connected()) { //check if client is there
        client.println(message);
    }
    client.flush();
}

Receive data with this function:用这个函数接收数据:

void availableMessage() {
    if (client.available()) {//check if client is there
        while (client.available()) {
            String message = client.readStringUntil('\n');  //read string until enter (end of message)
            Serial.println("Received: " + message);
            message.toCharArray(buffer, BUFFER);           // put message in char array (buffer)
            client.flush(); // discard all bytes that have been read
        }
    }
}

Check if someone has connected:检查是否有人已连接:

void connectClient() {
    if (server.hasClient())                          // if server has a client
    {
        if (client = server.available()) {            // if client is connected
            Serial.println("Connected");
        }
    }
}

I think this will get you in the direction of accomplishing your goal, good luck!我认为这会让你朝着实现目标的方向前进,祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM