#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>

#define SCREEN_WIDTH 128  // Define the screen width to be 128 pixels
#define SCREEN_HEIGHT 64  // Define the screen height to be 64 pixels
#define OLED_RESET -1     // Define the OLED reset pin to -1 (or -1 if the reset function is not used)
// Create an Adafruit_SSD1306 object that controls the OLED screen, specifying the screen width, height, I2C bus object, and reset pin
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define DHTPIN 19           // The DHT11 sensor is connected to digital pin 4 of the Arduino
#define DHTTYPE DHT11      // The DHT11 sensor was used
DHT dht(DHTPIN, DHTTYPE);  // Create DHT objects for connecting DHT sensors, specifying sensor pins and sensor models

void setup() {
  Serial.begin(9600);
  Wire.begin(5,4);  // The initial I2C bus SDA pin is set to 5 and the SCL pin is set to 4

  // Initializing the screen
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 initialization failed"));
    for (;;)
      ;
  }
  display.clearDisplay();  // Clear the screen buffer
  dht.begin();             // Initialize the DHT11 sensor
}

void loop() {
  // Read the temperature and humidity data
  int humidity = dht.readHumidity();
  int temperature = dht.readTemperature();
  display.clearDisplay(); // Clear the screen buffer
  // Display temperature and humidity data
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(16, 20);
  display.print("Temperature: ");
  display.println(temperature);
  display.setCursor(16, 40);
  display.print("Humidity: ");
  display.print(humidity);
  display.println("%");
  display.display();
  delay(2000);
}
