Skip to content

Commit 05a1b80

Browse files
committed
Fix WebClient example
1 parent c39cfee commit 05a1b80

File tree

1 file changed

+90
-95
lines changed

1 file changed

+90
-95
lines changed

libraries/Ethernet/examples/WebClient/WebClient.ino

Lines changed: 90 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@
1414
1515
*/
1616

17+
#include <PortentaEthernet.h>
1718
#include <SPI.h>
18-
#include <Ethernet.h>
1919

2020
// Enter a MAC address for your controller below.
2121
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
22-
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
22+
// byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
2323

2424
// if you don't want to use DNS (and reduce your sketch size)
2525
// use the numeric IP instead of the name for the server:
2626
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
27-
char server[] = "www.google.com"; // name address for Google (using DNS)
27+
char server[] = "www.google.com"; // name address for Google (using DNS)
2828

2929
// Set the static IP address to use if the DHCP fails to assign
30-
IPAddress ip(192, 168, 0, 177);
31-
IPAddress myDns(192, 168, 0, 1);
30+
IPAddress ip(192, 168, 2, 177);
31+
IPAddress myDns(192, 168, 2, 1);
3232

3333
// Initialize the Ethernet client library
3434
// with the IP address and port of the server
@@ -38,100 +38,95 @@ EthernetClient client;
3838
// Variables to measure the speed
3939
unsigned long beginMicros, endMicros;
4040
unsigned long byteCount = 0;
41-
bool printWebData = true; // set to false for better speed measurement
42-
43-
void setup() {
44-
// You can use Ethernet.init(pin) to configure the CS pin
45-
//Ethernet.init(10); // Most Arduino shields
46-
//Ethernet.init(5); // MKR ETH shield
47-
//Ethernet.init(0); // Teensy 2.0
48-
//Ethernet.init(20); // Teensy++ 2.0
49-
//Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
50-
//Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
51-
52-
// Open serial communications and wait for port to open:
53-
Serial.begin(9600);
54-
while (!Serial) {
55-
; // wait for serial port to connect. Needed for native USB port only
56-
}
57-
58-
// start the Ethernet connection:
59-
Serial.println("Initialize Ethernet with DHCP:");
60-
if (Ethernet.begin(mac) == 0) {
61-
Serial.println("Failed to configure Ethernet using DHCP");
62-
// Check for Ethernet hardware present
63-
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
64-
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
65-
while (true) {
66-
delay(1); // do nothing, no point running without Ethernet hardware
67-
}
41+
bool printWebData = true; // set to false for better speed measurement
42+
43+
void setup()
44+
{
45+
46+
// Open serial communications and wait for port to open:
47+
Serial.begin(9600);
48+
while (!Serial) {
49+
; // wait for serial port to connect. Needed for native USB port only
50+
}
51+
52+
// start the Ethernet connection:
53+
Serial.println("Initialize Ethernet with DHCP:");
54+
if (Ethernet.begin() == 0) {
55+
Serial.println("Failed to configure Ethernet using DHCP");
56+
// Check for Ethernet hardware present
57+
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
58+
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
59+
while (true) {
60+
delay(1); // do nothing, no point running without Ethernet hardware
61+
}
62+
}
63+
if (Ethernet.linkStatus() == LinkOFF) {
64+
Serial.println("Ethernet cable is not connected.");
65+
}
66+
// try to congifure using IP address instead of DHCP:
67+
Ethernet.begin(ip, myDns);
68+
} else {
69+
Serial.print(" DHCP assigned IP ");
70+
Serial.println(Ethernet.localIP());
6871
}
69-
if (Ethernet.linkStatus() == LinkOFF) {
70-
Serial.println("Ethernet cable is not connected.");
72+
// give the Ethernet shield a second to initialize:
73+
delay(1000);
74+
Serial.print("connecting to ");
75+
Serial.print(server);
76+
Serial.println("...");
77+
78+
// if you get a connection, report back via serial:
79+
if (client.connect(server, 80)) {
80+
Serial.print("connected to ");
81+
Serial.println(client.remoteIP());
82+
// Make a HTTP request:
83+
client.println("GET /search?q=arduino HTTP/1.1");
84+
client.println("Host: www.google.com");
85+
client.println("Connection: close");
86+
client.println();
87+
} else {
88+
// if you didn't get a connection to the server:
89+
Serial.println("connection failed");
7190
}
72-
// try to congifure using IP address instead of DHCP:
73-
Ethernet.begin(mac, ip, myDns);
74-
} else {
75-
Serial.print(" DHCP assigned IP ");
76-
Serial.println(Ethernet.localIP());
77-
}
78-
// give the Ethernet shield a second to initialize:
79-
delay(1000);
80-
Serial.print("connecting to ");
81-
Serial.print(server);
82-
Serial.println("...");
83-
84-
// if you get a connection, report back via serial:
85-
if (client.connect(server, 80)) {
86-
Serial.print("connected to ");
87-
Serial.println(client.remoteIP());
88-
// Make a HTTP request:
89-
client.println("GET /search?q=arduino HTTP/1.1");
90-
client.println("Host: www.google.com");
91-
client.println("Connection: close");
92-
client.println();
93-
} else {
94-
// if you didn't get a connection to the server:
95-
Serial.println("connection failed");
96-
}
97-
beginMicros = micros();
91+
beginMicros = micros();
9892
}
9993

100-
void loop() {
101-
// if there are incoming bytes available
102-
// from the server, read them and print them:
103-
int len = client.available();
104-
if (len > 0) {
105-
byte buffer[80];
106-
if (len > 80) len = 80;
107-
client.read(buffer, len);
108-
if (printWebData) {
109-
Serial.write(buffer, len); // show in the serial monitor (slows some boards)
94+
void loop()
95+
{
96+
// if there are incoming bytes available
97+
// from the server, read them and print them:
98+
int len = client.available();
99+
if (len > 0) {
100+
byte buffer[80];
101+
if (len > 80)
102+
len = 80;
103+
client.read(buffer, len);
104+
if (printWebData) {
105+
Serial.write(buffer, len); // show in the serial monitor (slows some boards)
106+
}
107+
byteCount = byteCount + len;
110108
}
111-
byteCount = byteCount + len;
112-
}
113-
114-
// if the server's disconnected, stop the client:
115-
if (!client.connected()) {
116-
endMicros = micros();
117-
Serial.println();
118-
Serial.println("disconnecting.");
119-
client.stop();
120-
Serial.print("Received ");
121-
Serial.print(byteCount);
122-
Serial.print(" bytes in ");
123-
float seconds = (float)(endMicros - beginMicros) / 1000000.0;
124-
Serial.print(seconds, 4);
125-
float rate = (float)byteCount / seconds / 1000.0;
126-
Serial.print(", rate = ");
127-
Serial.print(rate);
128-
Serial.print(" kbytes/second");
129-
Serial.println();
130-
131-
// do nothing forevermore:
132-
while (true) {
133-
delay(1);
109+
110+
// if the server's disconnected, stop the client:
111+
if (!client.connected()) {
112+
endMicros = micros();
113+
Serial.println();
114+
Serial.println("disconnecting.");
115+
client.stop();
116+
Serial.print("Received ");
117+
Serial.print(byteCount);
118+
Serial.print(" bytes in ");
119+
float seconds = (float)(endMicros - beginMicros) / 1000000.0;
120+
Serial.print(seconds, 4);
121+
float rate = (float)byteCount / seconds / 1000.0;
122+
Serial.print(", rate = ");
123+
Serial.print(rate);
124+
Serial.print(" kbytes/second");
125+
Serial.println();
126+
127+
// do nothing forevermore:
128+
while (true) {
129+
delay(1);
130+
}
134131
}
135-
}
136132
}
137-

0 commit comments

Comments
 (0)