8000 added Ethernet & WebServer library · fesch/arduino-esp32@ed73777 · GitHub
[go: up one dir, main page]

Skip to content

Commit ed73777

Browse files
committed
added Ethernet & WebServer library
1 parent bbcb5eb commit ed73777

File tree

26 files changed

+3575
-0
lines changed

26 files changed

+3575
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <Ethernet.h>
2+
#include "WiFi.h"
3+
4+
// declare the ethernet adapter
5+
ESP32Ethernet ethernet;
6+
7+
void WiFiEvent(WiFiEvent_t event){
8+
switch(event) {
9+
case SYSTEM_EVENT_ETH_START:
10+
//set eth hostname here
11+
ethernet.setHostname("esp32-eth");
12+
Serial.println("ETH started");
13+
Serial.print("ETH MAC: ");
14+
Serial.println(ethernet.getMacAddress());
15+
break;
16+
case SYSTEM_EVENT_ETH_CONNECTED:
17+
Serial.println("ETH connected");
18+
break;
19+
case SYSTEM_EVENT_ETH_GOT_IP:
20+
Serial.print("ETH IPv4: ");
21+
Serial.println(ethernet.localIP());
22+
break;
23+
case SYSTEM_EVENT_ETH_DISCONNECTED:
24+
Serial.println("ETH disconnected");
25+
break;
26+
default:
27+
break;
28+
}
29+
}
30+
31+
void setup(){
32+
// start the serial console
33+
Serial.begin(115200);
34+
// attach the callback event handler
35+
WiFi.onEvent(WiFiEvent);
36+
// start the ethernet
37+
ethernet.begin();
38+
}
39+
40+
void loop(){
41+
// nothing yet to do here
42+
}
43+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <Ethernet.h>
2+
#include "WiFi.h"
3+
4+
// declare the ethernet adapter
5+
ESP32Ethernet ethernet;
6+
7+
// define the network settings
8+
IPAddress ip(192, 168, 0, 123);
9+
IPAddress nm(255, 255, 255, 0);
10+
IPAddress gw(192, 168, 0, 1);
11+
12+
void WiFiEvent(WiFiEvent_t event){
13+
switch(event) {
14+
case SYSTEM_EVENT_ETH_START:
15+
//set eth hostname here
16+
ethernet.setHostname("esp32-eth");
17+
Serial.println("ETH started");
18+
Serial.print("ETH MAC: ");
19+
Serial.println(ethernet.getMacAddress());
20+
break;
21+
case SYSTEM_EVENT_ETH_CONNECTED:
22+
Serial.println("ETH connected");
23+
break;
24+
case SYSTEM_EVENT_ETH_GOT_IP:
25+
Serial.print("ETH IPv4: ");
26+
Serial.println(ethernet.localIP());
27+
break;
28+
case SYSTEM_EVENT_ETH_DISCONNECTED:
29+
Serial.println("ETH disconnected");
30+
break;
31+
default:
32+
break;
33+
}
34+
}
35+
36+
void setup(){
37+
// start the serial console
38+
Serial.begin(115200);
39+
// attach the callback event handler
40+
WiFi.onEvent(WiFiEvent);
41+
// start the ethernet
42+
ethernet.begin(ip,nm,gw);
43+
}
44+
45+
void loop(){
46+
// nothing yet to do here
47+
}
48+

libraries/Ethernet/library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=Ethernet(ESP32-EVB)
2+
version=1.0.5
3+
author=Bob Fisch
4+
maintainer=Bob Fisch <bob@fisch.lu>
5+
sentence=Enables network connection (local and Internet) the onboard Ethernet controller. (ESP32-EVB from www.olimex.com)
6+
paragraph=With this library you can use the port Ethernet to connect to Internet. The library permits you to connect to a local network also with DHCP and to resolve DNS.
7+
category=Communication
8+
url=
9+
architectures=esp32

libraries/Ethernet/src/Ethernet.cpp

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#include "Ethernet.h"
2+
3+
extern "C" {
4+
#include "WiFiGeneric.h"
5+
//void tcpipInit();
6+
};
7+
8+
9+
static eth_config_t eth_config = ETH_PHY_CONF;
10+
11+
static void ethernet_config_gpio(void){
12+
// RMII data pins are fixed:
13+
// CRS_DRV = GPIO27
14+
// TXD0 = GPIO19
15+
// TXD1 = GPIO22
16+
// TX_EN = GPIO21
17+
// RXD0 = GPIO25
18+
// RXD1 = GPIO26
19+
// CLK == GPIO0
20+
phy_rmii_configure_data_interface_pins();
21+
// MDC is GPIO 23, MDIO is GPIO 18
22+
phy_rmii_smi_configure_pins(PIN_SMI_MDC, PIN_SMI_MDIO);
23+
}
24+
25+
void tcpipInit();
26+
27+
int EthernetClass::begin()
28+
{
29+
eth_config.phy_addr = ETH_PHY_ADDR;
30+
eth_config.gpio_config = ethernet_config_gpio;
31+
eth_config.tcpip_input = tcpip_adapter_eth_input;
32+
//eth_config.phy_power_enable = ethernet_power_enable;
33+
tcpipInit();
34+
esp_err_t err = esp_eth_init(&eth_config);
35+
if(!err){
36+
err = esp_eth_enable();
37+
return 1;
38+
}
39+
return 0;
40+
}
41+
42+
int EthernetClass::config(IPAddress local_ip, IPAddress subnet, IPAddress gateway)
43+
{
44+
// stop the DHCP service
45+
if(tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_ETH) == ESP_OK) {
46+
// continue
47+
}
48+
else {
49+
return 0;
50+
}
51+
52+
// create a new TCP/IP info
53+
tcpip_adapter_ip_info_t info;
54+
info.ip.addr = static_cast<uint32_t>((local_ip));
55+
info.gw.addr = static_cast<uint32_t>((gateway));
56+
info.netmask.addr = static_cast<uint32_t>((subnet));
57+
// apply the IP settings
58+
if(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_ETH, &info) == ESP_OK) {
59+
// continue
60+
} else {
61+
return 0;
62+
}
63+
64+
return 1;
65+
}
66+
67+
int EthernetClass::begin(IPAddress local_ip, IPAddress subnet, IPAddress gateway)
68+
{
69+
// connect the callback function
70+
esp_event_loop_init(&WiFiGenericClass::_eventCallback, NULL);
71+
72+
// init the TCP/IP adapter
73+
tcpip_adapter_init();
74+
75+
// manually configure the adapter
76+
if(!config(local_ip,subnet,gateway))
77+
{
78+
return 0;
79+
}
80+
81+
eth_config.phy_addr = ETH_PHY_ADDR;
82+
eth_config.gpio_config = ethernet_config_gpio;
83+
eth_config.tcpip_input = tcpip_adapter_eth_input;
84+
//eth_config.phy_power_enable = ethernet_power_enable;
85+
esp_err_t err = esp_eth_init(&eth_config);
86+
if(!err){
87+
err = esp_eth_enable();
88+
return 1;
89+
}
90+
91+
return 0;
92+
}
93+
94+
IPAddress EthernetClass::localIP()
95+
{
96+
tcpip_adapter_ip_info_t ip;
97+
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, &ip);
98+
return IPAddress(ip.ip.addr);
99+
}
100+
101+
IPAddress EthernetClass::subnetMask()
102+
{
103+
tcpip_adapter_ip_info_t ip;
104+
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, &ip);
105+
return IPAddress(ip.netmask.addr);
106+
107+
}
108+
109+
IPAddress EthernetClass::gatewayIP()
110+
{
111+
tcpip_adapter_ip_info_t ip;
112+
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, &ip);
113+
return IPAddress(ip.gw.addr);
114+
}
115+
116+
bool EthernetClass::setHostname(const char * hostname)
117+
{
118+
return tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_ETH, hostname) == 0;
119+
}
120+
121+
bool EthernetClass::isFullDuplex()
122+
{
123+
return eth_config.phy_get_duplex_mode();
124+
}
125+
126+
uint8_t EthernetClass::getLinkSpeed()
127+
{
128+
return eth_config.phy_get_speed_mode()?100:10;
129+
}
130+
131+
String EthernetClass::getMacAddress()
132+
{
133+
uint8_t mac[6];
134+
char macStr[18] = { 0 };
135+
esp_eth_get_mac(mac);
136+
sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
137+
return String(macStr);
138+
}
139+
140+
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_ETHERNET)
141+
EthernetClass Ethernet;
142+
#endif

libraries/Ethernet/src/Ethernet.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#ifndef ethernet_h
2+
#define ethernet_h
3+
4+
//#include <inttypes.h>
5+
#include "IPAddress.h"
6+
#include "WiFi.h"
7+
#include "esp_eth.h"
8+
#include "eth_phy/phy.h"
9+
#include "eth_phy/phy_lan8720.h"
10+
11+
#define ETH_PHY_CONF phy_lan8720_default_ethernet_config
12+
#define ETH_PHY_ADDR PHY0
13+
//#define PIN_PHY_POWER 17
14+
#define PIN_SMI_MDC 23
15+
#define PIN_SMI_MDIO 18
16+
17+
class EthernetClass {
18+
private:
19+
20+
public:
21+
// Initialise the ethernet controller and gain every configuration through DHCP.
22+
// Returns 0 if the DHCP configuration failed, and 1 if it succeeded
23+
int begin();
24+
int begin(IPAddress local_ip, IPAddress subnet, IPAddress gateway);
25+
int config(IPAddress local_ip, IPAddress subnet, IPAddress gateway);
26+
27+
IPAddress localIP();
28+
IPAddress subnetMask();
29+
IPAddress gatewayIP();
30+
31+
bool setHostname(const char * hostname);
32+
bool isFullDuplex();
33+
uint8_t getLinkSpeed();
34+
String getMacAddress();
35+
36+
friend class EthernetClient;
37+
friend class EthernetServer;
38+
};
39+
40+
class ESP32Ethernet : public EthernetClass {
41+
};
42+
43+
#endif

0 commit comments

Comments
 (0)
0