---
title: "W5500を使ってESP32でEthernet UDP通信する"
url: "https://maker.wiznet.io/scott/projects/w5500%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6esp32%E3%81%A7ethernet-udp%E9%80%9A%E4%BF%A1%E3%81%99%E3%82%8B/"
markdown_url: "https://maker.wiznet.io/scott/projects/w5500%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6esp32%E3%81%A7ethernet-udp%E9%80%9A%E4%BF%A1%E3%81%99%E3%82%8B/md"
type: "UCC: User Created Content"
author: "Hiko"
author_url: "https://hikoleaf.hatenablog.jp/entry/2019/06/19/234339"
editor: "WIZnet"
editor_url: "https://maker.wiznet.io/"
original_author: "Hiko"
original_url: "https://hikoleaf.hatenablog.jp/entry/2019/06/19/234339"
published: "2022-09-30"
language: "en"
tags: ["W5500", "ESP32"]
hardware: ["WIZnet W5500", "Espressif ESP32"]
likes: 5
views: 5666
comments: 0
source: "WIZnet Makers (https://maker.wiznet.io/)"
---

# W5500を使ってESP32でEthernet UDP通信する

> Let's try UDP communication with ESP32 (Arduino core for the ESP32) using WIZnet's W5500.

Original author: Hiko (source: https://hikoleaf.hatenablog.jp/entry/2019/06/19/234339)

## Components

- **WIZnet W5500** x 1 ([docs](https://docs.wiznet.io/Product/Chip/Ethernet/W5500))
- **Espressif ESP32** x 1 ([docs](https://www.espressif.com/en/products/socs/esp32))

WIZnet parts: W5500 ([Datasheet](https://docs.wiznet.io/Product/Chip/Ethernet/W5500/datasheet?utm_source=maker&utm_medium=project&utm_campaign=w5500), [product hub](https://maker.wiznet.io/products/w5500/))

## Article

**1. Preparation**
Purchase W5500 on Aliexpress etc.

[1pcs USR ES1 W5500 Chip New SPI LAN Ethernet Convert TCPIP Mod - AliExpress.com | Integrated Circuits - AliExpress](https://ja.aliexpress.com/item/32598945210.html?spm=a2g0s.9042311.0.0.606e4c4d7x0z3r)

![f:id:hik0leaf:20190615222738j:plain](https://cdn-ak.f.st-hatena.com/images/fotolife/h/hik0leaf/20190615/20190615222738.jpg)

*very compact*

ESP32 uses ESP32-DevKitC. It can be purchased at Akizuki Denshi .

[akizukidenshi.com](http://akizukidenshi.com/catalog/g/gM-11819/)

**2. Connection**
Connect the W5500 and ESP32 as follows. As for the 3.3V supply, the current consumption of the W5500 is large, so it is added externally instead of from the ESP32.'

[W5500 | WIZnet Co., Ltd.](https://www.wiznet.io/product-item/w5500/)

ESP32-DevKitC uses NCP1117 that can output 1A or more to the 3.3V regulator, so I think it can be driven even if it is directly connected to W5500.

[NCP1117 Datasheet (PDF)](https://www.onsemi.jp/PowerSolutions/document/NCP1117JP-D.PDF)

![f:id:hik0leaf:20190620224653j:plain](https://cdn-ak.f.st-hatena.com/images/fotolife/h/hik0leaf/20190620/20190620224653.jpg)

*Connection diagram*

ESP32 --- W5500
IO33 --- CS
IO23 (VS_MOSI) --- MOSI
IO19 (VSMISO) --- MISO
IO18 (VS_SCK) --- SCLK
3V3 --- VCC
GND --- GND

According to the data sheet, it consumes 132mA at 100M Transmitting, so it is better to prepare an external power supply that can supply 200mA or more.

[products:w5500:datasheet \[Document Wiki\]](http://wizwiki.net/wiki/doku.php?id=products:w5500:datasheet)

![f:id:hik0leaf:20190615134207j:plain](https://cdn-ak.f.st-hatena.com/images/fotolife/h/hik0leaf/20190615/20190615134207.jpg)

*Current consumption of W5500 (Excerpt from data sheet)*

![f:id:hik0leaf:20190615134554j:plain](https://cdn-ak.f.st-hatena.com/images/fotolife/h/hik0leaf/20190615/20190615134554.jpg)

*W5500 module pinout*

**3. Library modification**
I would like to proceed with writing the code after wiring is completed, but [***Arduino***](http://d.hatena.ne.jp/keyword/Arduino)*** core for the ESP32 Ver 1.0.2*** has a defect in the library for using [Ethernet](http://d.hatena.ne.jp/keyword/Ethernet) , so it cannot be [compiled .](http://d.hatena.ne.jp/keyword/%A5%B3%A5%F3%A5%D1%A5%A4%A5%EB)

In order to be able to [compile](http://d.hatena.ne.jp/keyword/%A5%B3%A5%F3%A5%D1%A5%A4%A5%EB) , it is necessary to modify Client.h of [Arduino core for the ESP32.](http://d.hatena.ne.jp/keyword/Arduino)Client.h can be found in the path below if you installed it using Boards Manager. ( [Windows](http://d.hatena.ne.jp/keyword/Windows) )

***C:\Users\&lt;account name>\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.2\cores\esp32\Client.h***

Open Client.h in a [text editor and](http://d.hatena.ne.jp/keyword/%A5%C6%A5%AD%A5%B9%A5%C8%A5%A8%A5%C7%A5%A3%A5%BF)[comment out](http://d.hatena.ne.jp/keyword/%A5%B3%A5%E1%A5%F3%A5%C8%A5%A2%A5%A6%A5%C8) the line below .

```
// virtual int connect(IPAddress ip, uint16_t port, int timeout) =0; 
// virtual int connect(const char *host, uint16_t port, int timeout) =0;
```

You can find out more about this in the conversation below.

- [Adding ESP32 support and fix for ESP32 v 1.0.2 by kmpelectronics Pull Request #95 arduino-libraries/Ethernet GitHub](https://github.com/arduino-libraries/Ethernet/pull/95)

- [compilation error with ESP32 DIV Kit Issue #97 arduino-libraries/Ethernet GitHub](https://github.com/arduino-libraries/Ethernet/issues/97)

- [Server.h not matching Arduino API Issue #2704 espressif/arduino-esp32 GitHub](https://github.com/espressif/arduino-esp32/issues/2704)

- [Add connect with timeout to Client class espressif/arduino-esp32@9a9ff62 GitHub](https://github.com/espressif/arduino-esp32/commit/9a9ff622166149da30dd13a81485184617c56387#diff-0336fe0dc43715cecf17987314b9070b)

Also, the bug reported by this event

- [Problem with Ethernet.h compilation after board library update from 1.0.1 to 1.0.2 Issue #2786 espressif/arduino-esp32 GitHub](https://github.com/espressif/arduino-esp32/issues/2786)

- [Problem that Ethernet cannot be used with M5Stack - Qiita](https://qiita.com/skckmdtk2m/items/68880b9977a574848fd0)

- [Virtual functions in client.h are out of line with standard Arduino API Issue #2755 espressif/arduino-esp32 GitHub](https://github.com/espressif/arduino-esp32/issues/2755)

In summary, [Arduino](http://d.hatena.ne.jp/keyword/Arduino) core for the ESP32 Ver 1.0.2 seems to have a [compilation](http://d.hatena.ne.jp/keyword/%A5%B3%A5%F3%A5%D1%A5%A4%A5%EB) error because the implementation is not aware of compatibility with [Arduino](http://d.hatena.ne.jp/keyword/Arduino) 's [Ethernet .h.](http://d.hatena.ne.jp/keyword/Ethernet)

Here, following Mr. Juraj Andrássy's intention, instead of modifying the library on the [Arduino side, we will modify the library on the Arduino](http://d.hatena.ne.jp/keyword/Arduino) core for the ESP32 side.

**4. Code**
After fixing the library, write the code below to the ESP32.

```cpp
#include <Ethernet.h> 
#include <EthernetUdp.h>
bytemac [] = { 0x70 , 0x69 , 0x69 , 0x2D , 0x30 , 0x31 };
unsigned  int localPort = 3001 ;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
char ReplyBuffer[] = "acknowledged" ;
Ethernet UDP Udp;
void setup() {
 Serial. begin( 115200 );
 Serial.println( "starting..." );
 
 Ethernet.init( 33 ); // CS pin 33 
 Ethernet.begin(mac); // use DHCP
 // Check for Ethernet hardware present 
 if (Ethernet.hardwareStatus() == EthernetNoHardware) {
   Serial.println( "Ethernet shield was not found. Sorry, can't run without hardware. :(" );
    while ( true ) {
     delay( 1 ); // do nothing, no point running without Ethernet hardware
   }
 }
 if (Ethernet.linkStatus() == LinkOFF) {
   Serial.println( "Ethernet cable is not connected." );
 }
 Serial. print( "IP: " );
 Serial.println(Ethernet.localIP());
 
 Udp. begin(localPort);
}
void loop() {
  // if there's data available, read a packet 
 int packetSize = Udp.parsePacket();
  if (packetSize) {
   Serial.print( "Received packet of size " );
   Serial.println(packetSize);
   Serial. print( "From " );
   IPAddress remote = Udp.remoteIP();
   for ( int i= 0 ; i < 4 ; i++) {
     Serial.print(remote[i], DEC);
     if (i < 3 ) {
       Serial. print( "." );
     }
   }
   Serial. print( ", port " );
   Serial.println(Udp.remotePort());
   // read the packet into packetBuffer
   Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
   Serial. print( "Contents: " );
   Serial.println(packetBuffer);
   // send a reply to the IP address and port that sent us the packet we received
   Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
   Udp.write(ReplyBuffer);
   Udp.endPacket();
 }
 delay( 10 );
}
```

[Change the MAC address](http://d.hatena.ne.jp/keyword/MAC%A5%A2%A5%C9%A5%EC%A5%B9) according to your environment. [DHCP](http://d.hatena.ne.jp/keyword/DHCP) is used to set the [IP address](http://d.hatena.ne.jp/keyword/IP%A5%A2%A5%C9%A5%EC%A5%B9) , but you can also set a fixed IP by specifying the [IP address](http://d.hatena.ne.jp/keyword/IP%A5%A2%A5%C9%A5%EC%A5%B9) or [gateway as an argument to](http://d.hatena.ne.jp/keyword/gateway)[Ethernet](http://d.hatena.ne.jp/keyword/Ethernet) .begin() .

Default values ​​are set for [dns](http://d.hatena.ne.jp/keyword/dns) , [gateway](http://d.hatena.ne.jp/keyword/gateway) , and subnet when setting with a fixed IP, so it may work only with the [MAC address](http://d.hatena.ne.jp/keyword/MAC%A5%A2%A5%C9%A5%EC%A5%B9) and [IP address](http://d.hatena.ne.jp/keyword/IP%A5%A2%A5%C9%A5%EC%A5%B9) , but if you do not specify it, it will not work or communication will be unstable. Therefore, it is better to specify everything even if it is troublesome. [You can check the details of Ethernet](http://d.hatena.ne.jp/keyword/Ethernet) .begin() from the official reference below.

[Arduino - EthernetBegin](https://www.arduino.cc/en/Reference/EthernetBegin)

**5. Run**
Check the logs in the serial monitor. Communication partners can be easily prepared using [Node.js.](https://hikoleaf.hatenablog.jp/entry/2019/06/08/235753)If it works properly, you will see the received message in the log and the character string set in ReplyBuffer to the other party.

![f:id:hik0leaf:20190619233945j:plain](https://cdn-ak.f.st-hatena.com/images/fotolife/h/hik0leaf/20190619/20190619233945.jpg)

*Operation log of the serial monitor (Example: Receiving count-up data)*

---

Source: https://maker.wiznet.io/scott/projects/w5500%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6esp32%E3%81%A7ethernet-udp%E9%80%9A%E4%BF%A1%E3%81%99%E3%82%8B/
