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

//Define LED
#define biru 26
#define kuning 27
#define merah 14

//Define DHT
#define dht 15
DHTesp dhtSensor;

//Define OLED
#define LEBAR_LAYAR 128 // Lebar layar OLED yang digunakan
#define TINGGI_LAYAR 64 // Tinggi layar OLED yang digunakan
Adafruit_SSD1306 oled(LEBAR_LAYAR, TINGGI_LAYAR, &Wire, -1);

#define buzzer 2


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  //LED
  pinMode(biru, OUTPUT);
  pinMode(kuning, OUTPUT);
  pinMode(merah, OUTPUT);
  //DHT
  dhtSensor.setup(dht, DHTesp::DHT22);
  //Oled
  if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("failed to start SSD1306 OLED"));
    while (1);
    }
  pinMode(buzzer, OUTPUT);
}

void loop() {
  
  TempAndHumidity  data = dhtSensor.getTempAndHumidity();
  if(data.temperature < 29) {
    digitalWrite(biru, HIGH);
    digitalWrite(kuning, LOW);
    digitalWrite(merah, LOW);
    oled.clearDisplay ();
    oled.setTextSize(1);
    oled.setTextColor(WHITE);
    oled.setCursor(0, 10);
    oled.println("SUHU RENDAH");
    oled.display();
    delay(1000);
  }
  if(data.temperature > 29 && data.temperature <= 35) {
    digitalWrite(merah, LOW);
    digitalWrite(kuning, HIGH);
    digitalWrite(biru, LOW);
    oled.clearDisplay ();
    oled.setTextSize(1);
    oled.setTextColor(WHITE);
    oled.setCursor(0, 10);
    oled.println("SUHU CUKUP");
    oled.display();
    delay(1000);
  }
  if(data.temperature > 35 ) {
    digitalWrite(merah, HIGH);
    digitalWrite(kuning, LOW);
    digitalWrite(biru, LOW);
    digitalWrite(buzzer, HIGH);
    oled.clearDisplay ();
    oled.setTextSize(1);
    oled.setTextColor(WHITE);
    oled.setCursor(0, 10);
    oled.println("SUHU PANAS");
    oled.display();
    delay(1000);
  }
}