使用 W5100S-EVB-Pico 板深入创建 TCP 服务器

使用 W5100S-EVB-Pico 和 Arduino IDE,可以轻松设置 TCP 服务器、监控传入的客户端消息并以友好的问候进行响应。

转发:


项目介绍

当我们逐步使用 Arduino IDE 创建 TCP 服务器时,您将了解 W5100S-EVB-Pico 的强大功能,这款开发板在物联网领域开辟了道路。 这种有效的组合简化了服务器与客户端的通信,因为服务器不仅侦听传入连接,而且还通过“Hello,Client!”来发送热烈的欢迎。 向每个新连接发送消息。

你的工具箱

在我们深入讨论之前,请确保您已配备以下设备:

  1. W5100S-EVB-Pico 板
  2. 您计算机上安装的 Arduino IDE
  3. 以太网电缆
  4. 搭建舞台

首先,配置 Arduino IDE 以与 W5100S-EVB-Pico 板配合使用,如主存储库的 README 中所述。 一旦您的 IDE 启动并运行了必要的库,就可以在 IDE 中加载 TCP 服务器示例代码。

准备、设置、连接!

使用以太网电缆将 W5100S-EVB-Pico 板连接到本地网络。 现在是时候编译代码并将其上传到您的主板了。 随后,在Arduino IDE中打开Serial Monitor并将波特率设置为9600,确保通信通道畅通。

代码

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address for your controller below.
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};

const int csPin = 17; // Chip Select (CS) pin for W5100S on W5100S-EVB-Pico
const int serverPort = 80; // The port number on which the server will listen for incoming connections

EthernetServer server(serverPort);

void setup() {
  // Open the serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // Initialize Ethernet with the CS pin:
  Ethernet.init(csPin);

  // Start the Ethernet connection using DHCP:
  Serial.println("Attempting to obtain IP address using DHCP...");
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to obtain IP address using DHCP");
  } else {
    // Print the obtained IP address:
    Serial.print("Successfully obtained IP address: ");
    Serial.println(Ethernet.localIP());
  }

  // Start the server:
  server.begin();
  Serial.print("Server started on port ");
  Serial.println(serverPort);
}

void loop() {
  // Listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("New client connected");
    // Read the data from the client
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.print("Received from client: ");
        Serial.println(c);

        // Send a response to the client
        client.println("Hello, Client!");
      }
    }
    // Close the connection:
    client.stop();
    Serial.println("Client disconnected");
  }
}

行动中

当 W5100S-EVB-Pico 作为服务器启动时,它会侦听传入连接、读取来自客户端的消息并对每个连接做出响应。 串行监视器上的输出将如下所示:

Attempting to obtain IP address using DHCP...
Successfully obtained IP address: 192.168.1.123
Server started on port 80
New client connected
Received from client: H
Received from client: e
Received from client: l
Received from client: l
Received from client: o
Client disconnected

记住…

确保 W5100S-EVB-Pico 连接到本地网络,并且您的网络允许指定端口上的传入连接。 通过使用 TCP 客户端(甚至可能是 TCP 客户端示例中创建的客户端)连接到服务器来测试您的服务器!

此示例的优点在于代码不复杂,为理解 IoT 应用程序中 TCP 服务器的功能和功能奠定了坚实的基础。 利用 W5100S-EVB-Pico 和 Arduino IDE,无论您是物联网领域经验丰富的程序员还是新手,您都可以解锁网络通信的新水平。 现在,继续探索这项令人兴奋的技术,让您的想法变成现实!

文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值