---
title: "ESP32でEthernetライブラリのコンパイルエラーを回避する方法【Arduino使用】"
url: "https://maker.wiznet.io/louis_m/projects/esp32%E3%81%A7ethernet%E3%83%A9%E3%82%A4%E3%83%96%E3%83%A9%E3%83%AA%E3%81%AE%E3%82%B3%E3%83%B3%E3%83%91%E3%82%A4%E3%83%AB%E3%82%A8%E3%83%A9%E3%83%BC%E3%82%92%E5%9B%9E%E9%81%BF%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95%E3%80%90arduino%E4%BD%BF%E7%94%A8%E3%80%91/"
markdown_url: "https://maker.wiznet.io/louis_m/projects/esp32%E3%81%A7ethernet%E3%83%A9%E3%82%A4%E3%83%96%E3%83%A9%E3%83%AA%E3%81%AE%E3%82%B3%E3%83%B3%E3%83%91%E3%82%A4%E3%83%AB%E3%82%A8%E3%83%A9%E3%83%BC%E3%82%92%E5%9B%9E%E9%81%BF%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95%E3%80%90arduino%E4%BD%BF%E7%94%A8%E3%80%91/md"
type: "UCC: User Created Content"
author: "aloseed"
author_url: "https://aloseed.com/it/esp32-ethernet/"
editor: "WIZnet"
editor_url: "https://maker.wiznet.io/"
original_author: "aloseed"
original_url: "https://aloseed.com/it/esp32-ethernet/"
published: "2023-01-02"
language: "ja"
likes: 2
views: 456
comments: 1
source: "WIZnet Makers (https://maker.wiznet.io/)"
---

# ESP32でEthernetライブラリのコンパイルエラーを回避する方法【Arduino使用】

> ESP32でEthernetライブラリのコンパイルエラーを回避する方法【Arduino使用】

Original author: aloseed (source: https://aloseed.com/it/esp32-ethernet/)

## Article

こんにちは、あろっちです。

ESP32でEthernetライブラリのEthernetServerクラスを使ったスケッチでコンパイルエラーが発生する問題が確認されました。

これを回避するためのEthernetライブラリの修正方法を記述します。

目次

1. [Ethernetモジュールについて](https://aloseed.com/it/esp32-ethernet/#toc1)

2. [Ethernetライブラリ](https://aloseed.com/it/esp32-ethernet/#toc2)

3. [Ethernetライブラリの修正](https://aloseed.com/it/esp32-ethernet/#toc3)

4. [テスト](https://aloseed.com/it/esp32-ethernet/#toc4)

5. [関連記事](https://aloseed.com/it/esp32-ethernet/#toc5)

## **Ethernetモジュールについて**

ESP32をEthernetに繋ぐには以下のようなSPI接続の汎用Ethernetモジュールを用いるのがお手軽でシンプルかと思います。

ちなみに以下の製品のEthernet ICはいずれも**W5500**(WIZnet製)です。

こちらの制御プログラムを書くのにEhernetライブラリを使う感じです。

[![](https://aloseed.com/wp-content/uploads/2022/11/Ethernet-module-W5500-876x1024.jpeg)](https://aloseed.com/wp-content/uploads/2022/11/Ethernet-module-W5500.jpeg)

*左: W5500 Lite 右: W5500*

## **Ethernetライブラリ**

本記事ではEthernetライブラリは[**WIZnet Ethernet HAT**](https://www.wiznet.io/product-item/wiznet-ethernet-hat/)に対応した以下のWIZnet fork版を使用します。

<https://github.com/WIZnet-ArduinoEthernet/Ethernet>

WIZnet Ethernet HAT(**Raspberry Pi Pico用**)

[![](https://aloseed.com/wp-content/uploads/2022/12/wiznet-ethernet-hat-1.jpeg)](https://aloseed.com/wp-content/uploads/2022/12/wiznet-ethernet-hat-1.jpeg)

[![](https://aloseed.com/wp-content/uploads/2022/12/wiznet-ethernet-hat-2.jpeg)](https://aloseed.com/wp-content/uploads/2022/12/wiznet-ethernet-hat-2.jpeg)

[![](https://aloseed.com/wp-content/uploads/2022/12/pico-on-wiznet-ethernet-hat.jpeg)](https://aloseed.com/wp-content/uploads/2022/12/pico-on-wiznet-ethernet-hat.jpeg)

*Raspberry Pi Picoをセットしたところ*

参考サイト:

[![](https://aloseed.com/wp-content/uploads/cocoon-resources/blog-card-cache/9f8cdb7383f455369e5ffbaa3d5e7472.jpg)](http://tamanegi.digick.jp/computer-embedded/module/ethernethat/)

[**WIZnet Ethernet HAT(LAN通信モジュール)**](http://tamanegi.digick.jp/computer-embedded/module/ethernethat/)

[LAN通信するためのモジュールとしてWIZnetのEthernet HATを入手しました。 マイコンボードRaspberry Pi Pico用で合体して使用するモジュールです。 Raspberry Pi Picoを使用して通信した内容を記載します。](http://tamanegi.digick.jp/computer-embedded/module/ethernethat/)

[![](https://www.google.com/s2/favicons?domain=http://tamanegi.digick.jp/computer-embedded/module/ethernethat/)](http://tamanegi.digick.jp/computer-embedded/module/ethernethat/)

[tamanegi.digick.jp](http://tamanegi.digick.jp/computer-embedded/module/ethernethat/)

## **Ethernetライブラリの修正**

本記事の修正方法は他ボード(RP2040ボードなど)に影響が及ばないように配慮しています。
よって、この修正を入れても他ボードでもそのまま使えるかと思います。

| ライブラリのソースファイル場所 | Arduinoスケッチ保存フォルダ内のlibraries/Ethernet/src(※) |
| --- | --- |
| 修正ファイル | Ethernet.h EthernetServer.cpp |

*※ライブラリ名はEthernetですがGitHubからzipをダウンロードしてインストールした場合、フォルダ名がブランチ名がついた形(Ethernet-master等)になっていることも想定されます。*

**Ethernet.h**

EthernetServerの宣言の箇所

**修正前**

```cpp
class EthernetServer : public Server {
private:
	uint16_t _port;
public:
	EthernetServer(uint16_t port) : _port(port) { }
	EthernetClient available();
	EthernetClient accept();
	virtual void begin();
```

begin関数の箇所を以下のように修正します。

**修正後**

```cpp
class EthernetServer : public Server {
private:
	uint16_t _port;
public:
	EthernetServer(uint16_t port) : _port(port) { }
	EthernetClient available();
	EthernetClient accept();
#ifdef ARDUINO_ARCH_ESP32
	virtual void begin(uint16_t port=0);
#else
	virtual void begin();
#endif
```

**EthernetServer.cpp修正前**

```cpp
void EthernetServer::begin()
{
```

begin関数の箇所を以下のように修正します。

```cpp
#ifdef ARDUINO_ARCH_ESP32
void EthernetServer::begin(uint16_t port)
#else
void EthernetServer::begin()
#endif
{
```

## **テスト**

この修正を実施した後、Raspberry Pi Pico(RP2040)にEthernetServerを使ったサンプルスケッチを書いてESP32との対向通信を試してみました。

[![](https://aloseed.com/wp-content/uploads/2022/12/ethernet-communication.jpeg)](https://aloseed.com/wp-content/uploads/2022/12/ethernet-communication.jpeg)

*Raspberry Pi PicoがTCPSever、ESP32がTCPClientになっています*

Ethernet同士を直結するためクロスLANケーブルを使っています。

[![エレコム クロスLANケーブル 1ｍ ツメが折れない CAT5e ブルー LD-CTXT/BU10](https://m.media-amazon.com/images/I/31kCZTGKGIL._SL160_.jpg)](https://www.amazon.co.jp/dp/B00B42H20O?tag=aloseed-22&linkCode=ogi&th=1&psc=1)

[![](https://m.media-amazon.com/images/I/31kCZTGKGIL._SL500_.jpg)](https://www.amazon.co.jp/dp/B00B42H20O?tag=aloseed-22&linkCode=ogi&th=1&psc=1)

[![](https://m.media-amazon.com/images/I/41KmoCve3UL._SL75_.jpg)](https://www.amazon.co.jp/dp/B00B42H20O?tag=aloseed-22&linkCode=ogi&th=1&psc=1)

[![](https://m.media-amazon.com/images/I/41KmoCve3UL._SL500_.jpg)](https://www.amazon.co.jp/dp/B00B42H20O?tag=aloseed-22&linkCode=ogi&th=1&psc=1)

[エレコム クロスLANケーブル 1ｍ ツメが折れない CAT5e ブルー LD-CTXT/BU10](https://www.amazon.co.jp/dp/B00B42H20O?tag=aloseed-22&linkCode=ogi&th=1&psc=1)

エレコム

[**Amazon**](https://www.amazon.co.jp/gp/search?keywords=%E3%82%A8%E3%83%AC%E3%82%B3%E3%83%A0++LD-CTXT%2FBU+%E3%82%AF%E3%83%AD%E3%82%B9LAN%E3%82%B1%E3%83%BC%E3%83%96%E3%83%AB&tag=aloseed-22)

[**楽天**](https://hb.afl.rakuten.co.jp/hgc/1f51aabe.fd0f0469.1f51aabf.f1a98e37/?pc=https%3A%2F%2Fsearch.rakuten.co.jp%2Fsearch%2Fmall%2F%E3%82%A8%E3%83%AC%E3%82%B3%E3%83%A0++LD-CTXT%2FBU+%E3%82%AF%E3%83%AD%E3%82%B9LAN%E3%82%B1%E3%83%BC%E3%83%96%E3%83%AB%2F&m=https%3A%2F%2Fsearch.rakuten.co.jp%2Fsearch%2Fmall%2F%E3%82%A8%E3%83%AC%E3%82%B3%E3%83%A0++LD-CTXT%2FBU+%E3%82%AF%E3%83%AD%E3%82%B9LAN%E3%82%B1%E3%83%BC%E3%83%96%E3%83%AB%2F)

[**Yahoo!ショッピング![](https://ad.jp.ap.valuecommerce.com/servlet/gifbanner?sid=3580168&pid=887019722)**](https://ck.jp.ap.valuecommerce.com/servlet/referral?sid=3580168&pid=887019722&vc_url=http%3A%2F%2Fsearch.shopping.yahoo.co.jp%2Fsearch%3Fp%3D%E3%82%A8%E3%83%AC%E3%82%B3%E3%83%A0++LD-CTXT%2FBU+%E3%82%AF%E3%83%AD%E3%82%B9LAN%E3%82%B1%E3%83%BC%E3%83%96%E3%83%AB&vcpub=0.658124&vcid=cIfF-nTPHbbAyTWDavzhPvQNlRY258qCeLebXJcwiJJapkL1NWbfOw&isec=1672638697)

Raspberry Pi Picoのシリアルモニターを除いてみるとESP32からのパケットを受信できていることが確認できました。

[![](https://aloseed.com/wp-content/uploads/2022/12/pico-ethernet-serial-monitor.png)](https://aloseed.com/wp-content/uploads/2022/12/pico-ethernet-serial-monitor.png)

このように上記修正を入れてもRaspberry Pi Pico(RP2040ボード)でもEthernetServerクラスが使えることを確認できました。

---

Source: https://maker.wiznet.io/louis_m/projects/esp32%E3%81%A7ethernet%E3%83%A9%E3%82%A4%E3%83%96%E3%83%A9%E3%83%AA%E3%81%AE%E3%82%B3%E3%83%B3%E3%83%91%E3%82%A4%E3%83%AB%E3%82%A8%E3%83%A9%E3%83%BC%E3%82%92%E5%9B%9E%E9%81%BF%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95%E3%80%90arduino%E4%BD%BF%E7%94%A8%E3%80%91/
