---
title: "ESP32 Ethernet W5500 MQTT"
url: "https://maker.wiznet.io/austin/projects/esp32-ethernet-w5500-mqtt/"
markdown_url: "https://maker.wiznet.io/austin/projects/esp32-ethernet-w5500-mqtt/md"
type: "UCC: User Created Content"
author: "Asyasyarif"
author_url: "https://github.com/Asyasyarif/ESP32-Ethernet-W5500-MQTT"
editor: "WIZnet"
editor_url: "https://maker.wiznet.io/"
original_author: "Asyasyarif"
original_url: "https://github.com/Asyasyarif/ESP32-Ethernet-W5500-MQTT"
published: "2023-09-04"
language: "en"
tags: ["Arduino", "W5500", "MQTT"]
hardware: ["WIZnet W5500"]
likes: 0
views: 11212
comments: 0
source: "WIZnet Makers (https://maker.wiznet.io/)"
---

# ESP32 Ethernet W5500 MQTT

> Arduino-based project that uses ESP32 and W5500 Ethernet modules to connect to an MQTT broker and post messages periodically.

Original author: Asyasyarif (source: https://github.com/Asyasyarif/ESP32-Ethernet-W5500-MQTT)

## Components

- **WIZnet W5500** x 1 ([docs](https://docs.wiznet.io/Product/Chip/Ethernet/W5500))
- 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/))

## Documents and links

- [ESP32-Ethernet-W5500-MQTT](https://github.com/Asyasyarif/ESP32-Ethernet-W5500-MQTT) (code)

## Article

#### Project Description

Arduino-based project that uses ESP32 and W5500 Ethernet modules to connect to an MQTT broker and post messages periodically.

1. Include libraries :
  - &lt;SPI.h> : Library for SPI communication.
  - &lt;Ethernet.h> : Library for Ethernet connectivity.
  - &lt;PubSubClient.h> : Library for MQTT communication.

2. Define constants and variables :
  - ETHERNET_MAC : MAC address of the Ethernet module.
  - ETHERNET_IP : IP address to be used when connected via Ethernet.
  - ETHERNET_RESET_PIN, ETHERNET_CS_PIN : Pin numbers for Ethernet module-related pins.
  - MQTT_HOSTNAME, MQTT_PORT : MQTT broker host and port.
  - MQTT_PUBLISH_INTERVAL_MS : Interval for periodically publishing messages via MQTT (1 second).

3. Ethernet and MQTT client setup :
  - ethernetWizReset() : Resets the Ethernet module.
  - macCharArrayToBytes() : Converts a MAC address from a string to a byte array.
  - connectEthernet() : Configures and initializes the Ethernet connection.
  - setupMqtt() : Sets up and initializes the MQTT client.
  - connectToMqtt() : Connects to the MQTT broker and sets Last Will and Testament (LWT) properties.

4. setup() :
  - Serial communication is started.
  - Ethernet connection is configured.
  - MQTT client is configured and connected.

5. loop() :
  - If the MQTT client is not connected, it attempts to reconnect and waits until the connection is successful.
  - Handles MQTT client's loop processing.
  - Publishes the device's uptime to the "mydevice/uptime" topic at intervals defined by MQTT_PUBLISH_INTERVAL_MS.

This example demonstrates how to establish communication with an MQTT broker using an ESP32 and Ethernet module. It periodically publishes the device's uptime to the "mydevice/uptime" topic, making it a simple example for IoT device communication.

---

Source: https://maker.wiznet.io/austin/projects/esp32-ethernet-w5500-mqtt/
