-
Notifications
You must be signed in to change notification settings - Fork 1
/
ModernEthernetTest.ino
245 lines (214 loc) · 5.89 KB
/
ModernEthernetTest.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include <W5500lwIP.h>
#include <NetApiHelpers.h>
#include <MACAddress.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF };
Wiznet5500lwIP Ethernet(17);
#define EthernetClient WiFiClient
void setup() {
Serial.begin(115200);
delay(500);
while (!Serial);
Serial.println("START");
Serial.println();
// SPI.begin();
// SPI.setClockDivider(SPI_CLOCK_DIV4);
// SPI.setBitOrder(MSBFIRST);
// SPI.setDataMode(SPI_MODE0);
Serial.println("Attempting to connect with DHCP ...");
Ethernet.setHostname("arduino");
testEthernet();
Serial.println("Checking link ...");
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("\tretry...");
delay(500);
}
switch (Ethernet.linkStatus()) {
case LinkOFF:
Serial.println("\tEthernet cable is not connected.");
break;
case LinkON:
Serial.println("\tEthernet cable is connected.");
break;
default:
Serial.println("\tLink state unknown.");
break;
}
Serial.println();
IPAddress ip = Ethernet.localIP();
IPAddress gw = Ethernet.gatewayIP();
IPAddress mask = Ethernet.subnetMask();
IPAddress dns = Ethernet.dnsIP();
const char *serverName = "www.google.com";
Serial.print("Resolve IP for ");
Serial.println(serverName);
IPAddress resolvedIP;
Ethernet.hostByName("www.google.com", resolvedIP);
Serial.print("\t");
Serial.println(resolvedIP);
Serial.println();
Ethernet.end();
// IPAddress ip(192, 168, 1, 177);
// IPAddress gw(192, 168, 1, 1);
// IPAddress dns(192, 168, 1, 1);
// IPAddress mask(255, 255, 255, 0);
Serial.print("Configuring static IP ");
ip[3] = 177;
Serial.println(ip);
Ethernet.config(ip, dns, gw, mask);
if (Ethernet.dnsIP() == mask) {
Serial.println("ERROR: config() has wrong ordering of parameters");
while (true) {
delay(1);
}
}
testEthernet();
if (ip != Ethernet.localIP()) {
Serial.println("ERROR: Static IP was not used.");
while (true) {
delay(1);
}
}
IPAddress dnsIP2(8, 8, 8, 8);
Ethernet.setDNS(Ethernet.dnsIP(), dnsIP2);
if (Ethernet.dnsIP(1) != dnsIP2) {
Serial.print("ERROR: DNS IP setter or getter error. dnsIP(1) returned ");
Serial.println(Ethernet.dnsIP(1));
Serial.println();
}
Ethernet.end();
Serial.println("Attempting to connect without resetting static IP configuration"); // <-------
testEthernet();
if (ip != Ethernet.localIP()) {
Serial.println("ERROR: Static IP was cleared.");
}
Ethernet.end();
Serial.print("Configuring with automatic gateway, DNS and mask with static IP "); // <-------
ip[3] = 178;
Serial.println(ip);
Ethernet.config(ip);
testEthernet();
if (ip != Ethernet.localIP()) {
Serial.println("ERROR: Static IP was not used.");
while (true) {
delay(1);
}
}
IPAddress autoIP(ip);
autoIP[3] = 1;
IPAddress defaultMask(255, 255, 255, 0);
if (Ethernet.gatewayIP() == autoIP && Ethernet.dnsIP() == autoIP && Ethernet.subnetMask() == defaultMask) {
Serial.println("Automatic config values are OK");
} else {
Serial.println("ERROR: Automatic config values are wrong");
if (Ethernet.gatewayIP() != autoIP) {
Serial.print("\tgateway IP Address: ");
Serial.println(Ethernet.gatewayIP());
}
if (Ethernet.subnetMask() != defaultMask) {
Serial.print("\tsubnet IP mask: ");
Serial.println(Ethernet.subnetMask());
}
if (Ethernet.dnsIP() != autoIP) {
Serial.print("\tDNS server: ");
Serial.println(Ethernet.dnsIP());
}
}
Serial.println();
Ethernet.end();
Serial.println("Attempting to connect with DHCP again ...");
Ethernet.setHostname("arduino");
Ethernet.config(INADDR_NONE);
testEthernet();
if (Ethernet.localIP() == INADDR_NONE) {
Serial.println("ERROR: DHCP didn't run.");
} else if (Ethernet.localIP() == ip) {
Serial.println("ERROR: The IP didn't change from static IP to DHCP assigned IP.");
} else {
Serial.println("Check on router the hostname and MAC address.");
}
Serial.println();
Serial.println("END");
}
void loop() {
}
void testEthernet() {
Ethernet.begin(mac);
while (Ethernet.status() == WL_DISCONNECTED) {
Serial.print('.');
delay(1000);
}
Serial.println();
if (Ethernet.status() != WL_CONNECTED) {
Serial.println("ERROR: Ethenet didn't connect");
printStatus();
while (true) {
delay(1);
}
} else {
Serial.println("\t...success");
}
Serial.println();
printEthernetInfo();
Serial.println();
Serial.print("Attempt to connect to port 80 on ");
Serial.println(Ethernet.gatewayIP());
EthernetClient client;
if (client.connect(Ethernet.gatewayIP(), 80)) {
Serial.println("\t...success");
} else {
Serial.println("\t...ERROR");
}
client.stop();
Serial.println();
}
void printEthernetInfo() {
MACAddress mac;
Ethernet.macAddress(mac);
Serial.print("MAC: ");
Serial.println(mac);
Serial.print("IP Address: ");
Serial.println(Ethernet.localIP());
Serial.print("gateway IP Address: ");
Serial.println(Ethernet.gatewayIP());
Serial.print("subnet IP mask: ");
Serial.println(Ethernet.subnetMask());
Serial.print("DNS server: ");
IPAddress dns = Ethernet.dnsIP();
if (dns == INADDR_NONE) {
Serial.println("not set");
} else {
Serial.println(dns);
IPAddress dns2 = Ethernet.dnsIP(1);
if (dns2 != INADDR_NONE) {
Serial.print("DNS server2: ");
Serial.println(dns2);
}
}
}
void printStatus() {
int status = Ethernet.status();
const char* msg = nullptr;
switch (status) {
case WL_NO_SHIELD:
msg = "NO_SHIELD";
break;
case WL_CONNECTED:
msg = "CONNECTED";
break;
case WL_CONNECT_FAILED:
msg = "CONNECT_FAILED";
break;
case WL_CONNECTION_LOST:
msg = "CONNECTION_LOST";
break;
case WL_DISCONNECTED:
msg = "DISCONNECTED";
break;
}
Serial.print("status: ");
if (msg != nullptr) {
Serial.println(msg);
} else {
Serial.println(status);
}
}