-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLegacyEthernetTest.ino
206 lines (176 loc) · 5.08 KB
/
LegacyEthernetTest.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
#include <Ethernet.h>
#include <NetApiHelpers.h>
#include <MACAddress.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF };
void setup() {
Serial.begin(115200);
delay(500);
while (!Serial);
Serial.println("START");
Serial.println();
// Ethernet.init(10);
Serial.println("Attempting to connect with DHCP ...");
testEthernet(true);
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
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.dnsServerIP();
const char *serverName = "www.google.com";
Serial.print("Resolve IP for ");
Serial.println(serverName);
IPAddress resolvedIP;
if (Ethernet.hostByName("www.google.com", resolvedIP) == 1) {
Serial.print("\t");
Serial.println(resolvedIP);
} else {
Serial.println("\t...ERROR");
}
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.begin(mac, ip, dns, gw, mask);
if (Ethernet.dnsServerIP() == mask) {
Serial.println("ERROR: begin() has wrong ordering of parameters");
while (true) {
delay(1);
}
}
testEthernet(false);
if (ip != Ethernet.localIP()) {
Serial.println("ERROR: Static IP was not used.");
while (true) {
delay(1);
}
}
IPAddress dnsIP2(8, 8, 8, 8);
Ethernet.setDnsServerIP(dnsIP2);
if (Ethernet.dnsServerIP() != dnsIP2) {
Serial.print("ERROR: DNS IP setter or getter error. dnsServerIP() returned ");
Serial.println(Ethernet.dnsServerIP());
Serial.println();
}
Ethernet.setDnsServerIP(dns);
Ethernet.end();
Serial.print("Configuring with automatic gateway, DNS and mask with static IP "); // <-------
ip[3] = 178;
Serial.println(ip);
Ethernet.begin(mac, ip);
testEthernet(false);
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.dnsServerIP() == 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.dnsServerIP() != autoIP) {
Serial.print("\tDNS server: ");
Serial.println(Ethernet.dnsServerIP());
}
}
Serial.println();
Ethernet.end();
Serial.println("Attempting to connect with DHCP again ...");
testEthernet(true);
if (ip == Ethernet.localIP()) {
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() {
Ethernet.maintain();
}
void testEthernet(bool dhcp) {
if (dhcp) {
Ethernet.setHostname("arduino");
if (!Ethernet.begin(mac)) {
Serial.println("\t...ERROR");
while (true) {
delay(1);
}
} else {
Serial.println("\t...success");
}
}
Serial.println();
printEthernetStatus();
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 printEthernetStatus() {
MACAddress mac;
Ethernet.MACAddress(mac);
Serial.print("MAC: ");
Serial.println(mac);
if (mac[0] & 1) { // unicast bit is set
Serial.println("\t is the ordering of the MAC address bytes reversed?");
}
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.dnsServerIP();
if (dns == INADDR_NONE) {
Serial.println("not set");
} else {
Serial.println(dns);
}
}