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
-
Set Up Hardware
- Connect the DHT22 sensor to the ESP32 as follows:
- VCC to 3V3
- GND to GND
- Data Pin to GPIO 23
- Connect the DHT22 sensor to the ESP32 as follows:
-
Configure Arduino IDE
- Open Arduino IDE and install the necessary libraries:
- Search for and install the following libraries:
- Adafruit Unified Sensor
- DHT sensor library
- Adafruit MQTT Library
Sketch > Include Library > Manage Libraries...
-
Write the Code
- Open a new sketch and include the necessary libraries:
- Define the DHT sensor and Wi-Fi credentials:
- Set up the MQTT broker details:
- In the setup function, initialize Wi-Fi and MQTT client:
#include <WiFi.h> #include <DHT.h> #include <PubSubClient.h>
#define DHTPIN 23 #define DHTTYPE DHT22 const char* ssid = "YOUR_SSID"; const char* password = "YOUR_PASSWORD";
const char* mqtt_server = "MQTT_BROKER_ADDRESS";
void setup() { Serial.begin(115200); setup_wifi(); client.setServer(mqtt_server, 1883); dht.begin(); }
-
Implement the Loop Function
- Read the temperature and publish the data:
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); }
-
Upload the Code
- Connect your ESP32 to the computer using a USB cable.
- Select the correct board and port in Arduino IDE:
- Upload the code by clicking on the upload button (right arrow icon).
Tools > Board > ESP32 Dev Module Tools > Port > COMX
-
Monitor MQTT Messages
- Use an MQTT client (like MQTT.fx or a web-based client) to subscribe to the topic:
- You should see the temperature readings being published every 2 seconds.
home/temperature
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.