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

Appearance settings

Commit 3af1710

Browse filesBrowse files
committed
Add initial support for Ethernet and examples
1 parent 5c1b10f commit 3af1710
Copy full SHA for 3af1710

File tree

Expand file treeCollapse file tree

4 files changed

+499
-0
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+499
-0
lines changed
+81Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
This sketch shows the Ethernet event usage
3+
4+
*/
5+
6+
#include <ETH.h>
7+
8+
static bool eth_connected = false;
9+
10+
void WiFiEvent(WiFiEvent_t event)
11+
{
12+
switch (event) {
13+
case SYSTEM_EVENT_ETH_START:
14+
Serial.println("ETH Started");
15+
//set eth hostname here
16+
ETH.setHostname("esp32-ethernet");
17+
break;
18+
case SYSTEM_EVENT_ETH_CONNECTED:
19+
Serial.println("ETH Connected");
20+
break;
21+
case SYSTEM_EVENT_ETH_GOT_IP:
22+
Serial.print("ETH MAC: ");
23+
Serial.print(ETH.macAddress());
24+
Serial.print(", IPv4: ");
25+
Serial.print(ETH.localIP());
26+
if (ETH.fullDuplex()) {
27+
Serial.print(", FULL_DUPLEX");
28+
}
29+
Serial.print(", ");
30+
Serial.print(ETH.linkSpeed());
31+
Serial.println("Mbps");
32+
eth_connected = true;
33+
break;
34+
case SYSTEM_EVENT_ETH_DISCONNECTED:
35+
Serial.println("ETH Disconnected");
36+
eth_connected = false;
37+
break;
38+
case SYSTEM_EVENT_ETH_STOP:
39+
Serial.println("ETH Stopped");
40+
eth_connected = false;
41+
break;
42+
default:
43+
break;
44+
}
45+
}
46+
47+
void testClient(const char * host, uint16_t port)
48+
{
49+
Serial.print("\nconnecting to ");
50+
Serial.println(host);
51+
52+
WiFiClient client;
53+
if (!client.connect(host, port)) {
54+
Serial.println("connection failed");
55+
return;
56+
}
57+
client.printf("GET / HTTP/1.1\r\nHost: %s\r\n\r\n", host);
58+
while (client.connected() && !client.available());
59+
while (client.available()) {
60+
Serial.write(client.read());
61+
}
62+
63+
Serial.println("closing connection\n");
64+
client.stop();
65+
}
66+
67+
void setup()
68+
{
69+
Serial.begin(115200);
70+
WiFi.onEvent(WiFiEvent);
71+
ETH.begin();
72+
}
73+
74+
75+
void loop()
76+
{
77+
if (eth_connected) {
78+
testClient("google.com", 80);
79+
}
80+
delay(10000);
81+
}
+87Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
This sketch shows the Ethernet event usage
3+
4+
*/
5+
6+
#include <ETH.h>
7+
8+
#define ETH_ADDR 31
9+
#define ETH_POWER_PIN 17
10+
#define ETH_MDC_PIN 23
11+
#define ETH_MDIO_PIN 18
12+
#define ETH_TYPE ETH_PHY_TLK110
13+
14+
static bool eth_connected = false;
15+
16+
void WiFiEvent(WiFiEvent_t event)
17+
{
18+
switch (event) {
19+
case SYSTEM_EVENT_ETH_START:
20+
Serial.println("ETH Started");
21+
//set eth hostname here
22+
ETH.setHostname("esp32-ethernet");
23+
break;
24+
case SYSTEM_EVENT_ETH_CONNECTED:
25+
Serial.println("ETH Connected");
26+
break;
27+
case SYSTEM_EVENT_ETH_GOT_IP:
28+
Serial.print("ETH MAC: ");
29+
Serial.print(ETH.macAddress());
30+
Serial.print(", IPv4: ");
31+
Serial.print(ETH.localIP());
32+
if (ETH.fullDuplex()) {
33+
Serial.print(", FULL_DUPLEX");
34+
}
35+
Serial.print(", ");
36+
Serial.print(ETH.linkSpeed());
37+
Serial.println("Mbps");
38+
eth_connected = true;
39+
break;
40+
case SYSTEM_EVENT_ETH_DISCONNECTED:
41+
Serial.println("ETH Disconnected");
42+
eth_connected = false;
43+
break;
44+
case SYSTEM_EVENT_ETH_STOP:
45+
Serial.println("ETH Stopped");
46+
eth_connected = false;
47+
break;
48+
default:
49+
break;
50+
}
51+
}
52+
53+
void testClient(const char * host, uint16_t port)
54+
{
55+
Serial.print("\nconnecting to ");
56+
Serial.println(host);
57+
58+
WiFiClient client;
59+
if (!client.connect(host, port)) {
60+
Serial.println("connection failed");
61+
return;
62+
}
63+
client.printf("GET / HTTP/1.1\r\nHost: %s\r\n\r\n", host);
64+
while (client.connected() && !client.available());
65+
while (client.available()) {
66+
Serial.write(client.read());
67+
}
68+
69+
Serial.println("closing connection\n");
70+
client.stop();
71+
}
72+
73+
void setup()
74+
{
75+
Serial.begin(115200);
76+
WiFi.onEvent(WiFiEvent);
77+
ETH.begin(ETH_ADDR, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE);
78+
}
79+
80+
81+
void loop()
82+
{
83+
if (eth_connected) {
84+
testClient("google.com", 80);
85+
}
86+
delay(10000);
87+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.