1
1
2
- // work in progress ...
3
- // - status() ?
4
- // - linkStatus ?
5
-
6
2
#include < W5500lwIP.h>
7
3
#include < NetApiHelpers.h>
8
4
#include < MACAddress.h>
@@ -27,7 +23,25 @@ void setup() {
27
23
28
24
Serial.println (" Attempting to connect with DHCP ..." );
29
25
Ethernet.setHostname (" arduino" );
30
- testEthernet (true );
26
+ testEthernet ();
27
+
28
+ Serial.println (" Checking link ..." );
29
+ if (Ethernet.linkStatus () == LinkOFF) {
30
+ Serial.println (" \t retry..." );
31
+ delay (500 );
32
+ }
33
+ switch (Ethernet.linkStatus ()) {
34
+ case LinkOFF:
35
+ Serial.println (" \t Ethernet cable is not connected." );
36
+ break ;
37
+ case LinkON:
38
+ Serial.println (" \t Ethernet cable is connected." );
39
+ break ;
40
+ default :
41
+ Serial.println (" \t Link state unknown." );
42
+ break ;
43
+ }
44
+ Serial.println ();
31
45
32
46
IPAddress ip = Ethernet.localIP ();
33
47
IPAddress gw = Ethernet.gatewayIP ();
@@ -60,7 +74,7 @@ void setup() {
60
74
delay (1 );
61
75
}
62
76
}
63
- testEthernet (false );
77
+ testEthernet ();
64
78
if (ip != Ethernet.localIP ()) {
65
79
Serial.println (" ERROR: Static IP was not used." );
66
80
while (true ) {
@@ -79,7 +93,7 @@ void setup() {
79
93
Ethernet.end ();
80
94
81
95
Serial.println (" Attempting to connect without resetting static IP configuration" ); // <-------
82
- testEthernet (false );
96
+ testEthernet ();
83
97
if (ip != Ethernet.localIP ()) {
84
98
Serial.println (" ERROR: Static IP was cleared." );
85
99
}
@@ -89,7 +103,7 @@ void setup() {
89
103
ip[3 ] = 178 ;
90
104
Serial.println (ip);
91
105
Ethernet.config (ip);
92
- testEthernet (false );
106
+ testEthernet ();
93
107
if (ip != Ethernet.localIP ()) {
94
108
Serial.println (" ERROR: Static IP was not used." );
95
109
while (true ) {
@@ -123,7 +137,7 @@ void setup() {
123
137
Serial.println (" Attempting to connect with DHCP again ..." );
124
138
Ethernet.setHostname (" arduino" );
125
139
Ethernet.config (INADDR_NONE);
126
- testEthernet (true );
140
+ testEthernet ();
127
141
if (Ethernet.localIP () == INADDR_NONE) {
128
142
Serial.println (" ERROR: DHCP didn't run." );
129
143
} else if (Ethernet.localIP () == ip) {
@@ -139,22 +153,22 @@ void setup() {
139
153
void loop () {
140
154
}
141
155
142
- void testEthernet (bool dhcp) {
143
- bool ok = Ethernet.begin (mac);
144
- if (!ok) {
145
- Serial.println (" ERROR: Ethernet didn't connect" );
156
+ void testEthernet () {
157
+ Ethernet.begin (mac);
158
+ while (Ethernet.status () == WL_DISCONNECTED) {
159
+ Serial.print (' .' );
160
+ delay (1000 );
161
+ }
162
+ Serial.println ();
163
+ if (Ethernet.status () != WL_CONNECTED) {
164
+ Serial.println (" ERROR: Ethenet didn't connect" );
165
+ printStatus ();
146
166
while (true ) {
147
167
delay (1 );
148
168
}
169
+ } else {
170
+ Serial.println (" \t ...success" );
149
171
}
150
- if (dhcp) {
151
- while (!Ethernet.connected ()) {
152
- Serial.print (" ." );
153
- delay (1000 );
154
- }
155
- Serial.println ();
156
- }
157
- Serial.println (" \t ...success" );
158
172
Serial.println ();
159
173
160
174
printEthernetInfo ();
@@ -201,3 +215,31 @@ void printEthernetInfo() {
201
215
}
202
216
}
203
217
}
218
+
219
+ void printStatus () {
220
+ int status = Ethernet.status ();
221
+ const char * msg = nullptr ;
222
+ switch (status) {
223
+ case WL_NO_SHIELD:
224
+ msg = " NO_SHIELD" ;
225
+ break ;
226
+ case WL_CONNECTED:
227
+ msg = " CONNECTED" ;
228
+ break ;
229
+ case WL_CONNECT_FAILED:
230
+ msg = " CONNECT_FAILED" ;
231
+ break ;
232
+ case WL_CONNECTION_LOST:
233
+ msg = " CONNECTION_LOST" ;
234
+ break ;
235
+ case WL_DISCONNECTED:
236
+ msg = " DISCONNECTED" ;
237
+ break ;
238
+ }
239
+ Serial.print (" status: " );
240
+ if (msg != nullptr ) {
241
+ Serial.println (msg);
242
+ } else {
243
+ Serial.println (status);
244
+ }
245
+ }
0 commit comments