Interfacing ESP32 with W5500 ethernet module

I am trying to interface ESP32 with W5500 and run a ethernet webserver using Arduino IDE. I am using Asyncwebserver_ESP32_W5500 library.

Details of the library are as below

AsyncSimpleServer_ESP32_W5500.ino

For W5500 LwIP Ethernet in ESP32 (ESP32 + W5500)

AsyncWebServer_ESP32_W5500 is a library for the LwIP Ethernet W5500 in ESP32 to run AsyncWebServer

Based on and modified from ESPAsyncWebServer (GitHub - me-no-dev/ESPAsyncWebServer: Async Web Server for ESP8266 and ESP32)
Built by Khoi Hoang GitHub - khoih-prog/AsyncWebServer_ESP32_W5500: Asynchronous HTTP and WebSocket Server Library for (ESP32 + LwIP W5500). Now supporting using CString to save heap to send very large data and with examples to demo how to use beginChunkedResponse() to send large html in chunks
Licensed under GPLv3 license

There is no issue with compilation and upload. I am getting Ethernet connected message on serial monitor from ESP32. But when I connect ethernet cable from W5500 to ethernet port of linux machine I am getting trying to assign address message on linux machine and there is no further progress in ethernet connection

A ifconfig command output is as below

enp2s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 ether fc:aa:14:1a:d7:6e txqueuelen 1000 (Ethernet) RX packets 189 bytes 62718 (62.7 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 818 bytes 126627 (126.6 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 147200 bytes 191072676 (191.0 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 147200 bytes 191072676 (191.0 MB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

Ethernet server sketch uses following IP configuration

IPAddress myIP(192, 168, 2, 232);

IPAddress myGW(192, 168, 2, 1);

IPAddress mySN(255, 255, 255, 0);

// Google DNS Server IP

IPAddress myDNS(8, 8, 8, 8);

But sketch also asks me to use IP address according to my local network which I am unable to sort out.

with ESP32 Arduino version 3 you can use my EthernetESP32 library with esp32 web server libraries

I updated the version and tried with the web server example but no luck. I used esp32 example from ESP32 - Ethernet | ESP32 Tutorial and could find the webserver example as below

I updated the esp32 version to 3.1.1 and used a ETH_W5500_Arduino_SPI.ino

/*
    This sketch shows the Ethernet event usage

*/

#include <ETH.h>
#include <SPI.h>

// Set this to 1 to enable dual Ethernet support
#define USE_TWO_ETH_PORTS 0

#ifndef ETH_PHY_CS
#define ETH_PHY_TYPE ETH_PHY_W5500
#define ETH_PHY_ADDR 1
#define ETH_PHY_CS   5
#define ETH_PHY_IRQ  4
#define ETH_PHY_RST  25
#endif

// SPI pins
#define ETH_SPI_SCK  18
#define ETH_SPI_MISO 19
#define ETH_SPI_MOSI 23

#if USE_TWO_ETH_PORTS
// Second port on shared SPI bus
#ifndef ETH1_PHY_CS
#define ETH1_PHY_TYPE ETH_PHY_W5500
#define ETH1_PHY_ADDR 1
#define ETH1_PHY_CS   32
#define ETH1_PHY_IRQ  33
#define ETH1_PHY_RST  18
#endif
ETHClass ETH1(1);
#endif

static bool eth_connected = false;

void onEvent(arduino_event_id_t event, arduino_event_info_t info) {
  switch (event) {
    case ARDUINO_EVENT_ETH_START:
      Serial.println("ETH Started");
      //set eth hostname here
      ETH.setHostname("esp32-eth0");
      break;
    case ARDUINO_EVENT_ETH_CONNECTED: Serial.println("ETH Connected"); break;
    case ARDUINO_EVENT_ETH_GOT_IP:    Serial.printf("ETH Got IP: '%s'\n", esp_netif_get_desc(info.got_ip.esp_netif)); Serial.println(ETH);
#if USE_TWO_ETH_PORTS
      Serial.println(ETH1);
#endif
      eth_connected = true;
      break;
    case ARDUINO_EVENT_ETH_LOST_IP:
      Serial.println("ETH Lost IP");
      eth_connected = false;
      break;
    case ARDUINO_EVENT_ETH_DISCONNECTED:
      Serial.println("ETH Disconnected");
      eth_connected = false;
      break;
    case ARDUINO_EVENT_ETH_STOP:
      Serial.println("ETH Stopped");
      eth_connected = false;
      break;
    default: break;
  }
}

void testClient(const char *host, uint16_t port) {
  Serial.print("\nconnecting to ");
  Serial.println(host);

  NetworkClient client;
  if (!client.connect(host, port)) {
    Serial.println("connection failed");
    return;
  }
  client.printf("GET / HTTP/1.1\r\nHost: %s\r\n\r\n", host);
  while (client.connected() && !client.available());
  while (client.available()) {
    Serial.write(client.read());
  }

  Serial.println("closing connection\n");
  client.stop();
}

void setup() {
  Serial.begin(115200);
  Network.onEvent(onEvent);

  SPI.begin(ETH_SPI_SCK, ETH_SPI_MISO, ETH_SPI_MOSI);
  ETH.begin(ETH_PHY_TYPE, ETH_PHY_ADDR, ETH_PHY_CS, ETH_PHY_IRQ, ETH_PHY_RST, SPI);
#if USE_TWO_ETH_PORTS
  ETH1.begin(ETH1_PHY_TYPE, ETH1_PHY_ADDR, ETH1_PHY_CS, ETH1_PHY_IRQ, ETH1_PHY_RST, SPI);
#endif
}

void loop() {
  if (eth_connected) {
    testClient("google.com", 80);
  }
  delay(10000);
}

And the output on the serial monitor from ESP32 is as below

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)

configsip: 0, SPIWP:0xee

clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00

mode:DIO, clock div:1

load:0x3fff0030,len:4916

load:0x40078000,len:16436

load:0x40080400,len:4

ho 8 tail 4 room 4

load:0x40080404,len:3524

entry 0x400805b8

ETH Started

ETH Connected

ETH Lost IP

There is some issue with IP assignment. Can you please throw some light on the solution for getting the IP assigned?

Seems to me that you don't have an IP set in Linux, correct? What Linux flavour?

Hi,

I am using Linux mint.

I dont have idea of setting IP. Please share me any resource which can guide me in this regards.

Deepak

First of all, do you have a local network or will this be a peer to peeer only between your PC and ESP32? I'll explain briefly, as it is peer to peer:

IPAddress myIP(192, 168, 2, 232);
IPAddress mySN(255, 255, 255, 0);

These two lines gives that you have a network of 192.168.2.x
Your PC must have an IP inside that range, lets pick 192.168.2.230. This address and the subnetmask of 255.255.255.0 is all that is needed.

I have no experience in Linux Mint, but what I understand NetworkManager is used. If you search for 'set static IP networkmanager' you should find the information you need.

One more thing: I would remove the following lines since they are not required and might even give you trouble:

IPAddress myGW(192, 168, 2, 1);

// Google DNS Server IP

IPAddress myDNS(8, 8, 8, 8);

Hi,

I was able to assign static IP in the network manager and the output of Ifconfig is as below

enp2s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.2.232  netmask 255.255.255.0  broadcast 192.168.2.255
        inet6 fe80::ad2e:773a:a52d:7e98  prefixlen 64  scopeid 0x20<link>
        ether fc:aa:14:1a:d7:6e  txqueuelen 1000  (Ethernet)
        RX packets 197  bytes 68950 (68.9 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 255  bytes 29554 (29.5 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

When I ping the IP 192.168.2.232 I get the following response

PING 192.168.2.232 (192.168.2.232) 56(84) bytes of data.
64 bytes from 192.168.2.232: icmp_seq=1 ttl=64 time=0.043 ms
64 bytes from 192.168.2.232: icmp_seq=2 ttl=64 time=0.023 ms
64 bytes from 192.168.2.232: icmp_seq=3 ttl=64 time=0.028 ms
64 bytes from 192.168.2.232: icmp_seq=4 ttl=64 time=0.045 ms
64 bytes from 192.168.2.232: icmp_seq=5 ttl=64 time=0.042 ms
64 bytes from 192.168.2.232: icmp_seq=6 ttl=64 time=0.061 ms
64 bytes from 192.168.2.232: icmp_seq=7 ttl=64 time=0.064 ms
64 bytes from 192.168.2.232: icmp_seq=8 ttl=64 time=0.046 ms
64 bytes from 192.168.2.232: icmp_seq=9 ttl=64 time=0.025 ms
64 bytes from 192.168.2.232: icmp_seq=10 ttl=64 time=0.052 ms
64 bytes from 192.168.2.232: icmp_seq=11 ttl=64 time=0.054 ms
64 bytes from 192.168.2.232: icmp_seq=12 ttl=64 time=0.029 ms

But the output of the serial monitor still remains the same as below

rst:0x1 (POWERON_RESET),boot:0x1b (SPI_FAST_FLASH_BOOT)

configsip: 0, SPIWP:0xee

clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00

mode:DIO, clock div:1

load:0x3fff0030,len:4916

load:0x40078000,len:16436

load:0x40080400,len:4

ho 8 tail 4 room 4

load:0x40080404,len:3524

entry 0x400805b8

ETH Started

ETH Connected

ETH Lost IP

I used telnet command to check the port connection and the output is as below

deepak@deepak-H81M-S:~$ telnet 192.168.232.2 80
Trying 192.168.232.2...
telnet: Unable to connect to remote host: Connection timed out

There is some issue in connectivity between IP address and webserver port which I am unable to sort

Successful ping shows that the ESP's network is ok..

If you get message

and ping still works, the code is flawed, or that part is related to DHCP. DHCP isn't used when you set a static IP:

Successful ping shows that the ESP's network is ok.. :innocent:

is it
.2.232
or
.232.2
?

Sorry for the typo, the IP I used for ping purpose i.e. 192.168.2.232 is the static IP I used for assignment. But telnet response doesn't differ as below

deepak@deepak-H81M-S:~$ telnet 192.168.2.232 80
Trying 192.168.2.232...
telnet: Unable to connect to remote host: Connection refused

I conclude this thread with: Your network setup is fine. The telnet test gave the expected result, from a code point of view. To have a webserver running you need to load that functionality into the code.

Feel free to appoint solution to the answer that helped you, and good luck :cowboy_hat_face:

Hi Juraj,

I happened to visit your git hub repository and tried to compile the sample hello server.ino from the link EthernetESP32/examples/HelloServer/HelloServer.ino at master · Networking-for-Arduino/EthernetESP32 · GitHub

But I am unable to initialize the W5500 pin connection with ESP32 in example sketch

The connections of my board as below

W5500 -> ESP32

MOSI -> Pin 23
MISO-> Pin 19
CLK-> Pin18
SS/CS-> Pin 5
RST-> Pin 25

Please guide me in this regards.

don't wire RST

https://github.com/Networking-for-Arduino/EthernetESP32?tab=readme-ov-file#wiring

there is a typo Setup.begin(sck, miso, mosi); should be SPI.begin(sck, miso, mosi);

Hi Juraj,

I compiled the helloserver example from your git hub repository but faced some dependency errors as below

/home/deepak/.arduino15/packages/esp32/tools/esp-x32/2405/bin/../lib/gcc/xtensa-esp-elf/13.2.0/../../../../xtensa-esp-elf/bin/ld: /home/deepak/.cache/arduino/sketches/36E10FFF9F3770FEF4C425691E9AA737/sketch/sketch_mar19a.ino.cpp.o:(.literal.startup._GLOBAL__sub_I_driver+0x8): undefined reference to `_ZTV11W5500Driver'
/home/deepak/.arduino15/packages/esp32/tools/esp-x32/2405/bin/../lib/gcc/xtensa-esp-elf/13.2.0/../../../../xtensa-esp-elf/bin/ld: /home/deepak/.cache/arduino/sketches/36E10FFF9F3770FEF4C425691E9AA737/sketch/sketch_mar19a.ino.cpp.o:(.literal._Z5setupv+0x4): undefined reference to `Ethernet'
/home/deepak/.arduino15/packages/esp32/tools/esp-x32/2405/bin/../lib/gcc/xtensa-esp-elf/13.2.0/../../../../xtensa-esp-elf/bin/ld: /home/deepak/.cache/arduino/sketches/36E10FFF9F3770FEF4C425691E9AA737/sketch/sketch_mar19a.ino.cpp.o:(.literal._Z5setupv+0x48): undefined reference to `_ZN13EthernetClass4initER9EthDriver'
/home/deepak/.arduino15/packages/esp32/tools/esp-x32/2405/bin/../lib/gcc/xtensa-esp-elf/13.2.0/../../../../xtensa-esp-elf/bin/ld: /home/deepak/.cache/arduino/sketches/36E10FFF9F3770FEF4C425691E9AA737/sketch/sketch_mar19a.ino.cpp.o:(.literal._Z5setupv+0x4c): undefined reference to `_ZN13EthernetClass5beginEm'
/home/deepak/.arduino15/packages/esp32/tools/esp-x32/2405/bin/../lib/gcc/xtensa-esp-elf/13.2.0/../../../../xtensa-esp-elf/bin/ld: /home/deepak/.cache/arduino/sketches/36E10FFF9F3770FEF4C425691E9AA737/sketch/sketch_mar19a.ino.cpp.o: in function `_Z5setupv':
/home/deepak/Arduino/sketch_mar19a/sketch_mar19a.ino:49:(.text._Z5setupv+0x84): undefined reference to `_ZN13EthernetClass4initER9EthDriver'
/home/deepak/.arduino15/packages/esp32/tools/esp-x32/2405/bin/../lib/gcc/xtensa-esp-elf/13.2.0/../../../../xtensa-esp-elf/bin/ld: /home/deepak/Arduino/sketch_mar19a/sketch_mar19a.ino:52:(.text._Z5setupv+0x9e): undefined reference to `_ZN13EthernetClass5beginEm'
collect2: error: ld returned 1 exit status

exit status 1

Compilation error: exit status 1

Can you guide me into how to overcome these errors? I have updated ESP32 to latest version. Thankyou

did you install the library?

Hi Juraj,

I missed installing library. Compilation and uploading is smooth. Thanks for your support.

But there is some issue from my linux machine while initializing and assigning IP using DHCP, I tried to edit yaml file of netplan but no success.

Will your hello server example work with static IP allocation or is it only compatible with DHCP initialization? Pl clarify.

Thank You

of course it will work with static IP

Hi Juraj,

I have an update for you.

I couldn't configure my linux system as a dhcp server and hence was facing problem.

I purchased a TP link archer C6 wireless router and configured it as DHCP server.

I connected my IOT gateway with ESP32 and W5500 module configured with your hello server code.

Router was able to assign the IP address and HTTP server was online.

Code worked like a charm.

Thanks for the code.

with regards,
Deepak

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.