将静态 IP 地址与 W5100S-EVB-Pico 板和 Arduino IDE 结合使用

本文提供了一个使用ArduinoIDE和W5100S-EVB-Pico开发板设置静态IP地址的教程。通过修改代码中的ip变量,用户可以为设备分配静态IP。在上传代码后,静态IP地址将显示在串行监视器上,便于网络通信。该示例适用于需要固定IP地址的项目,可扩展至创建Web服务器或实现TCP/UDP功能。
摘要由CSDN通过智能技术生成
通过这个简单的示例,了解如何使用 Arduino IDE 和以太网库为 W5100S-EVB-Pico 板设置静态 IP 地址。

转发: Using a Static IP Address with the W5100S-EVB-Pico Board and Arduino IDE


项目介绍

本文介绍如何使用 Arduino IDE 为 W5100S-EVB-Pico 板配置静态 IP 地址。 本文包含代码示例以及使用静态 IP 地址设置开发板的说明。 它还提供了有关代码的要求、用法和示例输出的详细信息。

要求

要遵循本指南,您将需要: • W5100S-EVB-Pico 板 • 带有 W5100S-EVB-Pico 板支持并安装了 WIZnet 以太网库的 Arduino IDE。

代码说明 该代码使用 CS 引脚初始化以太网连接,并使用静态 IP 地址启动以太网连接。 ip 变量用于指定要使用的静态 IP 地址。 然后,静态 IP 地址将打印到串行监视器。

设置

要使用静态 IP 地址配置 W5100S-EVB-Pico 板,请执行以下步骤:

  1. 使用 USB 电缆将 W5100S-EVB-Pico 板连接到计算机。
  2. 打开 Arduino IDE,然后从“工具”>“板”菜单中选择“W5100S-EVB-Pico”板。
  3. 打开此存储库中提供的“Static_IP_Address”草图。
  4. 编辑 ip 变量以设置所需的静态 IP 地址。
  5. 将草图上传到 W5100S-EVB-Pico 板。

代码

#include <SPI.h> // Include the SPI library (used for communication between the microcontroller and the W5100S chip)
#include <Ethernet.h> // Include the Ethernet library

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

// Enter a static IP address for your controller below.
IPAddress ip(192, 168, 1, 177); // Example static IP address

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 a static IP address:
  Serial.println("Configuring Ethernet with a static IP address...");
  Ethernet.begin(mac, ip);

  // Print the static IP address:
  Serial.print("Static IP address: ");
  Serial.println(Ethernet.localIP());
}

void loop() {
  // Nothing to do here. This is just a Static IP Address example.
}

用法

要使用代码示例,请按照下列步骤操作:

  1. 转到“工具”>“串行监视器”,在 Arduino IDE 中打开串行监视器。
  2. 在串行监视器中将波特率设置为 9600。
  3. 该草图将使用静态 IP 地址配置以太网连接。 静态 IP 地址将打印到串行监视器。

示例输出

串行监视器上的输出应类似于以下内容:

Configuring Ethernet with a static IP address...
Static IP address: 192.168.1.177` 

具体的 IP 地址将取决于您在 ip 变量中设置的静态 IP 地址。

结果

笔记

本文作为学习如何在 Arduino IDE 中使用 W5100S-EVB-Pico 板配置静态 IP 地址的起点。 您可以通过实现其他以太网功能来扩展此示例,例如创建 Web 服务器或客户端以及使用 TCP/UDP 套接字。 当您想要确保将特定 IP 地址分配给您的设备并且不想依赖 DHCP 动态分配 IP 地址时,使用静态 IP 地址非常有用。

文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值