Skip to content

Commit 054549f

Browse files
committed
ModernEthernetTest added
1 parent 64e166d commit 054549f

File tree

1 file changed

+203
-0
lines changed

1 file changed

+203
-0
lines changed
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
2+
// work in progress ...
3+
// - status() ?
4+
// - linkStatus ?
5+
6+
#include <W5500lwIP.h>
7+
#include <NetApiHelpers.h>
8+
#include <MACAddress.h>
9+
10+
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF };
11+
12+
Wiznet5500lwIP Ethernet(17);
13+
#define EthernetClient WiFiClient
14+
15+
void setup() {
16+
17+
Serial.begin(115200);
18+
delay(500);
19+
while (!Serial);
20+
Serial.println("START");
21+
Serial.println();
22+
23+
// SPI.begin();
24+
// SPI.setClockDivider(SPI_CLOCK_DIV4);
25+
// SPI.setBitOrder(MSBFIRST);
26+
// SPI.setDataMode(SPI_MODE0);
27+
28+
Serial.println("Attempting to connect with DHCP ...");
29+
Ethernet.setHostname("arduino");
30+
testEthernet(true);
31+
32+
IPAddress ip = Ethernet.localIP();
33+
IPAddress gw = Ethernet.gatewayIP();
34+
IPAddress mask = Ethernet.subnetMask();
35+
IPAddress dns = Ethernet.dnsIP();
36+
37+
const char *serverName = "www.google.com";
38+
Serial.print("Resolve IP for ");
39+
Serial.println(serverName);
40+
IPAddress resolvedIP;
41+
Ethernet.hostByName("www.google.com", resolvedIP);
42+
Serial.print("\t");
43+
Serial.println(resolvedIP);
44+
Serial.println();
45+
46+
Ethernet.end();
47+
48+
// IPAddress ip(192, 168, 1, 177);
49+
// IPAddress gw(192, 168, 1, 1);
50+
// IPAddress dns(192, 168, 1, 1);
51+
// IPAddress mask(255, 255, 255, 0);
52+
53+
Serial.print("Configuring static IP ");
54+
ip[3] = 177;
55+
Serial.println(ip);
56+
Ethernet.config(ip, dns, gw, mask);
57+
if (Ethernet.dnsIP() == mask) {
58+
Serial.println("ERROR: config() has wrong ordering of parameters");
59+
while (true) {
60+
delay(1);
61+
}
62+
}
63+
testEthernet(false);
64+
if (ip != Ethernet.localIP()) {
65+
Serial.println("ERROR: Static IP was not used.");
66+
while (true) {
67+
delay(1);
68+
}
69+
}
70+
71+
IPAddress dnsIP2(8, 8, 8, 8);
72+
Ethernet.setDNS(Ethernet.dnsIP(), dnsIP2);
73+
if (Ethernet.dnsIP(1) != dnsIP2) {
74+
Serial.print("ERROR: DNS IP setter or getter error. dnsIP(1) returned ");
75+
Serial.println(Ethernet.dnsIP(1));
76+
Serial.println();
77+
}
78+
79+
Ethernet.end();
80+
81+
Serial.println("Attempting to connect without resetting static IP configuration"); // <-------
82+
testEthernet(false);
83+
if (ip != Ethernet.localIP()) {
84+
Serial.println("ERROR: Static IP was cleared.");
85+
}
86+
Ethernet.end();
87+
88+
Serial.print("Configuring with automatic gateway, DNS and mask with static IP "); // <-------
89+
ip[3] = 178;
90+
Serial.println(ip);
91+
Ethernet.config(ip);
92+
testEthernet(false);
93+
if (ip != Ethernet.localIP()) {
94+
Serial.println("ERROR: Static IP was not used.");
95+
while (true) {
96+
delay(1);
97+
}
98+
}
99+
IPAddress autoIP(ip);
100+
autoIP[3] = 1;
101+
IPAddress defaultMask(255, 255, 255, 0);
102+
if (Ethernet.gatewayIP() == autoIP && Ethernet.dnsIP() == autoIP && Ethernet.subnetMask() == defaultMask) {
103+
Serial.println("Automatic config values are OK");
104+
} else {
105+
Serial.println("ERROR: Automatic config values are wrong");
106+
if (Ethernet.gatewayIP() != autoIP) {
107+
Serial.print("\tgateway IP Address: ");
108+
Serial.println(Ethernet.gatewayIP());
109+
}
110+
if (Ethernet.subnetMask() != defaultMask) {
111+
Serial.print("\tsubnet IP mask: ");
112+
Serial.println(Ethernet.subnetMask());
113+
}
114+
if (Ethernet.dnsIP() != autoIP) {
115+
Serial.print("\tDNS server: ");
116+
Serial.println(Ethernet.dnsIP());
117+
}
118+
}
119+
Serial.println();
120+
121+
Ethernet.end();
122+
123+
Serial.println("Attempting to connect with DHCP again ...");
124+
Ethernet.setHostname("arduino");
125+
Ethernet.config(INADDR_NONE);
126+
testEthernet(true);
127+
if (Ethernet.localIP() == INADDR_NONE) {
128+
Serial.println("ERROR: DHCP didn't run.");
129+
} else if (Ethernet.localIP() == ip) {
130+
Serial.println("ERROR: The IP didn't change from static IP to DHCP assigned IP.");
131+
} else {
132+
Serial.println("Check on router the hostname and MAC address.");
133+
}
134+
Serial.println();
135+
136+
Serial.println("END");
137+
}
138+
139+
void loop() {
140+
}
141+
142+
void testEthernet(bool dhcp) {
143+
bool ok = Ethernet.begin(mac);
144+
if (!ok) {
145+
Serial.println("ERROR: Ethernet didn't connect");
146+
while (true) {
147+
delay(1);
148+
}
149+
}
150+
if (dhcp) {
151+
while (!Ethernet.connected()) {
152+
Serial.print(".");
153+
delay(1000);
154+
}
155+
Serial.println();
156+
}
157+
Serial.println("\t...success");
158+
Serial.println();
159+
160+
printEthernetInfo();
161+
Serial.println();
162+
163+
Serial.print("Attempt to connect to port 80 on ");
164+
Serial.println(Ethernet.gatewayIP());
165+
EthernetClient client;
166+
if (client.connect(Ethernet.gatewayIP(), 80)) {
167+
Serial.println("\t...success");
168+
} else {
169+
Serial.println("\t...ERROR");
170+
}
171+
client.stop();
172+
Serial.println();
173+
}
174+
175+
void printEthernetInfo() {
176+
177+
MACAddress mac;
178+
Ethernet.macAddress(mac);
179+
Serial.print("MAC: ");
180+
Serial.println(mac);
181+
182+
Serial.print("IP Address: ");
183+
Serial.println(Ethernet.localIP());
184+
185+
Serial.print("gateway IP Address: ");
186+
Serial.println(Ethernet.gatewayIP());
187+
188+
Serial.print("subnet IP mask: ");
189+
Serial.println(Ethernet.subnetMask());
190+
191+
Serial.print("DNS server: ");
192+
IPAddress dns = Ethernet.dnsIP();
193+
if (dns == INADDR_NONE) {
194+
Serial.println("not set");
195+
} else {
196+
Serial.println(dns);
197+
IPAddress dns2 = Ethernet.dnsIP(1);
198+
if (dns2 != INADDR_NONE) {
199+
Serial.print("DNS server2: ");
200+
Serial.println(dns2);
201+
}
202+
}
203+
}

0 commit comments

Comments
 (0)