通过这个简单的示例,了解如何使用 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 板,请执行以下步骤:
- 使用 USB 电缆将 W5100S-EVB-Pico 板连接到计算机。
- 打开 Arduino IDE,然后从“工具”>“板”菜单中选择“W5100S-EVB-Pico”板。
- 打开此存储库中提供的“Static_IP_Address”草图。
- 编辑 ip 变量以设置所需的静态 IP 地址。
- 将草图上传到 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.
}
用法
要使用代码示例,请按照下列步骤操作:
- 转到“工具”>“串行监视器”,在 Arduino IDE 中打开串行监视器。
- 在串行监视器中将波特率设置为 9600。
- 该草图将使用静态 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 地址非常有用。
文件