ESP32 - Ethernet
This tutorial instructs you how to use W5500 Ethernet module to connect ESP32 to Internet or your LAN Network. In detail, we will learn:
- How to connect ESP32 to W5500 Ethernet module
- How to program ESP32 to make HTTP request via Ethernet
- How to program ESP32 to create a simple web server via Ethernet
Hardware Used In This Tutorial
1 | × | ESP-WROOM-32 Dev Module | |
1 | × | USB Cable Type-C | |
1 | × | W5500 Ethernet Module | |
1 | × | Ethernet Cable | |
1 | × | Jumper Wires | |
1 | × | Breadboard | |
1 | × | (Recommended) Screw Terminal Expansion Board for ESP32 |
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Introduction to W5500 Ethernet module
The W5500 Ethernet module has two interfaces:
- RJ45 interface: to connect to router/switch via Ethernet cable
- SPI interface: to connect to ESP32 board, including 10 pins:
- NC pin: let this pin unconnected.
- INT pin: let this pin unconnected.
- RST pin: reset pin, connect this pin to the EN pin of ESP32.
- GND pin: connect this pin to the ESP32's GND pin.
- 5V pin: let this pin unconnected.
- 3.3V pin: connect this pin to the ESP32's 3.3V pin.
- MISO pin: connect this pin to the ESP32's SPI MISO pin.
- MOSI pin: connect this pin to the ESP32's SPI MOSI pin.
- SCS pin: connect this pin to the ESP32's SPI CS pin.
- SCLK pin: connect this pin to the ESP32's SPI SCK pin.
Wiring Diagram between ESP32 and W5500 Ethernet module
This image is created using Fritzing. Click to enlarge image
If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.
ESP32 code for Ethernet Module - Making HTTP request via Ethernet
The following code functions as a web client, making HTTP requests to the web server at http://example.com/.
Quick Instructions
- If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
- Do the wiring between the Ethernet module and ESP32 as the above wiring diagram
- Connect the ESP32 to PC via an USB cable
- Connect the Ethernet module to your router/switch via an Ethernet cable
- Open Arduino IDE on your PC.
- Select the right ESP32 board (e.g. ESP32 Dev Module) and COM port.
- Click to the Libraries icon on the left bar of the Arduino IDE.
- Search “Ethernet”, then find the Ethernet library by Various
- Click Install button to install Ethernet library.
- Open Serial On Arduino IDE
- Copy the above code and paste it to Arduino IDE
- Click Upload button on Arduino IDE to upload code to ESP32
- Check out the result on Serial Monitor, it looks like below:
※ NOTE THAT:
If there is another device on the same local network with the same MAC address, it might not work.
ESP32 code for Ethernet Module - Web Server
The following code transforms the ESP32 into a web server that responds to web browsers with a simple webpage.
Quick Instructions
- Copy the above code and paste it to Arduino IDE
- Click Upload button on Arduino IDE to upload code to ESP32
- Check out the result on Serial Monitor, it looks like below:
- Copy the above IP address and paste it to the address bar of a web browser, you will see a simple webpage served by ESP32 as follows: