//PROGRAM TPI PEMBACAAN DHT PADA OLED

//Masukan Library Komponen
#include <SPI.h>; //Library OLED
#include <Wire.h>; //Library OLED
#include <Adafruit_GFX.h>; //Library OLED
#include <Adafruit_SSD1306.h>; //Library OLED
#include "DHT.h"; //Library DHT

//Deklarasikan Variabel
#define SCREEN_WIDTH 128 //Deklarasi OLED
#define SCREEN_WIDTH 64 //Deklarasi OLED
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire, -1);
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);




//Mulai
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  dht.begin (); //Setup DHT22

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F(" Hello "));
    for(;;);
  
  }
  //Setup OLED Display
  delay(1000);
  display.clearDisplay();
  display.setTextColor(WHITE);
  //display.setTextSize(1);
  //display.setCursor(5, 10);
  //display.print("Rizky Ananda");
  //display.display();
}

void loop() {
  // put your main code here, to run repeatedly:
  
  //looping bacaan DHT
  float suhu = dht.readTemperature(); //Baca Suhu
  float RH = dht.readHumidity(); //Baca RH
  if (isnan(suhu) || isnan(RH)){

  }

  //Menampilkan Suhu dan RH di Serial Monitor
  //else {
    //Serial.print("Suhu: ");
    //Serial.print(suhu);
    //Serial.print(" C ");
    //Serial.print(" Kelembaban ");
    //Serial.print(RH);
    //Serial.println(" %"); 
    //delay(1000);
  display.clearDisplay();
  //Program menampilkan Suhu dan RH pada OLED Display
  //Tampilan Judul
  display.setTextSize(1); //Ukuran Text Judul
  display.setCursor(30, 0); //Letak Judul
  display.print("Data Suhu & RH");

  //Menampilkan Suhu
  display.setTextSize(1); //Ukuran Text Suhu
  display.setCursor(0,30); //Letak Suhu
  display.print("Suhu");
  display.print(suhu);
  display.print (" C");

  //Menampilkan RH
  display.setTextSize(1); //Ukuran Text RH
  display.setCursor(0,40); //Letak RH
  display.print("Kelembaban");
  display.print(RH);
  display.print (" %");

  display.display();
  
//Selesai
}