---
title: "ESP32 & W5500-Based Asynchronous MQTT Temperature & Humidity Monitoring System"
url: "https://maker.wiznet.io/josephsr/projects/esp32-w5500-based-asynchronous-mqtt-temperature-humidity-monitoring-system/"
markdown_url: "https://maker.wiznet.io/josephsr/projects/esp32-w5500-based-asynchronous-mqtt-temperature-humidity-monitoring-system/md"
type: "UCC: User Created Content"
author: "alex9ufo"
author_url: "https://alex9ufoexploer.blogspot.com/2025/01/esp32-asyncmqttesp32-mqtt-dht22.html"
editor: "WIZnet"
editor_url: "https://maker.wiznet.io/"
original_author: "alex9ufo"
original_url: "https://alex9ufoexploer.blogspot.com/2025/01/esp32-asyncmqttesp32-mqtt-dht22.html"
published: "2025-03-19"
language: "en"
hardware: ["Espressif ESP32S", "WIZnet W5500"]
likes: 0
views: 1609
comments: 0
source: "WIZnet Makers (https://maker.wiznet.io/)"
---

# ESP32 & W5500-Based Asynchronous MQTT Temperature & Humidity Monitoring System

> This project uses ESP32 with a W5500 to collect temperature and humidity data from a DHT22 sensor and send it to an MQTT broker using async communication.

Original author: alex9ufo (source: https://alex9ufoexploer.blogspot.com/2025/01/esp32-asyncmqttesp32-mqtt-dht22.html)

## Components

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

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. Project Overview & FeaturesCore Functionality:**

- This project reads temperature and humidity data from a **DHT22 sensor** and transmits it to an **MQTT broker** via **W5500 Ethernet or Wi-Fi**.

- The system supports **asynchronous MQTT communication** using the **AsyncMQTT_ESP32** library, ensuring efficient and non-blocking message handling.

**Problem Solved & Use Case:**

- Allows **real-time remote monitoring** of environmental conditions.

- Ideal for **IoT applications**, such as **smart home automation and industrial monitoring**.

**Key Features:**

- **Hardware:** ESP32, W5500 Ethernet module, DHT22 sensor

- **Network:** Supports both **Ethernet (W5500) and Wi-Fi connectivity**

- **Protocol:** MQTT

- **Data Handling:** JSON-based message formatting for structured transmission

---

### **2. Hardware & Software StackHardware Components:**

- **Microcontroller:** ESP32

- **Network Module:** W5500 Ethernet (SPI Interface)

- **Sensor:** DHT22 Temperature & Humidity Sensor

**Software Components:**

- **Programming Language:** C++ (Arduino IDE)

- **Libraries Used:**
  - **AsyncMQTT_ESP32**: Non-blocking MQTT communication
  - **Adafruit Sensor & DHT_U**: Sensor data collection
  - **ArduinoJson**: JSON data formatting

- **Network Protocols:** MQTT, TCP/IP

---

### **3. Key Parts of the Source Code**

#### **Ethernet Connection Setup**

```c
void setup() {
  Serial.begin(115200);
  // Initialize Ethernet
  Ethernet.init(ETH_CS_PIN);
  Ethernet.begin(mac, ip);
  // Setup MQTT broker connection
  mqttClient.setServer(MQTT_HOST, MQTT_PORT);
  mqttClient.setCallback(mqttCallback);
}
```

MQTT Broker Connection

```c
void connectToMqtt() {
  while (!mqttClient.connected()) {
    Serial.print("Connecting to MQTT...");
    if (mqttClient.connect("ESP32Client")) {
      Serial.println("Connected");
      // Subscribe to a topic
      mqttClient.subscribe("sensor/data");
    } else {
      Serial.print("Failed, rc=");
      Serial.print(mqttClient.state());
      Serial.println(" Retrying in 5 seconds...");
      delay(5000);
    }
  }
}
```

Reading Sensor Data & Sending MQTT Messages

```c
void readSensorData() {
  sensors_event_t event;
  dht.temperature().getEvent(&event);
  float temperature = event.temperature;
  dht.humidity().getEvent(&event);
  float humidity = event.relative_humidity;
  
  // Format data as JSON
  StaticJsonDocument<256> doc;
  doc["temperature"] = temperature;
  doc["humidity"] = humidity;
  
  char jsonBuffer[512];
  serializeJson(doc, jsonBuffer);
  
  // Publish data to MQTT topic
  mqttClient.publish("sensor/data", jsonBuffer);
}
```

### **3. Comparison with Traditional MQTT Implementations**

#### ✅ **Advantages:Asynchronous Processing:**

- Uses **AsyncMQTT_ESP32** for non-blocking communication.

- Improves system responsiveness and efficiency.

**Better Scalability:**

- Compatible with various boards, including ESP32 and **multiple Ethernet modules (W5500, ENC28J60, etc.)**.

#### ❌ **Disadvantages:Increased Complexity:**

- Asynchronous programming requires a deeper understanding compared to traditional synchronous implementations.

**Library Dependency:**

- The project heavily relies on **AsyncMQTT_ESP32**, which may require maintenance and updates to remain compatible.

---

### **4. Suggested Improvements & Enhancements**

✔ **Security:** Implement **TLS/SSL encryption** for MQTT to enhance data security.
✔ **Error Handling:** Add mechanisms to detect and handle **network failures, sensor errors, and MQTT disconnections**.
✔ **Data Storage:** Enable **local storage (SD card) or cloud-based database integration** for long-term data logging.
✔ **Real-time Monitoring:** Integrate with **Node-RED** or **other visualization tools** to display real-time sensor data.

---

Source: https://maker.wiznet.io/josephsr/projects/esp32-w5500-based-asynchronous-mqtt-temperature-humidity-monitoring-system/
