Skip to content
This repository was archived by the owner on Feb 28, 2024. It is now read-only.

Commit 6cf056e

Browse files
authored
Update Ethernet.ino
1 parent 8e9c1cb commit 6cf056e

File tree

1 file changed

+77
-23
lines changed

1 file changed

+77
-23
lines changed

examples/Ethernet/Ethernet.ino

+77-23
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,84 @@
11
#include "EthernetInterface.h"
22
EthernetInterface net;
33

4-
void setup() {
5-
6-
/* ETH */
7-
int retry_count = 0;
8-
eth_restart:
9-
net.set_blocking(false);
10-
int res = net.connect();
11-
int start = millis();
12-
while ((millis() - start < 20000) && (net.get_connection_status() != NSAPI_STATUS_GLOBAL_UP)) {
13-
delay(10);
14-
}
15-
if (net.get_connection_status() != NSAPI_STATUS_GLOBAL_UP && retry_count++ < 2) {
16-
net.disconnect();
17-
goto eth_restart;
18-
}
19-
const char *mac = net.get_mac_address();
20-
Serial.println("Ethernet MAC" +String(mac));
21-
SocketAddress addr;
22-
net.get_ip_address(&addr);
23-
Serial.println("Ethernet IP" + String(addr.get_ip_address()));
4+
void setup()
5+
{
6+
Serial.begin(115200);
7+
while (!Serial)
8+
;
249

25-
}
10+
Serial.println("Ethernet example for H7 + PMC");
11+
12+
// Bring up the ethernet interface
13+
net.connect();
14+
15+
// Show the network address
16+
SocketAddress addr;
17+
net.get_ip_address(&addr);
18+
Serial.print("IP address: ");
19+
Serial.println(addr.get_ip_address() ? addr.get_ip_address() : "None");
20+
21+
// Open a socket on the network interface, and create a TCP connection to mbed.org
22+
TCPSocket socket;
23+
socket.open(&net);
24+
25+
net.gethostbyname("ifconfig.io", &addr);
26+
addr.set_port(80);
27+
socket.connect(addr);
28+
29+
String request;
30+
request += "GET / HTTP/1.1\r\n";
31+
request += "Host: ifconfig.io\r\n";
32+
request += "User-Agent: curl/7.64.1\r\n";
33+
request += "Accept: */*\r\n";
34+
request += "Connection: close\r\n";
35+
request += "\r\n";
36+
37+
auto scount = socket.send(request.c_str(), request.length());
38+
Serial.print("Sent ");
39+
Serial.print(scount);
40+
Serial.println(" bytes: ");
41+
Serial.print(request);
2642

27-
void loop() {
28-
// put your main code here, to run repeatedly:
43+
// Receive a simple HTTP response
44+
const size_t rlen { 64 };
45+
char rbuffer[rlen + 1] {};
46+
size_t rcount;
47+
size_t rec { 0 };
48+
String response;
49+
50+
while ((rec = socket.recv(rbuffer, rlen)) > 0) {
51+
rcount += rec;
52+
response += rbuffer;
53+
memset(rbuffer, 0, rlen);
54+
}
55+
Serial.print("Received ");
56+
Serial.print(rcount);
57+
Serial.println(" bytes: ");
58+
Serial.println(response);
59+
60+
const String clTag = "Content-Length: ";
61+
auto clIndex = response.indexOf(clTag);
62+
clIndex += clTag.length();
63+
auto cl = response.substring(clIndex, clIndex + 2);
64+
const String bodyTag = "\r\n\r\n";
65+
auto bodyIndex = response.indexOf(bodyTag);
66+
if (bodyIndex != -1) {
67+
bodyIndex += bodyTag.length();
68+
auto body = response.substring(bodyIndex, bodyIndex + cl.toInt());
69+
Serial.print("My public IPv4 Address is: ");
70+
Serial.println(body);
71+
}
72+
73+
74+
// Close the socket to return its memory and bring down the network interface
75+
socket.close();
76+
77+
// Bring down the ethernet interface
78+
net.disconnect();
79+
Serial.println("Done");
80+
}
2981

82+
void loop()
83+
{
3084
}

0 commit comments

Comments
 (0)