#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "DHTesp.h"

#define TINGGI_LAYAR 64 // Tinggi layar OLED yang digunakan
#define LEBAR_LAYAR 128 // Lebar layar OLED yang digunakan

Adafruit_SSD1306 display(LEBAR_LAYAR, TINGGI_LAYAR, &Wire, -1);
const int LED_PIN = 13;
const int DHT_PIN1 = 19;
const int DHT_PIN2 = 18;
const int DHT_PIN3 = 25;
const int DHT_PIN4 = 26;
const int DHT_PIN5 = 27;

DHTesp dhtSensor1;
DHTesp dhtSensor2;
DHTesp dhtSensor3;
DHTesp dhtSensor4;
DHTesp dhtSensor5;

void setup() {
  Serial.begin(115200);

  // Memastikan OLED dapat berjalan atau tidak
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;);
  }

  // Setup DHT sensor
  dhtSensor1.setup(DHT_PIN1, DHTesp::DHT22);
  dhtSensor2.setup(DHT_PIN2, DHTesp::DHT22);
  dhtSensor3.setup(DHT_PIN3, DHTesp::DHT22);
  dhtSensor4.setup(DHT_PIN4, DHTesp::DHT22);
  dhtSensor5.setup(DHT_PIN5, DHTesp::DHT22);

  display.clearDisplay(); // membersihkan tampilan
  display.setTextColor(WHITE); //warna OLED (putih)

  //Setup LED
  pinMode(LED_PIN, OUTPUT);
}

void loop() {
  // Read temperature and humidity from DHT sensor
  TempAndHumidity data1 = dhtSensor1.getTempAndHumidity();
  TempAndHumidity data2 = dhtSensor2.getTempAndHumidity();
  TempAndHumidity data3 = dhtSensor3.getTempAndHumidity();
  TempAndHumidity data4 = dhtSensor4.getTempAndHumidity();
  TempAndHumidity data5 = dhtSensor5.getTempAndHumidity();

  float avgtemp = (data1.temperature + data2.temperature + data3.temperature + data4.temperature + data5.temperature)/5;
  float avghum = (data1.humidity + data2.humidity + data3.humidity + data4.humidity + data5.humidity)/5;

  // Display temperature and humidity on OLED
  display.clearDisplay(); // Clear the previous display
  display.setTextSize(1);
  display.setCursor(0, 0);
  display.print("Temp: " + String(avgtemp, 2) + "C");
  display.setCursor(0, 20);
  display.print("Humidity: " + String(avghum, 1) + "%");
  display.display(); // Update the OLED display

  // Print temperature and humidity to Serial for debugging
  Serial.println("==========================================");
  Serial.println("Temperature/Humidity Sensor 1:");
  Serial.println(String(data1.temperature, 2) + "C / " + String(data1.humidity, 1) + "%");
  Serial.println("------------------------------------------");

  Serial.println("Temperature/Humidity Sensor 2:");
  Serial.println(String(data2.temperature, 2) + "C / " + String(data2.humidity, 1) + "%");
  Serial.println("------------------------------------------");

  Serial.println("Temperature/Humidity Sensor 3:");
  Serial.println(String(data3.temperature, 2) + "C / " + String(data3.humidity, 1) + "%");
  Serial.println("------------------------------------------");

  Serial.println("Temperature/Humidity Sensor 4:");
  Serial.println(String(data4.temperature, 2) + "C / " + String(data4.humidity, 1) + "%");
  Serial.println("------------------------------------------");

  Serial.println("Temperature/Humidity Sensor 5:");
  Serial.println(String(data5.temperature, 2) + "C / " + String(data5.humidity, 1) + "%");
  Serial.println("------------------------------------------");
  
  Serial.println("Rata- Rata Temperature/Humidity Sensor 5:");
  Serial.println(String(avgtemp, 2) + "C / " + String(avghum, 1) + "%");
  Serial.println("==========================================");

  digitalWrite(LED_PIN, HIGH);
  delay(2000); // Wait for a new reading from the sensor (DHT22 has ~0.5Hz sample rate)
  digitalWrite(LED_PIN, LOW);
}
$abcdeabcde151015202530fghijfghij