Tuesday, February 4, 2025
ElectronicsHow ToInternet of ThingsTutorials/DIY

Creating a Virtual Weather Station Using Wokwi and ESP32

Building a weather station is an exciting project for electronics enthusiasts, and with Wokwi, you can create and simulate a fully functional virtual weather station without needing physical hardware. This guide walks you through creating a virtual weather station using an ESP32 microcontroller on Wokwi. You’ll learn how to simulate temperature, humidity, and pressure readings and display the data on an OLED screen.

What You’ll Need

Components (Virtual in Wokwi):

  1. ESP32: The microcontroller to collect and process sensor data.
  2. DHT22 Sensor: Simulates temperature and humidity readings.
  3. BMP280 Sensor: Simulates barometric pressure and temperature readings.
  4. OLED Display (SSD1306): Displays the collected weather data.
  5. Connecting Wires: For virtual connections between components.

Software:

  1. Wokwi: The platform for circuit simulation.
  2. Arduino IDE (optional): For coding and debugging the ESP32.

Step 1: Setting Up Your Wokwi Project

1.1: Start a New Project

  1. Go to Wokwi’s website and log in.
  2. Click New Project and select ESP32 as the microcontroller.

1.2: Add Components

  1. Open the Parts Library on the left-hand side.
  2. Drag and drop the following components into the workspace:
    • ESP32
    • DHT22 sensor
    • BMP280 sensor
    • SSD1306 OLED display
  3. Arrange the components for easy visualization.

Step 2: Wiring the Components

2.1: Connect the DHT22 Sensor

  • VCC: Connect to the 3.3V pin of the ESP32.
  • GND: Connect to the GND pin of the ESP32.
  • DATA: Connect to GPIO 15 of the ESP32.

2.2: Connect the BMP280 Sensor

  • VCC: Connect to the 3.3V pin of the ESP32.
  • GND: Connect to the GND pin of the ESP32.
  • SCL: Connect to GPIO 22 (I2C Clock) of the ESP32.
  • SDA: Connect to GPIO 21 (I2C Data) of the ESP32.

2.3: Connect the OLED Display

  • VCC: Connect to the 3.3V pin of the ESP32.
  • GND: Connect to the GND pin of the ESP32.
  • SCL: Connect to GPIO 22 (I2C Clock) of the ESP32.
  • SDA: Connect to GPIO 21 (I2C Data) of the ESP32.

Step 3: Writing the Code

3.1: Open the Code Editor

  1. Click on the Code Editor in Wokwi.
  2. Write the following code to read sensor data and display it on the OLED screen:
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <Adafruit_BMP280.h>

// Define DHT22 pin and type
#define DHTPIN 15
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);
Adafruit_BMP280 bmp;

// Define OLED display dimensions
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

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

  // Initialize DHT22 sensor
  dht.begin();

  // Initialize BMP280 sensor
  if (!bmp.begin(0x76)) {
    Serial.println("BMP280 initialization failed!");
    while (1);
  }

  // Initialize OLED display
  if (!display.begin(SSD1306_I2C_ADDRESS, 0x3C)) {
    Serial.println("OLED initialization failed!");
    while (1);
  }

  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
}

void loop() {
  // Read DHT22 sensor data
  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();

  // Read BMP280 sensor data
  float pressure = bmp.readPressure() / 100.0; // Convert Pa to hPa

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

  // Display sensor data on Serial Monitor
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println(" °C");
  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.println(" %");
  Serial.print("Pressure: ");
  Serial.print(pressure);
  Serial.println(" hPa");

  // Display data on OLED
  display.clearDisplay();
  display.setCursor(0, 0);
  display.println("Weather Station");
  display.print("Temp: ");
  display.print(temperature);
  display.println(" C");
  display.print("Humidity: ");
  display.print(humidity);
  display.println(" %");
  display.print("Pressure: ");
  display.print(pressure);
  display.println(" hPa");
  display.display();

  delay(2000); // Wait 2 seconds before updating
}

3.2: Save and Run

  1. Save your code in Wokwi.
  2. Click the Play button to start the simulation.
  3. Observe the weather data on the OLED display.

Step 4: Testing and Debugging

  1. Simulation:
    • Verify that the OLED displays temperature, humidity, and pressure readings correctly.
    • Use the Serial Monitor to debug and check raw sensor values.
  2. Common Issues:
    • Ensure all components are correctly connected.
    • Double-check I2C addresses for the BMP280 and OLED display.
    • Ensure the libraries for DHT, BMP280, and SSD1306 are included.

Step 5: Experiment and Expand

Now that your virtual weather station is up and running, try enhancing it:

  1. Add More Sensors:
    • Include a light sensor (e.g., LDR) to measure ambient light.
    • Use a rain sensor to detect precipitation.
  2. Data Logging:
    • Simulate logging data to an SD card.
    • Integrate with an online platform like Thingspeak to visualize data.
  3. IoT Integration:
    • Send sensor data to the cloud using ESP32’s Wi-Fi capabilities.
    • Build a dashboard to monitor your weather station remotely.

Conclusion

Creating a virtual weather station using Wokwi and ESP32 is a fun and educational project that introduces you to microcontrollers, sensors, and data visualization. With the flexibility of Wokwi, you can prototype and refine your ideas without needing physical components. Start experimenting and take your virtual weather station to the next level!

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin

Harshvardhan Mishra has 753 posts and counting. See all posts by Harshvardhan Mishra

Leave a Reply

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