// #include "DHTesp.h"

// const int DHT_PIN = 14;

// DHTesp dhtSensor;

// void setup() {
//   Serial.begin(115200);
//   dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
// }

// void loop() {
//   TempAndHumidity  data = dhtSensor.getTempAndHumidity();
//   Serial.println("Temp: " + String(data.temperature, 2) + "°C");
//   Serial.println("Humidity: " + String(data.humidity, 1) + "%");
//   Serial.println("---");
//   delay(2000); // Wait for a new reading from the sensor (DHT22 has ~0.5Hz sample rate)
// }
#include <OneWire.h>
#include <DallasTemperature.h>
#include "DHTesp.h"
#include <ESP32Servo.h>

// GPIO where the DS18B20 is connected to
const int oneWireBus = 4;
// GPIO where the DHT22 is connected to
const int DHT_PIN = 14;
// GPIO where the buzzer is connected to
const int BUZZER_PIN = 13;
// GPIO pins for the ultrasonic sensor
const int TRIG_PIN = 5;
const int ECHO_PIN = 18;
// GPIO pin for the servo motor
const int SERVO_PIN = 15;

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);
// Pass our oneWire reference to Dallas Temperature sensor 
DallasTemperature sensors(&oneWire);
// Create an instance of the DHTesp class
DHTesp dhtSensor;
// Create an instance of the Servo class
Servo myServo;

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

  // Start the DS18B20 sensor
  sensors.begin();

  // Start the DHT22 sensor
  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);

  // Initialize the buzzer pin as output
  pinMode(BUZZER_PIN, OUTPUT);
  digitalWrite(BUZZER_PIN, LOW); // Ensure the buzzer is off initially

  // Initialize the ultrasonic sensor pins
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);

  // Attach the servo motor to the servo pin
  myServo.attach(SERVO_PIN);
  myServo.write(0); // Initial position
}

void loop() {
  // Request temperature from DS18B20
  sensors.requestTemperatures(); 
  float temperatureC_DS18B20 = sensors.getTempCByIndex(0);
  float temperatureF_DS18B20 = sensors.getTempFByIndex(0);

  // Print DS18B20 temperature
  Serial.print("DS18B20 Temp: ");
  Serial.print(temperatureC_DS18B20);
  Serial.println("ºC");
  Serial.print("DS18B20 Temp: ");
  Serial.print(temperatureF_DS18B20);
  Serial.println("ºF");

  // Get temperature and humidity from DHT22
  TempAndHumidity data = dhtSensor.getTempAndHumidity();

  // Print DHT22 data
  Serial.print("DHT22 Temp: ");
  Serial.print(data.temperature, 2);
  Serial.println("°C");
  Serial.print("DHT22 Humidity: ");
  Serial.print(data.humidity, 1);
  Serial.println("%");
  Serial.println("---");

  // Check if the temperature from either sensor is above 32°C
  if (temperatureC_DS18B20 > 32.0 || data.temperature > 32.0) {
    digitalWrite(BUZZER_PIN, HIGH); // Turn on the buzzer
  } else {
    digitalWrite(BUZZER_PIN, LOW); // Turn off the buzzer
  }

  // Measure distance using the ultrasonic sensor
  long duration, distance;
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  
  duration = pulseIn(ECHO_PIN, HIGH);
  distance = duration * 0.034 / 2; // Calculate the distance in cm

  // Print the distance
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  Serial.println("---");

  // Rotate the servo motor if distance is less than or equal to 200 cm
  if (distance <= 200) {
    myServo.write(90); // Rotate to 90 degrees
  } else {
    myServo.write(0); // Rotate back to 0 degrees
  }

  // Delay between readings
  delay(5000);
}