how-to-monitor-temperature-in-real-time-with-esp32-dht22-and-mqtt.png

How to Monitor Temperature in Real-Time with ESP32, DHT22, and MQTT

Introduction

This tutorial will guide you through setting up an ESP32 to monitor temperature in real-time using the DHT22 sensor and the Adafruit MQTT library. You’ll learn how to send temperature data over Wi-Fi using MQTT, a lightweight messaging protocol ideal for IoT applications. By the end, you will have a basic understanding of how to configure your ESP32 and DHT22 sensor to publish temperature readings to an MQTT broker.

Prerequisites

  • Basic knowledge of Arduino programming
  • ESP32 development board
  • DHT22 temperature and humidity sensor
  • MQTT broker (e.g., Mosquitto or a cloud-based broker like Adafruit IO)
  • Arduino IDE installed with ESP32 board support
  • Adafruit MQTT library installed in Arduino IDE

Parts/Tools

  • ESP32 Development Board
  • DHT22 Sensor
  • Breadboard and Jumper Wires
  • USB Cable for ESP32
  • Computer with Arduino IDE

Steps

  1. Set Up Hardware

    1. Connect the DHT22 sensor to the ESP32 as follows:
      • VCC to 3V3
      • GND to GND
      • Data Pin to GPIO 23
  2. Configure Arduino IDE

    1. Open Arduino IDE and install the necessary libraries:
    2. Sketch > Include Library > Manage Libraries...
    3. Search for and install the following libraries:
      • Adafruit Unified Sensor
      • DHT sensor library
      • Adafruit MQTT Library
  3. Write the Code

    1. Open a new sketch and include the necessary libraries:
    2. #include <WiFi.h>
      #include <DHT.h>
      #include <PubSubClient.h>
    3. Define the DHT sensor and Wi-Fi credentials:
    4. #define DHTPIN 23
      #define DHTTYPE DHT22
      const char* ssid = "YOUR_SSID";
      const char* password = "YOUR_PASSWORD";
    5. Set up the MQTT broker details:
    6. const char* mqtt_server = "MQTT_BROKER_ADDRESS";
    7. In the setup function, initialize Wi-Fi and MQTT client:
    8. void setup() {
          Serial.begin(115200);
          setup_wifi();
          client.setServer(mqtt_server, 1883);
          dht.begin();
      }
  4. Implement the Loop Function

    1. Read the temperature and publish the data:
    2. void loop() {
          if (!client.connected()) {
              reconnect();
          }
          client.loop();
          
          float h = dht.readHumidity();
          float t = dht.readTemperature();
          
          if (isnan(h) || isnan(t)) {
              Serial.println("Failed to read from DHT sensor!");
              return;
          }
          
          String temp = String(t);
          client.publish("home/temperature", temp.c_str());
          delay(2000);
      }
  5. Upload the Code

    1. Connect your ESP32 to the computer using a USB cable.
    2. Select the correct board and port in Arduino IDE:
    3. Tools > Board > ESP32 Dev Module
      Tools > Port > COMX
    4. Upload the code by clicking on the upload button (right arrow icon).
  6. Monitor MQTT Messages

    1. Use an MQTT client (like MQTT.fx or a web-based client) to subscribe to the topic:
    2. home/temperature
    3. You should see the temperature readings being published every 2 seconds.

Troubleshooting

  • If you don’t see any messages:
    • Check the MQTT broker address and ensure it is reachable.
    • Ensure the DHT22 sensor is connected properly.
    • Check the wiring and ensure no loose connections.
  • If the temperature readings are incorrect:
    • Make sure the DHT22 library is correctly installed.
    • Restart the ESP32 to reset the readings.

Conclusion

In this tutorial, you successfully configured an ESP32 to monitor temperature using a DHT22 sensor and transmit the data over MQTT. You’ve learned how to set up the hardware, write the code, and troubleshoot common issues. This setup can be expanded to include other sensors or functionalities, making it a solid foundation for IoT projects.

Leave a Comment

Your email address will not be published. Required fields are marked *