Mastering ESP32 for IoT Applications: Building a Weather Station

Integrating sensors and web connectivity with the ESP32 microcontroller

By Lester Knight Chaykin

Introduction

The ESP32 microcontroller is renowned for its robustness and versatility, making it an excellent choice for Internet of Things (IoT) applications. In this blog post, we’ll delve into building a comprehensive IoT-based weather station using the ESP32. We’ll cover sensor integration, data acquisition, and web connectivity to transmit sensor data to a cloud server.

Design

The IoT weather station will utilize the ESP32 to gather environmental data from multiple sensors:

  • DHT22 for temperature and humidity.
  • BMP280 for atmospheric pressure.

These sensors are chosen for their accuracy, ease of interfacing, and availability. The ESP32 will also connect to a WiFi network to send this data to a cloud platform (e.g., ThingSpeak) for storage and analysis.

Hardware Setup

  1. ESP32 Dev Board
  2. DHT22 Temperature and Humidity Sensor
  3. BMP280 Pressure Sensor
  4. Breadboard and Connecting Wires
  5. 3.3V Power Supply

Circuit Diagram

Here’s a simple wiring diagram for connecting the DHT22 and BMP280 to the ESP32:

[ESP32] -- [DHT22]
GPIO23 <--> Data

[ESP32] -- [BMP280]
GPIO21 <--> SDA (I2C Data)
GPIO22 <--> SCL (I2C Clock)

Ensure that all connections are secure and that the sensors are powered with 3.3V to match the ESP32 I/O levels.

Implementation

We’ll use the Arduino IDE to program the ESP32. First, include the necessary libraries and define the pins and WiFi credentials.

#include <WiFi.h>
#include <HTTPClient.h>
#include "DHT.h"
#include "Adafruit_BMP280.h"

// WiFi credentials
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";

// Sensor setup
#define DHTPIN 23
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
Adafruit_BMP280 bmp;

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  
  dht.begin();
  bmp.begin();
}

void loop() {
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();
  float pressure = bmp.readPressure() / 100.0;

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

  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.print(" °C, Humidity: ");
  Serial.print(humidity);
  Serial.print("%, Pressure: ");
  Serial.print(pressure);
  Serial.println(" hPa");

  sendToCloud(temperature, humidity, pressure);
  delay(2000); // Wait for 2 seconds before next read
}

void sendToCloud(float temp, float hum, float press) {
  if(WiFi.status() == WL_CONNECTED){
    HTTPClient http;
    String serverPath = "http://api.thingspeak.com/update?api_key=YOUR_API_KEY";
    serverPath += "&field1=" + String(temp) + "&field2=" + String(hum) + "&field3=" + String(press);

    http.begin(serverPath);
    int httpResponseCode = http.GET();
    
    if(httpResponseCode>0){
      String response = http.getString();
      Serial.println(response);
    }
    else{
      Serial.print("Error on sending POST: ");
      Serial.println(httpResponseCode);
    }
    http.end();
  }
}

Debugging

During the implementation, a common issue was intermittent sensor readings, especially from the DHT22. This was resolved by ensuring a stable power supply and adding pull-up resistors to the data line.

Results and Conclusion

The ESP32 weather station is now capable of monitoring environmental conditions and sending this data to the cloud for analysis. This project serves as a fundamental base for further expansion into automated home systems, agricultural monitoring, or any IoT application requiring robust sensor integration.

This project exemplifies how the ESP32 can be utilized in real-world IoT applications, combining sensor data acquisition, processing, and web connectivity in a seamless manner.

Share: