[Digi-Key Follow Me Issue 4] Basic Task 1: Static IP Configuration, Ping, and Packet Capture Analysis
[Copy link]
[Digi-Key Follow me Issue 4] Basic Task 1: Complete the initialization of the main control board W5500 (static IP configuration), and be able to ping it using a LAN computer. At the same time, W5500 can ping Internet sites; use packet capture software (Wireshark, Sniffer, etc.) to capture the ping message of the local PC, display and analyze it.
Basic Task 1 is about basic network configuration and usage. It can be completed well in the Arduino development environment. The following is the specific process.
1. Hardware Understanding
First, find the corresponding connection pins of W5500 from the pin diagram of the development board:
As can be seen from the above figure, W5500 is directly connected to the GPIO corresponding to SPI0, and the chip select is GPIO17, which is needed when initializing the network card device.
2. Add development board support library to Arduino IDE
The address of the support library. You can use either of the following two options. You do not need to set both at the same time:
After the settings are completed, wait for the library information to be automatically updated, and then install the corresponding board library:
After the installation is complete, connect the development board to the computer. For the first time, it is best to hold down the BOOT button and connect it to the computer until a USB flash drive appears on the computer and then release the BOOT button:
Then select the development board:
To provide support for the W5500 wired network module, you also need to install the following libraries:
To implement the ping function on the development board, you also need to install the following libraries:
After downloading, copy it to the libraries directory of Arduino for later use.
After the installation is complete, in the Arduino file menu, check whether they can be correctly recognized:
3. Static IP Configuration
The Ethernet library is configured to support DHCP connections by default, and can also support the setting of static IP, DNS server, gateway, and subnet mask through initialization settings.
The specific code is as follows:
#include <SPI.h>
#include <Ethernet.h>
// 网卡mac地址
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xE1, 0xF2, 0xE3 };
// 静态ip地址、DNS服务、网关、子网掩码
// byte ip[] = { 192, 168, 1, 188 };
IPAddress ip(192, 168, 1, 188);
IPAddress myDns(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
void setup() {
// 配置串口
Serial.begin(115200);
while (!Serial) {
; // 等待串口连接
}
// 静态IP设置
Serial.println("Ethernet Begin");
Ethernet.init(17);
Ethernet.begin(mac, ip, myDns, gateway, subnet);
// 输出网卡mac地址、IP地址、子网掩码、DNS、网关
Serial.print("My Mac address: ");
byte macBuffer[6]; // 设置mac地址存储buff
Ethernet.MACAddress(macBuffer); // 读取mac地址
for (byte octet = 0; octet < 6; octet++) {
Serial.print(macBuffer[octet], HEX);
if (octet < 5) {
Serial.print('-');
}
}
Serial.println("");
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
Serial.print("My subnet: ");
Serial.println(Ethernet.subnetMask());
Serial.print("My DNS address: ");
Serial.println(Ethernet.dnsServerIP());
Serial.print("My GateWay address: ");
Serial.println(Ethernet.gatewayIP());
}
void loop() {
}
Because the W5500 of W5500_Evb_Pico is a wired network module with SPI interface, the SPI and Ethernet libraries are called at the beginning.
Then, the corresponding mac address (EE-A1-B2-C3-D4-F5), static IP address (192.168.1.188), DNS service (192.168.1.1), gateway (192.168.1.1), subnet mask (255.255.255.0) were configured.
Use Ethernet.init(17) to chip-select the W5500 connection corresponding to W5500_Evb_Pico, and use Ethernet.begin(mac, ip, myDns, gateway, subnet) to set the above information.
Compile the above code, download the firmware to the development board, and run it. The serial port output is as follows:
At this point, you can ping the IP address of the development board on your computer:
In the management interface of the router, you can also see that the device corresponding to the MAC address and IP is connected successfully:
4. DNS resolution
To ping the domain name of an Internet website, you need to use a domain name resolution service.
In the above code, add the following part to use the domain name resolution service:
// DNS
#include <Dns.h>
// DNS
DNSClient dnClient;
// DNS解析用的ip
IPAddress dstip;
void setup() {
// 在联网成功以后,添加如下的代码
// 解析域名
dnClient.begin(Ethernet.dnsServerIP());
const char domains[3][20] = { "www.eeworld.com.cn", "www.digikey.cn", "www.digikey.com" };
for (int i = 0; i < 3; i++) {
if (dnClient.getHostByName(domains[i], dstip) == 1) {
Serial.print(domains[i]);
Serial.print(" = ");
Serial.println(dstip);
} else Serial.println(F("dns lookup failed"));
}
}
After compiling and downloading, the running results are as follows:
5. Ping Internet Sites
In the previous step, the domain name resolution has been successfully implemented. In the next step, you can use the obtained IP address to Ping.
Use the EthernetICMP library installed earlier to complete the Ping task. The specific code added is as follows:
// ICMP
#include <EthernetICMP.h>
// ICMP Ping
char buffer[256];
SOCKET pingSocket = 0;
EthernetICMPPing ping(pingSocket, (uint16_t)random(0, 255));
// Ping测试
void pingTest() {
EthernetICMPEchoReply echoReply = ping(dstip, 4);
if (echoReply.status == SUCCESS) {
sprintf(buffer,
"Reply[%d] from: %d.%d.%d.%d: bytes=%d time=%ldms TTL=%d",
echoReply.data.seq,
echoReply.addr[0],
echoReply.addr[1],
echoReply.addr[2],
echoReply.addr[3],
REQ_DATASIZE,
millis() - echoReply.data.time,
echoReply.ttl);
} else {
sprintf(buffer, "Echo request failed; %d", echoReply.status);
}
Serial.println(buffer);
}
void setup() {
// 在联网成功以后,添加如下的代码
// 解析域名
dnClient.begin(Ethernet.dnsServerIP());
const char domains[3][20] = { "www.eeworld.com.cn", "www.digikey.cn", "www.digikey.com" };
for (int i = 0; i < 3; i++) {
if (dnClient.getHostByName(domains[i], dstip) == 1) {
Serial.print(domains[i]);
Serial.print(" = ");
Serial.println(dstip);
if (i == 0)
pingTest();
} else Serial.println(F("dns lookup failed"));
}
}
In the above code, a ping operation is performed on the resolved IP address of the first domain name.
After compiling and downloading, the running results are as follows:
6. Packet capture tool to analyze ping messages
Analyzing network packets is very suitable for using Wireshark. After installing and opening it, select the network card connected to the LAN:
After clicking, you can start to find and analyze the captured packets.
1. Ping the development board on your computer:
By default, Wireshark finds all traffic, which is difficult to analyze. You can use the following filter:
ip.dst==192.168.1.188 and ip.src==192.168.1.100
This filter indicates that the target IP of the data packet is 192.168.1.188, which is the IP address of the development board, and the source address is the local IP 192.168.1.100. After applying it, you can find the actual data you need:
Click on one to analyze:
It is clear from what address Src, to what address Dst, and what protocol Protocol.
2. Ping the computer from the development board:
To ping a computer from the development board, add the following code to the loop() section:
void loop() {
dstip.fromString("192.168.1.100");
pingTest();
delay(1000);
}
The above IP address is the IP address of the computer.
After compiling and downloading, the running results are as follows:
At this point, in Wireshark, apply the following filter rules:
ip.src==192.168.1.188 and ip.dst==192.168.1.100
It can be clearly seen from the address Src 192.168.1.188 to the address Dst 192.168.1.100, as well as the protocol Protocol (Echo (ping) request).
VII. Conclusion
At this point, the basic network configuration, domain name resolution, Ping, and message analysis on W5500_Evb_Pico have been completed.
On this basis, network services and network request tasks can be carried out subsequently.
In addition, Wireshark is a very powerful network packet capture and analysis tool. It is the best tool for network debugging. Skillful use of it will greatly enhance network debugging and development work.
|