Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Articles

ESP32 Complete Project : Wi-Fi-connected weather station using the ESP32

The ESP32 is a powerful microcontroller with built-in Wi-Fi and Bluetooth capabilities, making it an excellent choice for IoT projects. Let's create a sample project where we build a Wi-Fi-connected weather station using the ESP32, which will display temperature and humidity readings from a DHT22 sensor and send the data to an online dashboard. (ESP 32)

 

Project Overview

Objective

  • Measure temperature and humidity using a DHT22 sensor.
  • Display the readings on an OLED display.
  • Send the data to an online IoT platform like ThingSpeak or Blynk for remote monitoring.

Components Required

  • ESP32 development board
  • DHT22 temperature and humidity sensor
  • 0.96" I2C OLED display
  • Breadboard and jumper wires
  • Micro USB cable

Software Required

  • Arduino IDE with ESP32 board support installed
  • Libraries for DHT22 and OLED display

Step-by-Step Guide

Step 1: Set Up the ESP32 with Arduino IDE

  1. Install the ESP32 Board in Arduino IDE:

    • Open Arduino IDE.
    • Go to File > Preferences.
    • Enter the following URL in the Additional Board Manager URLs field:

      https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json

        • Go to Tools > Board > Board Manager.
        • Search for "ESP32" and click Install.
      1. Select the ESP32 Board:

        • Go to Tools > Board and select your ESP32 model, such as ESP32 Dev Module.
      2. Connect the ESP32 to Your Computer:

        • Use a USB cable to connect the ESP32 to your computer.
        • Select the appropriate COM port under Tools > Port.

      Step 2: Circuit Diagram

      Here's a circuit diagram to connect the ESP32 with the DHT22 sensor and OLED display:

       

      Connections:

      • DHT22 Sensor:

        • VCC to 3.3V on ESP32
        • GND to GND on ESP32
        • DATA to GPIO 15 on ESP32
      • OLED Display (I2C):

        • VCC to 3.3V on ESP32
        • GND to GND on ESP32
        • SDA to GPIO 21 on ESP32
        • SCL to GPIO 22 on ESP32

      Step 3: Install Required Libraries

      1. DHT Sensor Library:

        • Go to Sketch > Include Library > Manage Libraries.
        • Search for "DHT sensor library" by Adafruit and install it.
      2. Adafruit Unified Sensor Library:

        • Search for "Adafruit Unified Sensor" and install it.
      3. OLED Display Library (Adafruit SSD1306 and GFX):

        • Search for "Adafruit SSD1306" and install it.
        • Search for "Adafruit GFX Library" and install it.

      Step 4: Arduino Code

      Here's the Arduino code for the project:

      #include #include #include #include #include #include #include #include #define SCREEN_WIDTH 128 // OLED display width, in pixels#define SCREEN_HEIGHT 64 // OLED display height, in pixels#define OLED_RESET    -1 // Reset pin # (or -1 if sharing Arduino reset pin)Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);#define DHTPIN 15     // Pin where the DHT22 is connected#define DHTTYPE DHT22 // DHT 22 (AM2302)// Initialize DHT sensor.DHT_Unified dht(DHTPIN, DHTTYPE);const char* ssid = "Your_SSID";const char* password = "Your_PASSWORD";// ThingSpeak Settingsconst char* server = "api.thingspeak.com";String apiKey = "Your_Thingspeak_API_Key";void setup() {  // Start Serial  Serial.begin(115200);    // Initialize the DHT sensor  dht.begin();  // Initialize the OLED display  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {     Serial.println(F("SSD1306 allocation failed"));    for(;<img src="https://computeraidedautomation.com/images/smiley/wink.svg" alt="Wink" />;  }  delay(2000);  display.clearDisplay();  // 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");}void loop() {  // Get temperature and humidity values  sensors_event_t event;  dht.temperature().getEvent(&event);  float temperature = event.temperature;  dht.humidity().getEvent(&event);  float humidity = event.relative_humidity;  // Display values on OLED  display.clearDisplay();  display.setTextSize(1);  display.setTextColor(SSD1306_WHITE);  display.setCursor(0, 0);  display.print("Temperature: ");  display.print(temperature);  display.println(" *C");  display.print("Humidity: ");  display.print(humidity);  display.println(" %");  display.display();  // Send data to ThingSpeak  if (WiFi.status() == WL_CONNECTED) {    HTTPClient http;    String url = String("http://") + server + "/update?api_key=" + apiKey + "&field1=" + String(temperature) + "&field2=" + String(humidity);    http.begin(url);    int httpCode = http.GET();    if (httpCode > 0) {      String payload = http.getString();      Serial.println(payload);    }    http.end();  } else {    Serial.println("WiFi not connected");  }    // Wait a few seconds between measurements  delay(10000);}



      Step 5: Configure ThingSpeak

      1. Create a ThingSpeak Account:

      2. Create a New Channel:

        • Create a new channel for your weather station.
        • Add fields for temperature and humidity.
      3. Obtain Your API Key:

        • Go to your channel's settings and get the Write API Key.
        • Replace
          Your_Thingspeak_API_Key
          in the code with your actual API key.

      Step 6: Upload the Code

      1. Open the Arduino IDE and paste the code.
      2. Connect the ESP32 to your computer.
      3. Select the ESP32 Board under Tools > Board.
      4. Select the Port under Tools > Port.
      5. Click the Upload Button to upload the code to the ESP32.

      Step 7: Monitor the Output

      1. Open the Serial Monitor in Arduino IDE to view the debug logs.
      2. Check the OLED Display for real-time temperature and humidity readings.
      3. Visit Your ThingSpeak Channel to see the data being plotted in real-time.

      Conclusion

      This project demonstrates how to build a simple IoT weather station using an ESP32. It reads temperature and humidity data from a DHT22 sensor and displays it on an OLED screen while sending the data to ThingSpeak for remote monitoring.

      You can expand this project by adding more sensors (such as air quality or light sensors), integrating with other IoT platforms, or implementing additional features like alerts and notifications.



caa July 23 2024 97 reads 0 comments Print

0 comments

Leave a Comment

Please Login to Post a Comment.
  • No Comments have been Posted.

Sign In
Not a member yet? Click here to register.
Forgot Password?
Users Online Now
Guests Online 7
Members Online 0

Total Members: 11
Newest Member: Jhilam