├── schematic.jpg └── README.md /schematic.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/flusflas/esp32-ethernet/HEAD/schematic.jpg -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # esp32-ethernet 2 | 3 | This repository is intended to be just a simple and straightforward reminder of how to configure a wired Ethernet connection on a ESP32 using a LAN8720 module. 4 | 5 | The solucion came from [https://sautter.com/blog/ethernet-on-esp32-using-lan8720/](https://sautter.com/blog/ethernet-on-esp32-using-lan8720/) and [this issue](https://github.com/espressif/arduino-esp32/issues/2907) from the [Arduino core for the ESP32](https://github.com/espressif/arduino-esp32) repo. I simply made a few tries to come up with the same solution as **esp13** and **DarlanJurak** exposed in that issue. I'm even going to take the liberty of using their schematics and code. 6 | 7 | I don't have a deep (not even slight) knowledge of the operation of the LAN8720 module. Honestly, I just wanted it to work :) 8 | 9 | ## Wiring 10 | 11 | The wiring is exactly as in [https://sautter.com/blog/ethernet-on-esp32-using-lan8720/](https://sautter.com/blog/ethernet-on-esp32-using-lan8720/). This schematic stolen [from **esp13**](https://github.com/espressif/arduino-esp32/issues/2907) may be clearer: 12 | 13 | ![Schematic](schematic.jpg) 14 | 15 | > **Note:** According to my tests, `GPIO17` connection can be completely removed without affecting the operation of the circuit :confused:. 16 | 17 | ### Jumping the enable pin of the oscillator in the LAN8720 module and the `NC` pin 18 | I found that this workaround behaved differently on the two tested scenarios: 19 | - When programming the ESP32 using the Arduino IDE I needed to manually stop the contact immediately after powering on or resetting the board for the connection to work. 20 | - Using the ESPHome firmware this connection is not necessary at all. 21 | 22 | ## Test code 23 | 24 | ### Arduino IDE 25 | 26 | ```c 27 | /* 28 | This sketch shows how to configure different external or internal clock sources for the Ethernet PHY 29 | */ 30 | 31 | #include 32 | 33 | /* 34 | * ETH_CLOCK_GPIO0_IN - default: external clock from crystal oscillator 35 | * ETH_CLOCK_GPIO0_OUT - 50MHz clock from internal APLL output on GPIO0 - possibly an inverter is needed for LAN8720 36 | * ETH_CLOCK_GPIO16_OUT - 50MHz clock from internal APLL output on GPIO16 - possibly an inverter is needed for LAN8720 37 | * ETH_CLOCK_GPIO17_OUT - 50MHz clock from internal APLL inverted output on GPIO17 - tested with LAN8720 38 | */ 39 | #ifdef ETH_CLK_MODE 40 | #undef ETH_CLK_MODE 41 | #endif 42 | #define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT 43 | 44 | // Pin# of the enable signal for the external crystal oscillator (-1 to disable for internal APLL source) 45 | #define ETH_POWER_PIN -1 46 | 47 | // Type of the Ethernet PHY (LAN8720 or TLK110) 48 | #define ETH_TYPE ETH_PHY_LAN8720 49 | 50 | // I²C-address of Ethernet PHY (0 or 1 for LAN8720, 31 for TLK110) 51 | #define ETH_ADDR 1 52 | 53 | // Pin# of the I²C clock signal for the Ethernet PHY 54 | #define ETH_MDC_PIN 23 55 | 56 | // Pin# of the I²C IO signal for the Ethernet PHY 57 | #define ETH_MDIO_PIN 18 58 | 59 | 60 | static bool eth_connected = false; 61 | 62 | void WiFiEvent(WiFiEvent_t event) { 63 | switch (event) { 64 | case SYSTEM_EVENT_ETH_START: 65 | Serial.println("ETH Started"); 66 | //set eth hostname here 67 | ETH.setHostname("esp32-ethernet"); 68 | break; 69 | case SYSTEM_EVENT_ETH_CONNECTED: 70 | Serial.println("ETH Connected"); 71 | break; 72 | case SYSTEM_EVENT_ETH_GOT_IP: 73 | Serial.print("ETH MAC: "); 74 | Serial.print(ETH.macAddress()); 75 | Serial.print(", IPv4: "); 76 | Serial.print(ETH.localIP()); 77 | if (ETH.fullDuplex()) { 78 | Serial.print(", FULL_DUPLEX"); 79 | } 80 | Serial.print(", "); 81 | Serial.print(ETH.linkSpeed()); 82 | Serial.println("Mbps"); 83 | eth_connected = true; 84 | break; 85 | case SYSTEM_EVENT_ETH_DISCONNECTED: 86 | Serial.println("ETH Disconnected"); 87 | eth_connected = false; 88 | break; 89 | case SYSTEM_EVENT_ETH_STOP: 90 | Serial.println("ETH Stopped"); 91 | eth_connected = false; 92 | break; 93 | default: 94 | break; 95 | } 96 | } 97 | 98 | void testClient(const char * host, uint16_t port) { 99 | Serial.print("\nconnecting to "); 100 | Serial.println(host); 101 | 102 | WiFiClient client; 103 | if (!client.connect(host, port)) { 104 | Serial.println("connection failed"); 105 | return; 106 | } 107 | client.printf("GET / HTTP/1.1\r\nHost: %s\r\n\r\n", host); 108 | while (client.connected() && !client.available()); 109 | while (client.available()) { 110 | Serial.write(client.read()); 111 | } 112 | 113 | Serial.println("closing connection\n"); 114 | client.stop(); 115 | } 116 | 117 | void setup() { 118 | Serial.begin(115200); 119 | WiFi.onEvent(WiFiEvent); 120 | ETH.begin(ETH_ADDR, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLK_MODE); 121 | } 122 | 123 | 124 | void loop() { 125 | if (eth_connected) { 126 | testClient("google.com", 80); 127 | } 128 | delay(10000); 129 | } 130 | ``` 131 | 132 | ### ESPHome 133 | 134 | ```yaml 135 | esphome: 136 | name: test_esp32_ethernet 137 | platform: ESP32 138 | board: esp-wrover-kit 139 | 140 | ethernet: 141 | type: LAN8720 142 | mdc_pin: GPIO23 143 | mdio_pin: GPIO18 144 | clk_mode: GPIO17_OUT 145 | phy_addr: 1 146 | 147 | # # Optional manual IP 148 | # manual_ip: 149 | # static_ip: 192.168.1.123 150 | # gateway: 192.168.1.1 151 | # subnet: 255.255.255.0 152 | 153 | # Enable logging 154 | logger: 155 | level: VERBOSE 156 | 157 | # Enable Home Assistant API 158 | api: 159 | 160 | ota: 161 | 162 | web_server: 163 | port: 80 164 | ``` 165 | --------------------------------------------------------------------------------