【问题标题】:ESP32 to ESP32 WiFi Server/Client ProblemESP32 到 ESP32 WiFi 服务器/客户端问题
【发布时间】:2018-12-08 13:04:24
【问题描述】:

我有一个 ESP32 作为客户端,另一个 ESP32 作为直接通信和户外使用的接入点。 我已经在 AP 端设置了一个服务器,并希望客户端与之通信,但我似乎无法完成这项工作。

我想知道两件事:

  1. 如何从客户端向服务器发送或写入数据?
  2. 如何读取和显示从客户端发送到服务器的数据?

我已附上以下代码:

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();
  }
}

客户代码

//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()
{

}

【问题讨论】:

  • 为清晰起见编辑了语法。编辑列表和段落的格式。
  • 查看 WiFiClient 示例

标签: c++ arduino wifi microcontroller esp32


【解决方案1】:

作为上一个答案的替代方案,您可以使用 espnow 作为各种 esp32 之间的协议。这里是example

【讨论】:

    【解决方案2】:

    你必须实现某种协议,如 TCP、UDP 来交换数据。

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

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

    【讨论】:

      【解决方案3】:

      看看这个非常方便的功能:

      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");
      }
      

      你可以用这个函数写数据:

      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();
      }
      

      用这个函数接收数据:

      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
              }
          }
      }
      

      检查是否有人连接:

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

      我认为这会让你朝着实现目标的方向前进,祝你好运!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-09-08
        • 2022-06-24
        • 1970-01-01
        • 2020-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多