// Sistem Monitoring Suhu Kandang Ayam
// Soal No.21
// Nama   : Alfian Nur Rahman
// Kelas  : IOT3-3

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>;
#define DHTPIN 7     
#define DHTTYPE DHT22
#define ledBiru 10
#define ledKuning 9
#define ledMerah 8
#define Buzzer 11

Adafruit_SSD1306 display(128, 64, &Wire, -1);
DHT dht(DHTPIN, DHTTYPE); 
  
float temp; 
void setup() 
{
  Serial.begin(9600);
  dht.begin();
  pinMode(ledBiru, OUTPUT);
  pinMode(ledKuning, OUTPUT);
  pinMode(ledMerah, OUTPUT);
  pinMode(Buzzer, OUTPUT);

	digitalWrite(ledBiru, LOW);
  digitalWrite(ledKuning, LOW);
  digitalWrite(ledMerah, LOW);
  digitalWrite(Buzzer, LOW);

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); 
  display.clearDisplay();
  display.display();
  delay(5000);
}

void loop() {
  display.clearDisplay();
  display.setFont();
  display.setTextColor(1);
  temp= dht.readTemperature();
    
  display.setCursor(0,0);
  display.println("Temp: ");
  display.setCursor(0,10);
  display.print(temp);
  display.setCursor(40,10);
  display.println(" Celsius");
  //display.display();
  delay(1000);

    Serial.print("Temp: ");
    Serial.print(temp);
    Serial.println(" Celsius");
    delay(1000); 
  if (temp < 29){
    digitalWrite(ledBiru, HIGH);
    digitalWrite(ledKuning, LOW);
    digitalWrite(ledMerah, LOW);
    digitalWrite(Buzzer, LOW);
    noTone(Buzzer);
  //display.clearDisplay();
  //display.setFont();
  //display.setTextColor(1);

  //display.setFont(100);
  display.setCursor(0, 40);
  display.print("SUHU RENDAH");
  }
  if (temp >=29 && temp <= 35){
    digitalWrite(ledBiru, LOW);
    digitalWrite(ledKuning, HIGH);
    digitalWrite(ledMerah, LOW);
    digitalWrite(Buzzer, LOW);
    noTone(Buzzer);
  display.setCursor(0, 40);
  display.print("SUHU CUKUP");  
  }
  else if (temp > 35){
    digitalWrite(ledBiru, LOW);
    digitalWrite(ledKuning, LOW);
    digitalWrite(ledMerah, HIGH);
    digitalWrite(Buzzer, HIGH);
    tone(Buzzer, 1000);
  display.setCursor(0, 40);
  display.print("SUHU PANAS");
  }
  display.display(); 
}