/*
  ESP32 HTTPClient Jokes API Example

  https://wokwi.com/projects/342032431249883731

  Copyright (C) 2022, Uri Shaked
*/

// #include <WiFi.h>
// #include <HTTPClient.h>
#include <ArduinoJson.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>

#include "DHT.h"

#define DHTPIN1 25
#define DHTPIN2 33
#define DHTPIN3 26
#define DHTPIN4 14
#define DHTPIN5 27
int r=21;
int g=17;
int b=22;
int buzz=16;

#define DHTTYPE1 DHT22
#define DHTTYPE2 DHT22
#define DHTTYPE3 DHT22
#define DHTTYPE4 DHT22
#define DHTTYPE5 DHT22

DHT dht1(DHTPIN1, DHTTYPE1);
DHT dht2(DHTPIN2, DHTTYPE2);
DHT dht3(DHTPIN3, DHTTYPE3);
DHT dht4(DHTPIN4, DHTTYPE4);
DHT dht5(DHTPIN5, DHTTYPE5);

// const char* ssid = "Wokwi-GUEST";
// const char* password = "";

//#define BTN_PIN 5
#define TFT_DC 2
#define TFT_CS 15
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);

void setup() {
  Serial.begin(115200);
  Serial.println(F("DHTxx test!"));
  pinMode(r, OUTPUT);
  pinMode(g, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(buzz, OUTPUT);

  dht1.begin();
  dht2.begin();
  dht3.begin();
  dht4.begin();
  dht5.begin();

  tft.begin();
  tft.setRotation(0);
}

void loop() {

    // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  //float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t1 = dht1.readTemperature();
  float t2 = dht2.readTemperature();
  float t3 = dht3.readTemperature();
  float t4 = dht4.readTemperature();
  float t5 = dht5.readTemperature();
  float average = t1+t2+t3+t4+t5/5.00;
  Serial.println("Average: ");
  Serial.print(average);
  Serial.println("");

  // Read temperature as Fahrenheit (isFahrenheit = true)
  //float f = dht.readTemperature(true);



  // Check if any reads failed and exit early (to try again).
  if (isnan(t1) || isnan(t2) || isnan(t3)|| isnan(t4)|| isnan(t5)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }
  else if((t1>=40)||(t2>=40)||(t3>=40)||(t4>=40)||(t5>=40)){
    tft.fillScreen(ILI9341_WHITE);
    tft.setTextColor(ILI9341_RED);
    tft.setTextSize(3);
    tft.setCursor(0,0);
    tft.println(" ");
    tft.print(" Fire Hazard");
    tft.println(" ");
    tft.println(" ");
    tft.println(" Temperature:");
    tft.println(" ");
    tft.print("  ");
    tft.print(t1);
    tft.print(char(232));
    tft.println("C");
    tft.print("  ");
    tft.print(t2);
    tft.print(char(232));
    tft.println("C");
    tft.print("  ");
    tft.print(t3);
    tft.print(char(232));
    tft.println("C");
    tft.print("  ");
    tft.print(t4);
    tft.print(char(232));
    tft.println("C");
    tft.print("  ");
    tft.print(t5);
    tft.print(char(232));
    tft.print("C");
    tone(buzz,250);
    digitalWrite(r, HIGH);
    digitalWrite(g, LOW);
    digitalWrite(b, LOW);
    delay(1000);
}
else if ((average>=25)&&(average<=40)){
    tft.fillScreen(ILI9341_WHITE);
    tft.setTextColor(ILI9341_DARKGREEN);
    tft.setTextSize(3);
    tft.setCursor(0,0);
    tft.println(" ");
    tft.print(" ");
    tft.println("  Amidious");
    tft.println(" ");
    tft.println(" Temperature:");
    tft.println(" ");
    tft.print("  ");
    tft.print(t1);
    tft.print(char(232));
    tft.println("C");
    tft.print("  ");
    tft.print(t2);
    tft.print(char(232));
    tft.println("C");
    tft.print("  ");
    tft.print(t3);
    tft.print(char(232));
    tft.println("C");
    tft.print("  ");
    tft.print(t4);
    tft.print(char(232));
    tft.println("C");
    tft.print("  ");
    tft.print(t5);
    tft.print(char(232));
    tft.print("C");
    tone(buzz,0);
    digitalWrite(r, LOW);
    digitalWrite(g, HIGH);
    digitalWrite(b, LOW);
    delay(1000);

}
else if(average<25){
    tft.fillScreen(ILI9341_WHITE);
    tft.setTextColor(ILI9341_RED);
    tft.setTextSize(3);
    tft.setCursor(0,0);
    tft.println(" ");
    tft.print(" ");
    tft.print("  Cold  ");
    tft.println(" ");
    tft.println(" ");
    tft.println(" Temperature:");
    tft.println(" ");
    tft.print("  ");
    tft.print(t1);
    tft.print(char(232));
    tft.println("C");
    tft.print("  ");
    tft.print(t2);
    tft.print(char(232));
    tft.println("C");
    tft.print("  ");
    tft.print(t3);
    tft.print(char(232));
    tft.println("C");
    tft.print("  ");
    tft.print(t4);
    tft.print(char(232));
    tft.println("C");
    tft.print("  ");
    tft.print(t5);
    tft.print(char(232));
    tft.print("C");
    tone(buzz,0);
    digitalWrite(r, LOW);
    digitalWrite(g, LOW);
    digitalWrite(b, HIGH);
    delay(1000);

}
else{
  tft.fillScreen(ILI9341_BLACK);
  tft.setTextColor(ILI9341_WHITE);
  tft.setTextSize(3);
  tft.setCursor(0,1);
  tft.println(" ");
  tft.println(" ");
  tft.println(" Fire_Alarm_");
  tft.println(" ");
  tft.println("   System");
  tone(buzz,0);
  digitalWrite(r, LOW);
  digitalWrite(g, LOW);
  digitalWrite(b, LOW);
  delay(1000);

}

  

  // Serial.print(F("Humidity: "));
  // Serial.print(h);
  Serial.println(F("Temperature: "));
  Serial.print(t1);
  Serial.print(", ");
  Serial.print(t2);
  Serial.print(", ");
  Serial.print(t3);
  Serial.print(", ");
  Serial.print(t4);
  Serial.print(", ");
  Serial.print(t5);
  Serial.println("");

  // Serial.print(F("°C "));
  // Serial.print(f);
  // Serial.print(F("°F  Heat index: "));
  // Serial.print(hic);
  // Serial.print(F("°C "));
  // Serial.print(hif);
  // Serial.println(F("°F"));

  delay(3000);
}