---
title: "W5500を使ってArduinoでMQTT通信する"
url: "https://maker.wiznet.io/scott/projects/w5500%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6arduino%E3%81%A7mqtt%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%A6arduino%E3%81%A7mqtt%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/16/112608"
editor: "WIZnet"
editor_url: "https://maker.wiznet.io/"
original_author: "Hiko"
original_url: "https://hikoleaf.hatenablog.jp/entry/2019/06/16/112608"
published: "2022-11-01"
language: "en"
tags: ["W5500", "MQTT"]
hardware: ["WIZnet W5500", "Arduino UNO"]
likes: 4
views: 7756
comments: 0
source: "WIZnet Makers (https://maker.wiznet.io/)"
---

# W5500を使ってArduinoでMQTT通信する

> Let's try MQTT communication with Arduino using WIZnet's W5500 .

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

## Components

- **WIZnet W5500** x 1 ([docs](https://docs.wiznet.io/Product/Chip/Ethernet/W5500))
- **Arduino UNO** x 1 ([docs](https://www.farnell.com/datasheets/1682209.pdf))
- Software: **Arduino IDE** ([docs](https://www.arduino.cc/en/software))

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. Prepare

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*

[Arduino](http://d.hatena.ne.jp/keyword/Arduino) uses UNO R3.

### 2. Connection

[To communicate with Arduino](http://d.hatena.ne.jp/keyword/Arduino) via SPI, connect the W5500 as follows. The IO level is 3.3V, but it is also 5V tolerant, so it can be directly connected to [Arduino .](http://d.hatena.ne.jp/keyword/Arduino)Another point to note is that the current consumption of the W5500 is large, so the 3.3V is supplied externally instead of from the [Arduino .](http://d.hatena.ne.jp/keyword/Arduino)

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

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

*Connection diagram*

| [Arduino](http://d.hatena.ne.jp/keyword/Arduino) | W5500 |
| --- | --- |
| D10 (SS) | CS |
| D11 (MOSI) | SMOKE |
| D12 (MISO) | MISO |
| D13 (SCK) | SCLK |
| 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*

***[Caution] When connecting with a breadboard, keep the wires as short as possible. If the wire is long, the communication will be unstable.***

### 3. Library installation

When using W5500 , unlike ENC28J60, you can control the module with the standard [Ethernet](https://hikoleaf.hatenablog.jp/entry/2019/06/15/220337)[library](http://d.hatena.ne.jp/keyword/Ethernet) , so install only PubSubClient that handles the MQTT part.

[github.com](https://github.com/knolleary/pubsubclient)

The library can be searched by name from the library manager of the [Arduino](http://d.hatena.ne.jp/keyword/Arduino)[IDE](http://d.hatena.ne.jp/keyword/IDE) and installed.

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

### 4. Code

Write the following code to [Arduino](http://d.hatena.ne.jp/keyword/Arduino) .

```
#include <Ethernet.h>
#include <PubSubClient.h>

byte mac[] = { 0x70, 0x69, 0x69, 0x2D, 0x30, 0x31 };
byte server[] = { 10, 0, 1, 5 };

void callback(char* topic, byte* payload, unsigned int length) {
  payload[length] = '\0';
  String msg = String((char*) payload);
  Serial.println(msg);
}


EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);


void reconnect() {
  while (!client.connected()) {
    Serial.println("Attempting MQTT connection...");
    
    if (client.connect("myClient")) {
      Serial.println("MQTT PubSub Ready");
      client.publish("output", "ready");
      client.subscribe("input");
      Serial.println("subscribe input");
    } else {
      Serial.println("MQTT PubSub failer");
      Serial.println("try again in 2 seconds");
      delay(2000);
    }
  }  
}


void setup() {
  Serial.begin(9600);
  Serial.println("starting...");
  
  Ethernet.init(10);// CS pin 10
  Ethernet.begin(mac);

  // 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());
  
  delay(1500);
}


void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
}
```

PubSubClient also has [API](http://d.hatena.ne.jp/keyword/API) documentation.

[Arduino Client for MQTT](https://pubsubclient.knolleary.net/api.html#PubSubClient)

[Change the MAC address](http://d.hatena.ne.jp/keyword/MAC%A5%A2%A5%C9%A5%EC%A5%B9) and server address according to your environment. The server address is the address of MQTT Broker, and MQTT Broker is required to check the operation. The easiest and recommended way to build MQTT Broker is to install [mosqitto](https://mosquitto.org/download/) on [Raspberry Pi .](http://d.hatena.ne.jp/keyword/Raspberry%20Pi)

### 5. Run

After confirming that the MQTT Broker is working, check the log on the serial monitor connected to the [Arduino .](http://d.hatena.ne.jp/keyword/Arduino)If everything works fine, you should see something like this:

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

When you install mosquitto, PubSub client is installed in addition to Broker, so let's check the operation by launching terminals for Publish (Pub) and Subscribe (Sub).

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

*Subscribe screen*

Subscribe to topic output. You can confirm that ready is sent when [Arduino starts up.](http://d.hatena.ne.jp/keyword/Arduino)If you miss it , reset the [Arduino](http://d.hatena.ne.jp/keyword/Arduino) and it will be sent again.

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

*Publish screen*

If you send a message (payload) to the input topic, you can check the reception on the [Arduino 's serial monitor.](http://d.hatena.ne.jp/keyword/Arduino)

### 6. Summary

In this blog, I have introduced that [TCP](http://d.hatena.ne.jp/keyword/TCP) and [UDP](http://d.hatena.ne.jp/keyword/UDP) communication is possible even with weak [microcomputers](http://d.hatena.ne.jp/keyword/%A5%DE%A5%A4%A5%B3%A5%F3) by using W5500 and ENC28J60 . Recently, with the advent of ESP8266 and ESP32 equipped with [Wi-Fi](http://d.hatena.ne.jp/keyword/Wi-Fi) , it has become possible to easily create IoT devices by themselves. I think there are situations where you want it.

[When communicating with a microcomputer](http://d.hatena.ne.jp/keyword/%A5%DE%A5%A4%A5%B3%A5%F3) , it would be nice to be able to select wired, wireless, and [protocols](http://d.hatena.ne.jp/keyword/%A5%D7%A5%ED%A5%C8%A5%B3%A5%EB) in consideration of cost, application, usage environment, etc.

---

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