IoT-based Indoor Environment Monitoring With ESP32 and DHT22 (Adafruit IO)

by sarful in Circuits > Arduino

26 Views, 0 Favorites, 0 Comments

IoT-based Indoor Environment Monitoring With ESP32 and DHT22 (Adafruit IO)

1 DHT22 - Digital Temperature and Humidity Sensors.png

In this project, you will build an indoor environment monitoring system using an ESP32 microcontroller and a DHT22 sensor. The system will measure temperature and humidity in real-time and send the data to Adafruit IO, a cloud platform designed for IoT applications. This project can be used to monitor indoor environments like homes, offices, or storage rooms for better environmental control. The data will be visualized on Adafruit IO’s dashboard, allowing users to analyze the conditions of their indoor environment.

Supplies

Components Required:

  1. ESP32 – Microcontroller with Wi-Fi capabilities.
  2. DHT22 Sensor – Temperature and humidity sensor (DHT22 is more accurate than DHT11).
  3. Jumper Wires – For wiring the components.
  4. Breadboard – For prototyping.
  5. 4.7kΩ Resistor – Pull-up resistor for the DHT22 data line.
  6. Power Supply – To power the ESP32.
  7. Adafruit IO Account – Cloud platform to visualize and store sensor data.


Wiring the Components:

  1. DHT22 to ESP32:
  2. VCC of DHT22 → 3.3V pin on ESP32.
  3. GND of DHT22 → GND pin on ESP32.
  4. Data Pin of DHT22 → Pin D4 on ESP32.
  5. 4.7kΩ Resistor: Place a resistor between VCC and Data Pin of the DHT22.

Setting Up Adafruit IO:

Step 1: Create an Adafruit IO Account

  1. Go to Adafruit IO and sign up for a free account.
  2. After logging in, you'll be directed to the Adafruit IO Dashboard.

Step 2: Create a New Feed

  1. In the Adafruit IO dashboard, click on the Feeds tab.
  2. Click + New Feed and create two feeds:
  3. Temperature Feed (for storing temperature data)
  4. Humidity Feed (for storing humidity data)
  5. These feeds will allow the ESP32 to send temperature and humidity data to Adafruit IO.

Step 3: Create an Adafruit IO Key

  1. Go to AIO Key in your account settings.
  2. Copy the AIO Key. This key is required for your ESP32 to communicate with Adafruit IO.

Setting Up Arduino IDE:

  1. Install ESP32 Board Support:
  2. Open Arduino IDE and go to File → Preferences.
  3. Add the following URL in Additional Boards Manager URLs:

https://dl.espressif.com/dl/package_esp32_index.json
  1. Then go to Tools → Board → Board Manager, search for ESP32, and install it.
  2. Install Necessary Libraries:
  3. Go to Sketch → Include Library → Manage Libraries.
  4. Install the following libraries:
  5. DHT sensor library (by Adafruit)
  6. Adafruit IO Arduino (for sending data to Adafruit IO)

Complete Arduino Code:

#include <WiFi.h>
#include <DHT.h>
#include <AdafruitIO.h>
#include <AdafruitIO_WiFi.h>

// WiFi credentials
const char* ssid = "your-SSID";
const char* password = "your-password";

// Adafruit IO credentials
#define IO_USERNAME "your-IO-username"
#define IO_KEY "your-AIO-key"

// DHT22 Sensor settings
#define DHTPIN 4 // Pin connected to the DHT22 sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302)

// Create DHT object
DHT dht(DHTPIN, DHTTYPE);

// Create Adafruit IO instance
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, ssid, password);

// Create Adafruit IO feeds
AdafruitIO_Feed *temperatureFeed = io.feed("temperature");
AdafruitIO_Feed *humidityFeed = io.feed("humidity");

void setup() {
// Start Serial Monitor
Serial.begin(115200);

// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");

// Connect to Adafruit IO
io.connect();
while (io.status() < AIO_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to Adafruit IO");

// Initialize DHT sensor
dht.begin();
}

void loop() {
// Read temperature and humidity from DHT22 sensor
float temperature = dht.readTemperature(); // Temperature in Celsius
float humidity = dht.readHumidity(); // Humidity in percentage

// Check if readings are valid
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}

// Print the temperature and humidity to the Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C ");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");

// Send data to Adafruit IO
temperatureFeed->save(temperature); // Send temperature data
humidityFeed->save(humidity); // Send humidity data

// Wait for 10 seconds before sending new data
delay(10000);
}

Code Explanation:

  1. Wi-Fi Setup:
  2. The code connects the ESP32 to your local Wi-Fi network using the WiFi.begin() function. The program waits until it successfully connects to Wi-Fi.
  3. Adafruit IO Setup:
  4. The Adafruit IO credentials (username and key) are used to authenticate the ESP32 with the Adafruit IO cloud platform.
  5. The program creates two feeds on Adafruit IO to store temperature and humidity data.
  6. DHT22 Sensor Setup:
  7. The DHT22 sensor is initialized on Pin D4 of the ESP32.
  8. The program continuously reads temperature and humidity using dht.readTemperature() and dht.readHumidity().
  9. Data Sending to Adafruit IO:
  10. The temperature and humidity readings are sent to their respective feeds on Adafruit IO using temperatureFeed->save() and humidityFeed->save().
  11. Looping:
  12. The readings are taken every 10 seconds, and the data is updated on the Adafruit IO dashboard.

Testing the Project:

  1. Upload the Code:
  2. Connect your ESP32 to your computer and upload the code using the Arduino IDE.
  3. Monitor the Serial Output:
  4. Open the Serial Monitor to view the temperature and humidity readings from the sensor.
  5. Check Adafruit IO Dashboard:
  6. Go to your Adafruit IO account and check the Dashboard. You should see live updates of your temperature and humidity readings.

Conclusion:

This ESP32-based Indoor Environment Monitoring system allows you to monitor temperature and humidity data in real-time and store it on the Adafruit IO platform for analysis. You can view the data on your personal dashboard, track historical trends, or integrate this project with automation systems for controlling fans, air conditioners, or dehumidifiers based on the environmental conditions.

Affiliate Disclaimer:

Some links in this article are affiliate links, meaning that I may earn a small commission if you make a purchase through them, at no additional cost to you. Thank you for supporting my work!