Arduino网络编程实战-Ethernet篇-静态方式设置IP 静态方式设置IP Arduino Ethernet Shield V1 允许 Arduino 板连接到互联网。 它基于 Wiznet W5100ethernet 芯片(数据表)。 Wiznet W5100 提供支持 TCP 和 UDP 的网络 (IP) 堆栈。 它最多支持四个同时套接字连接。
本实例将演示如何通过静态方式获取IP。
1、硬件准备
Arduino Mega 2560
Arduino Ethernet Shield
路由器(推荐可以上网、开启DHCP)
网线一条
电脑一台
2、软件准备
3、代码实现 1)添加头文件
1 2 3 #include <SPI.h> #include <Ethernet.h>
2)设置物理地址(MAC)和IP
1 2 3 4 5 6 byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress ip(192, 168, 0, 177);
3)启动网卡
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Ethernet.begin(mac,ip); // 通过设置MAC、IP,进行静态方式获取IP if (Ethernet.hardwareStatus() == EthernetNoHardware) { Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :("); while (true) { delay(1); } } if (Ethernet.linkStatus() == LinkOFF) { Serial.println("Ethernet cable is not connected."); while(true){ delay(1); } }
4)获取IP
1 2 3 4 5 6 7 8 9 Serial.print("IP:"); Serial.println(Ethernet.localIP()); // 查询分配IP Serial.print("Subnet Mask:"); Serial.println(Ethernet.subnetMask()); // 查询子网掩码 Serial.print("Gateway:"); Serial.println(Ethernet.gatewayIP()); // 查询网关IP Serial.print("DNS Server:"); Serial.println(Ethernet.dnsServerIP()); // 查询DNS服务器IP
4、运行结果
文章来源: https://iotsmart.blog.csdn.net/article/details/122659148
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!