Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7ce4010

Browse files
updated tests/examples
1 parent 054549f commit 7ce4010

File tree

5 files changed

+87
-48
lines changed

5 files changed

+87
-48
lines changed

‎examples/LegacyEthernetTest/LegacyEthernetTest.ino‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ void setup() {
1414
Serial.println();
1515

1616
// Ethernet.init(10);
17-
// Ethernet.begin(mac, IPAddress(182, 168, 0, 177)); // force hw init for some libraries
17+
18+
Serial.println("Attempting to connect with DHCP ...");
19+
testEthernet(true);
1820

1921
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
2022
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
@@ -40,9 +42,6 @@ void setup() {
4042
}
4143
Serial.println();
4244

43-
Serial.println("Attempting to connect with DHCP ...");
44-
testEthernet(true);
45-
4645
IPAddress ip = Ethernet.localIP();
4746
IPAddress gw = Ethernet.gatewayIP();
4847
IPAddress mask = Ethernet.subnetMask();
@@ -145,6 +144,7 @@ void setup() {
145144
}
146145

147146
void loop() {
147+
Ethernet.maintain();
148148
}
149149

150150
void testEthernet(bool dhcp) {

‎examples/ModernEthernetTest/ModernEthernetTest.ino‎

Lines changed: 63 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11

2-
// work in progress ...
3-
// - status() ?
4-
// - linkStatus ?
5-
62
#include <W5500lwIP.h>
73
#include <NetApiHelpers.h>
84
#include <MACAddress.h>
@@ -27,7 +23,25 @@ void setup() {
2723

2824
Serial.println("Attempting to connect with DHCP ...");
2925
Ethernet.setHostname("arduino");
30-
testEthernet(true);
26+
testEthernet();
27+
28+
Serial.println("Checking link ...");
29+
if (Ethernet.linkStatus() == LinkOFF) {
30+
Serial.println("\tretry...");
31+
delay(500);
32+
}
33+
switch (Ethernet.linkStatus()) {
34+
case LinkOFF:
35+
Serial.println("\tEthernet cable is not connected.");
36+
break;
37+
case LinkON:
38+
Serial.println("\tEthernet cable is connected.");
39+
break;
40+
default:
41+
Serial.println("\tLink state unknown.");
42+
break;
43+
}
44+
Serial.println();
3145

3246
IPAddress ip = Ethernet.localIP();
3347
IPAddress gw = Ethernet.gatewayIP();
@@ -60,7 +74,7 @@ void setup() {
6074
delay(1);
6175
}
6276
}
63-
testEthernet(false);
77+
testEthernet();
6478
if (ip != Ethernet.localIP()) {
6579
Serial.println("ERROR: Static IP was not used.");
6680
while (true) {
@@ -79,7 +93,7 @@ void setup() {
7993
Ethernet.end();
8094

8195
Serial.println("Attempting to connect without resetting static IP configuration"); // <-------
82-
testEthernet(false);
96+
testEthernet();
8397
if (ip != Ethernet.localIP()) {
8498
Serial.println("ERROR: Static IP was cleared.");
8599
}
@@ -89,7 +103,7 @@ void setup() {
89103
ip[3] = 178;
90104
Serial.println(ip);
91105
Ethernet.config(ip);
92-
testEthernet(false);
106+
testEthernet();
93107
if (ip != Ethernet.localIP()) {
94108
Serial.println("ERROR: Static IP was not used.");
95109
while (true) {
@@ -123,7 +137,7 @@ void setup() {
123137
Serial.println("Attempting to connect with DHCP again ...");
124138
Ethernet.setHostname("arduino");
125139
Ethernet.config(INADDR_NONE);
126-
testEthernet(true);
140+
testEthernet();
127141
if (Ethernet.localIP() == INADDR_NONE) {
128142
Serial.println("ERROR: DHCP didn't run.");
129143
} else if (Ethernet.localIP() == ip) {
@@ -139,22 +153,22 @@ void setup() {
139153
void loop() {
140154
}
141155

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();
146166
while (true) {
147167
delay(1);
148168
}
169+
} else {
170+
Serial.println("\t...success");
149171
}
150-
if (dhcp) {
151-
while (!Ethernet.connected()) {
152-
Serial.print(".");
153-
delay(1000);
154-
}
155-
Serial.println();
156-
}
157-
Serial.println("\t...success");
158172
Serial.println();
159173

160174
printEthernetInfo();
@@ -201,3 +215,31 @@ void printEthernetInfo() {
201215
}
202216
}
203217
}
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+
}

‎examples/WiFiAdvancedChatServer/WiFiAdvancedChatServer.ino‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,10 @@ void setup() {
3939
Serial.print("Attempting to connect to SSID \"");
4040
Serial.print(ssid);
4141
Serial.println("\" with DHCP ...");
42-
int status = WiFi.begin(ssid, pass);
43-
// status = WiFi.waitForConnectResult();
44-
if (status != WL_CONNECTED) {
45-
Serial.println("STA didn't connect");
46-
while (true) {
47-
delay(1);
48-
}
49-
} else {
50-
Serial.println("\t...success");
42+
WiFi.begin(ssid, pass);
43+
while (WiFi.status() != WL_CONNECTED) {
44+
Serial.print('.');
45+
delay(1000);
5146
}
5247
Serial.println();
5348

‎examples/WiFiPagerServer/WiFiPagerServer.ino‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,12 @@ void setup() {
4646
Serial.print("Attempting to connect to SSID \"");
4747
Serial.print(ssid);
4848
Serial.println("\" with DHCP ...");
49-
int status = WiFi.begin(ssid, pass);
50-
// status = WiFi.waitForConnectResult();
51-
if (status != WL_CONNECTED) {
52-
Serial.println("STA didn't connect");
53-
while (true) {
54-
delay(1);
55-
}
49+
WiFi.begin(ssid, pass);
50+
while (WiFi.status() != WL_CONNECTED) {
51+
Serial.print('.');
52+
delay(1000);
5653
}
54+
Serial.println();
5755

5856
server.begin();
5957

‎examples/WiFiTest/WiFiTest.ino‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ void setup() {
103103
testWiFi();
104104
if (ip != WiFi.localIP()) {
105105
Serial.println("ERROR: Static IP was cleared.");
106+
Serial.println();
106107
}
107108
WiFi.disconnect();
108109
// while (WiFi.status() == WL_CONNECTED) {
@@ -154,7 +155,6 @@ void setup() {
154155
Serial.println("\" with DHCP again..."); // <-------
155156
WiFi.setHostname("arduino");
156157
WiFi.config(INADDR_NONE);
157-
// WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
158158
testWiFi();
159159
if (WiFi.localIP() == INADDR_NONE) {
160160
Serial.println("ERROR: DHCP didn't run.");
@@ -172,11 +172,15 @@ void loop() {
172172
}
173173

174174
void testWiFi() {
175-
int status = WiFi.begin(ssid, pass);
176-
// status = WiFi.waitForConnectResult();
177-
if (status != WL_CONNECTED) {
175+
WiFi.begin(ssid, pass);
176+
while (WiFi.status() == WL_DISCONNECTED) {
177+
Serial.print('.');
178+
delay(1000);
179+
}
180+
Serial.println();
181+
if (WiFi.status() != WL_CONNECTED) {
178182
Serial.println("ERROR: STA didn't connect");
179-
printWiFiStatus(status);
183+
printWiFiStatus();
180184
while (true) {
181185
delay(1);
182186
}
@@ -215,7 +219,6 @@ void printWiFiInfo() {
215219

216220
MACAddress bssid;
217221
WiFi.BSSID(bssid);
218-
// bssid = WiFi.BSSID();
219222
Serial.print("BSSID: ");
220223
Serial.println(bssid);
221224
if (bssid[0] & 1) { // multicast bit is set
@@ -249,7 +252,8 @@ void printWiFiInfo() {
249252
}
250253
}
251254

252-
void printWiFiStatus(int status) {
255+
void printWiFiStatus() {
256+
int status = WiFi.status();
253257
const char* msg = nullptr;
254258
switch (status) {
255259
case WL_NO_SHIELD:

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /