ESP8266 - Ethernet

This guide shows you how to connect the ESP8266 to the Internet or your local network using the W5500 Ethernet module. Here's what we will discuss:

ESP8266 NodeMCU Ethernet

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×W5500 Ethernet Module
1×Ethernet Cable
1×Jumper Wires
1×Breadboard
1×(Optional) Screw Terminal Expansion Board for ESP8266

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of W5500 Ethernet module

The W5500 Ethernet module has two kinds of connections:

  • RJ45 interface: Connect this to a network device like a router or a switch using an Ethernet cable.
  • SPI interface: Connect this to an ESP8266 board using the following connections for its 10 pins:
    • NC pin: Leave this pin unconnected.
    • INT pin: Leave this pin unconnected.
    • RST pin: Connect this to the reset (EN) pin on the ESP8266.
    • GND pin: Connect this to the ground (GND) pin on the ESP8266.
    • 5V pin: Do not connect this pin.
    • 3.3V pin: Connect this to the 3.3V pin on the ESP8266.
    • MISO pin: Connect this to the SPI MISO pin on the ESP8266.
    • MOSFi pin: Connect this to the SPI MOSI pin on the ESP8266.
    • SCS pin: Connect this to the SPI CS (Chip Select) pin on the ESP8266.
    • SCLK pin: Connect this to the SPI SCK (Clock) pin on the Arauthenticated.
    Ethernet module pinout
    image source: diyables.io

Wiring Diagram between ESP8266 and W5500 Ethernet module

The wiring diagram between ESP8266 NodeMCU and Ethernet module

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

image source: diyables.io

ESP8266 code for Ethernet Module - Making HTTP request via Ethernet

This code works as a web client. It sends HTTP requests to the web server at http://example.com/.

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-ethernet */ #include <SPI.h> #include <Ethernet.h> // replace the MAC address below by the MAC address printed on a sticker on the Arduino Shield 2 byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF }; EthernetClient client; int HTTP_PORT = 80; String HTTP_METHOD = "GET"; // or POST char HOST_NAME[] = "example.com"; String PATH_NAME = "/"; void setup() { Serial.begin(9600); delay(1000); Serial.println("ESP8266 - Ethernet Tutorial"); // initialize the Ethernet shield using DHCP: if (Ethernet.begin(mac) == 0) { Serial.println("Failed to obtaining an IP address"); // check for Ethernet hardware present if (Ethernet.hardwareStatus() == EthernetNoHardware) Serial.println("Ethernet shield was not found"); // check for Ethernet cable if (Ethernet.linkStatus() == LinkOFF) Serial.println("Ethernet cable is not connected."); while (true) ; } // connect to web server on port 80: if (client.connect(HOST_NAME, HTTP_PORT)) { // if connected: Serial.println("Connected to server"); // make a HTTP request: // send HTTP header client.println(HTTP_METHOD + " " + PATH_NAME + " HTTP/1.1"); client.println("Host: " + String(HOST_NAME)); client.println("Connection: close"); client.println(); // end HTTP header while (client.connected()) { if (client.available()) { // read an incoming byte from the server and print it to serial monitor: char c = client.read(); Serial.print(c); } } // the server's disconnected, stop the client: client.stop(); Serial.println(); Serial.println("disconnected"); } else { // if not connected: Serial.println("connection failed"); } } void loop() { }

Detailed Instructions

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Connect the Ethernet module to the ESP8266 board as shown in the wiring diagram.
  • Use an Ethernet cable to connect the Ethernet module to your router or switch.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective 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.
ESP8266 NodeMCU Ethernet library
  • Open the Serial Monitor in the Arduino IDE.
  • Copy the code given and paste it into the Arduino IDE.
  • Press the Upload button in the Arduino IDE to send the code to the ESP25.
  • Check out the result on the Serial Monitor which will show the result as below.
COM6
Send
ESP8266 - Ethernet Tutorial Connected to server HTTP/1.1 200 OK Accept-Ranges: bytes Age: 208425 Cache-Control: max-age=604800 Content-Type: text/html; charset=UTF-8 Date: Fri, 12 Jul 2024 07:08:42 GMT Etag: "3147526947" Expires: Fri, 19 Jul 2024 07:08:42 GMT Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT Server: ECAcc (lac/55B8) Vary: Accept-Encoding X-Cache: HIT Content-Length: 1256 Connection: close <!doctype html> <html> <head> <title>Example Domain</title> <meta charset="utf-8" /> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> </head> <body> <div> <h1>Example Domain</h1> <p>This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.</p> <p><a href="https://www.iana.org/domains/example">More information...</a></p> </div> </body> </html> disconnected
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTE THAT:

If another device on the same network has the same MAC address, it may not function properly.

ESP8266 code for Ethernet Module - Web Server

The code below transforms the ESP8266 into a web server. This server delivers a basic webpage to internet browsers.

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-ethernet */ #include <SPI.h> #include <Ethernet.h> // replace the MAC address below by the MAC address printed on a sticker on the Arduino Shield 2 byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF }; EthernetServer server(80); void setup() { Serial.begin(9600); delay(1000); Serial.println("ESP8266 - Ethernet Tutorial"); // initialize the Ethernet shield using DHCP: if (Ethernet.begin(mac) == 0) { Serial.println("Failed to obtaining an IP address"); // check for Ethernet hardware present if (Ethernet.hardwareStatus() == EthernetNoHardware) Serial.println("Ethernet shield was not found"); // check for Ethernet cable if (Ethernet.linkStatus() == LinkOFF) Serial.println("Ethernet cable is not connected."); while (true) ; } server.begin(); Serial.print("ESP8266 - Web Server IP Address: "); Serial.println(Ethernet.localIP()); } void loop() { // listen for incoming clients EthernetClient client = server.available(); if (client) { Serial.println("new client"); // an HTTP request ends with a blank line bool currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); Serial.write(c); // if you've gotten to the end of the line (received a newline // character) and the line is blank, the HTTP request has ended, // so you can send a reply if (c == '\n' && currentLineIsBlank) { // send a standard HTTP response header client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println("Connection: close"); // the connection will be closed after completion of the response client.println(); client.println("<!DOCTYPE HTML>"); client.println("<html>"); client.println("<body>"); client.println("<h1>ESP8266 - Web Server with Ethernet</h1>"); client.println("</body>"); client.println("</html>"); break; } if (c == '\n') { // you're starting a new line currentLineIsBlank = true; } else if (c != '\r') { // you've gotten a character on the current line currentLineIsBlank = false; } } } // give the web browser time to receive the data delay(1); // close the connection: client.stop(); Serial.println("client disconnected"); } }

Detailed Instructions

  • Copy the above code and paste it into the Arduino IDE.
  • Click the Upload button in Arduino IDE to transfer the code to the ESP8266.
  • Check the results on the Serial Monitor; they will display as described.
COM6
Send
ESP8266 - Ethernet Tutorial ESP8266 - Web Server IP Address: 192.168.0.2
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Copy the IP address given above and enter it into your web browser’s address bar. You will see a simple webpage displayed by the ESP8266.
ESP8266 NodeMCU Ethernet Web Server

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!