================================================================================
  Question 1: ESP32 DHT22 Temperature & Humidity Monitoring [30 Marks]
================================================================================

FULL QUESTION:
CAE 2 IoT Practical Exam - Section A (Answer either Q1 or Q2):
Q1. DHT22 Temperature and Humidity Monitoring [30 Marks]
Develop an ESP32-based temperature and humidity monitoring system using a DHT22 sensor.
The system should:
- Interface the DHT22 sensor with ESP32.
- Read the temperature and humidity values.
- Display the sensor readings through the Serial Monitor.
- Demonstrate the working of the system with suitable sensor readings.

SHORT QUESTION:
ESP32 DHT22 Temperature and Humidity Monitoring on Serial Monitor [30 Marks].

================================================================================
README & SETUP INSTRUCTIONS:
================================================================================
# Question 1: ESP32 DHT22 Temperature & Humidity Monitoring

## Problem Statement [30 Marks]
Develop an ESP32-based temperature and humidity monitoring system using a DHT22 sensor.
The system should:
1. Interface the DHT22 sensor with ESP32.
2. Read the temperature and humidity values.
3. Display the sensor readings through the Serial Monitor.
4. Demonstrate the working of the system with suitable sensor readings.

---

## Hardware Wiring
| DHT22 Sensor Pin | ESP32 Pin | Description |
| :--- | :--- | :--- |
| **VCC** | **3V3** | 3.3V Power Supply |
| **SDA / DATA** | **GPIO 18** | Digital Sensor Signal |
| **GND** | **GND** | Ground |

---

## How to Run in Wokwi Simulator
1. Navigate to [wokwi.com](https://wokwi.com) and create a new **ESP32** project.
2. Replace `sketch.ino` with the content of `sketch.ino` in this folder.
3. Replace `diagram.json` with `diagram.json` in this folder.
4. Add the libraries listed in `libraries.txt` (`DHT sensor library` and `Adafruit Unified Sensor`).
5. Click **Start Simulation (Play)**.
6. Open the **Serial Monitor** at **115200 baud**.
7. Click the DHT22 sensor icon and adjust the temperature and humidity sliders to observe real-time updates.

---

## Expected Output (Serial Monitor)
```text
==================================================
  ESP32 DHT22 Temperature & Humidity System
==================================================
DHT22 Sensor Initialized successfully.
Streaming readings every 2 seconds...

Temperature: 24.5 °C  |  Humidity: 50.0 %
Temperature: 28.2 °C  |  Humidity: 65.4 %
Temperature: 31.0 °C  |  Humidity: 72.1 %
```

================================================================================
SOURCE CODE FILES:
================================================================================

1. sketch.ino
--------------------------------------------------------------------------------
/*
================================================================================
CAE 2 – IOT PRACTICAL EXAMINATION
QUESTION 1: DHT22 Temperature and Humidity Monitoring [30 Marks]
================================================================================
Develop an ESP32-based temperature and humidity monitoring system using a
DHT22 sensor.
The system should:
1. Interface the DHT22 sensor with ESP32.
2. Read the temperature and humidity values.
3. Display the sensor readings through the Serial Monitor.
4. Demonstrate the working of the system with suitable sensor readings.
================================================================================
*/

// Short Description: ESP32 reads real-time ambient temperature and humidity from a DHT22 sensor and streams formatted readings to the Serial Monitor at 115200 baud.

/*
README / BEFORE RUNNING:
- Prerequisites: Wokwi Simulator or physical ESP32 board with Arduino IDE.
- Libraries Required: Install "DHT sensor library" and "Adafruit Unified Sensor" in Arduino IDE / libraries.txt.
- Hardware Wiring (ESP32 DevKit):
    * DHT22 VCC  -> ESP32 3V3 (Power)
    * DHT22 SDA  -> ESP32 GPIO 18 (Data Pin)
    * DHT22 GND  -> ESP32 GND (Ground)
- How to Run:
    1. In Wokwi: Load sketch.ino, diagram.json, and libraries.txt, then click 'Start Simulation'.
    2. Physical Hardware: Select 'DOIT ESP32 DEVKIT V1' in Arduino IDE, select COM port, and upload.
    3. Open Serial Monitor set to 115200 baud.
- Expected Behavior:
    * Prints "DHT22 Monitoring Initialized..." on startup.
    * Every 2 seconds, streams: "Temperature: XX.X °C  |  Humidity: YY.Y %".
    * In Wokwi, click the DHT22 sensor and adjust the temperature/humidity sliders to simulate live variations.
*/

#include <DHT.h>

#define DHT_PIN 18
#define DHT_TYPE DHT22

DHT dht(DHT_PIN, DHT_TYPE);

void setup() {
  Serial.begin(115200);
  delay(1000);
  Serial.println("==================================================");
  Serial.println("  ESP32 DHT22 Temperature & Humidity System");
  Serial.println("==================================================");
  
  dht.begin();
  Serial.println("DHT22 Sensor Initialized successfully.");
  Serial.println("Streaming readings every 2 seconds...\n");
}

void loop() {
  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();

  // Validate sensor readings
  if (isnan(temperature) || isnan(humidity)) {
    Serial.println("[ERROR] Failed to read from DHT22 sensor! Check wiring.");
    delay(2000);
    return;
  }

  // Display formatted readings to Serial Monitor
  Serial.print("Temperature: ");
  Serial.print(temperature, 1);
  Serial.print(" °C  |  Humidity: ");
  Serial.print(humidity, 1);
  Serial.println(" %");

  delay(2000);
}
--------------------------------------------------------------------------------

2. diagram.json
--------------------------------------------------------------------------------
{
  "version": 1,
  "author": "IoT Practical Exam",
  "editor": "wokwi",
  "parts": [
    { "type": "board-esp32-devkit-c-v4", "id": "esp", "top": 0, "left": 0, "attrs": {} },
    { "type": "wokwi-dht22", "id": "dht1", "top": 0.3, "left": -159, "attrs": {} }
  ],
  "connections": [
    [ "esp:TX", "$serialMonitor:RX", "", [] ],
    [ "esp:RX", "$serialMonitor:TX", "", [] ],
    [ "dht1:GND", "esp:GND.2", "black", [ "v0" ] ],
    [ "dht1:VCC", "esp:3V3", "red", [ "v0" ] ],
    [ "dht1:SDA", "esp:18", "green", [ "v0" ] ]
  ],
  "dependencies": {}
}
--------------------------------------------------------------------------------

3. libraries.txt
--------------------------------------------------------------------------------
DHT sensor library
Adafruit Unified Sensor
--------------------------------------------------------------------------------
