You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

技术求助:Windows部署TCP Server、ESP32部署TCP Client遇阻,求示例参考

ESP32 TCP Client Example for Your String Modification Use Case

Hey there! I totally get how overwhelming it can feel setting up your first ESP32 TCP client when you're new to this tech. Let's walk through a complete, easy-to-follow example that does exactly what you need: connects to your Windows TCP server, receives a string, modifies it, and sends it back. We'll use the Arduino framework since it's super beginner-friendly.

Step 1: ESP32 TCP Client Code

Here's a full, commented sketch you can copy-paste into the Arduino IDE. Make sure you update the WiFi credentials and your Windows server's IP/port to match your setup:

#include <WiFi.h>

// Replace with your WiFi credentials
const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASSWORD";

// Replace with your Windows PC's local IP and chosen port
const char* serverIP = "192.168.1.100";
const uint16_t serverPort = 1234;

WiFiClient client;

void setup() {
  Serial.begin(115200);
  delay(10);

  // Connect to WiFi
  Serial.println("Connecting to WiFi...");
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nWiFi connected! IP address: ");
  Serial.println(WiFi.localIP());

  // Connect to TCP server
  Serial.println("\nConnecting to TCP server...");
  if (client.connect(serverIP, serverPort)) {
    Serial.println("Connected to server successfully!");
    client.println("Hello from ESP32 TCP Client!"); // Send initial message
  } else {
    Serial.println("Failed to connect to server. Check IP/port and try again.");
  }
}

void loop() {
  // Check if we're still connected to the server
  if (!client.connected()) {
    Serial.println("\nDisconnected from server. Reconnecting...");
    // Try to reconnect every 5 seconds
    delay(5000);
    if (client.connect(serverIP, serverPort)) {
      Serial.println("Reconnected to server!");
      client.println("Reconnected: Hello from ESP32!");
    }
    return;
  }

  // Check if there's data available from the server
  if (client.available() > 0) {
    String receivedString = client.readStringUntil('\n');
    Serial.print("Received from server: ");
    Serial.println(receivedString);

    // Modify the string (customize this part to your needs!)
    // Example 1: Convert to uppercase
    String modifiedString = receivedString.toUpperCase();
    // Example 2: Add a prefix
    // String modifiedString = "[ESP32 Modified] " + receivedString;

    Serial.print("Sending back modified string: ");
    Serial.println(modifiedString);
    client.println(modifiedString); // Send modified string back
  }

  delay(100);
}

Step 2: Key Parts Explained

Let's break down the important bits so you understand what's happening:

  • WiFi Connection: The code first connects your ESP32 to your local WiFi network—this is required to reach your Windows server on the same network.
  • TCP Client Initialization: WiFiClient is the core library that handles TCP connections for ESP32.
  • Server Connection: client.connect(serverIP, serverPort) establishes a connection to your Windows server. Make sure your PC's firewall allows incoming connections on the port you choose!
  • Data Handling: The loop checks if there's incoming data from the server, reads it, modifies it (you can tweak the modification logic to whatever you need), and sends it back.

Step 3: Testing with Windows TCP Server

Since you're a beginner, you don't need to write a custom server right away. Use these easy tools to test:

  • Netcat (Command Line): If you have Windows 10/11, you can use the built-in nc command. Open Command Prompt and run:
    nc -l -p 1234
    
    This starts a TCP server listening on port 1234. You can type strings and press Enter to send them to the ESP32, and you'll see the modified response come back.
  • Graphical TCP Test Tool: If you prefer a GUI, search for a free lightweight TCP test tool—most let you send/receive strings with just a few clicks, no coding needed.

Step 4: Debugging Tips

If you run into issues, try these:

  • Double-check that your ESP32 and Windows PC are on the same WiFi network.
  • Verify your Windows PC's local IP address (run ipconfig in Command Prompt to find it).
  • Make sure your Windows firewall isn't blocking the port you're using. You may need to add an inbound rule for the port.
  • Use the Arduino Serial Monitor (set to 115200 baud) to see debug messages—this will tell you if the ESP32 connected to WiFi/server successfully, and what data it's receiving/sending.

内容的提问来源于stack exchange,提问作者Tadas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:57:36