#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "DHT.h"
#include <Wire.h>
 
#define TFT_SCK    18
#define TFT_MOSI   23
#define TFT_MISO   19
#define TFT_CS     22
#define TFT_DC     21
#define TFT_RESET  17

#define DHTPIN 25  
#define DHTTYPE DHT22
 
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
DHT dht(DHTPIN, DHTTYPE);

const int r =4;
const int g = 0;
const int b= 2;


void setup() {

  Serial.begin(115200);

  pinMode(r,OUTPUT);
  pinMode(g,OUTPUT);
  pinMode(b,OUTPUT);

  Serial.println(F("DHTxx test!"));
  dht.begin();
  tft.begin();
 
}

void loop() { 

  float t = dht.readTemperature();
  float f = dht.readTemperature(true);

  tft.setRotation(1);
  tft.fillScreen(0xFFFF);
  tft.setCursor(18, 10);
  tft.setTextColor(ILI9341_BLACK);
  tft.setTextSize(2);
  tft.println("Sri Sairam Eng. Clg");

  tft.setCursor(45, 42);
  tft.setTextColor(ILI9341_BLUE);
  tft.setTextSize(2);
  tft.println("Tempature Sensor");

  if(t>=30)
  {
    tft.setCursor(24, 82);
    tft.setTextColor(ILI9341_RED);
    tft.print(t);
    tft.print(" ");
    tft.print(char(223));
    tft.print("C");
    
    tft.setCursor(184, 82);
    tft.setTextColor(ILI9341_RED);
    tft.print(f);
    tft.print(" ");
    tft.print(char(223));
    tft.print("F");

    tone(16, 25, 200);

    digitalWrite(r, HIGH);
    digitalWrite(g, LOW);
    digitalWrite(b, LOW);
  }
  else 
  {
    tft.setCursor(24, 82);
    tft.setTextColor(ILI9341_GREEN);
    tft.print(t);
    tft.print(" ");
    tft.print(char(223));
    tft.print("C");
    
    tft.setCursor(184, 82);
    tft.setTextColor(ILI9341_GREEN);
    tft.print(f);
    tft.print(" ");
    tft.print(char(223));
    tft.print("F");

    digitalWrite(r, LOW);
    digitalWrite(g, HIGH);
    digitalWrite(b, LOW);
  }
  delay(100); 

}