#include <DHT.h>
#include <WiFi.h>
#include "ThingSpeak.h"
#include <Wire.h>
#include <SPI.h>
#include <LiquidCrystal.h>

// Define pin for the DHT22 sensor
#define DHTPIN 2
// Define DHT sensor type
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

// Initialize the LCD with the pins used
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);

// WiFi credentials
const char* WIFI_NAME = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
// ThingSpeak channel and API information
const int myChannelNumber = 2525635;//your channel ID
const char* myApiKey = "0UIFGZI9G74FDKWW";//your channel write api key
const char* server = "api.thingspeak.com";


WiFiClient client;

void setup() {
  // Start serial communication for debugging
  Serial.begin(9600);
  
  // Initialize the DHT sensor
  dht.begin();
  
  // Initialize the LCD display
  lcd.begin(16, 2);
  delay(2000); // Wait for 2 seconds
  lcd.clear();

  // Connect to WiFi
  Serial.print("Connecting to WiFi");
  WiFi.begin(WIFI_NAME,"");
  while (WiFi.status() != WL_CONNECTED) {
    delay(100);
    Serial.print(".");
  }
  Serial.println(" Connected!");

  // Initialize ThingSpeak
  ThingSpeak.begin(client);
  
  // Set pin modes for other outputs
  pinMode(22, OUTPUT);
}

void loop() {
  // Read humidity and temperature from DHT sensor
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();
  
  // Update ThingSpeak fields
  ThingSpeak.setField(1, temperature);
  ThingSpeak.setField(2, humidity);
  int x = ThingSpeak.writeFields(myChannelNumber, myApiKey);
  
  // Display data on the LCD
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(F("HUMIDITY: "));
  lcd.print(humidity);
  lcd.print(F(" %"));
  lcd.setCursor(0, 1);
  lcd.print(F("TEMP: "));
  lcd.print(temperature);
  lcd.print(F(" C"));
  if (temperature < 40 && humidity < 60 ){
    digitalWrite(22, LOW);
  }
  // Check for high temperature condition
  if (temperature >= 40) {
    digitalWrite(22, HIGH); // Turn on the warning RBG 
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(F("HIGH TEMP!!!"));
    
    // Push warning data to ThingSpeak
    ThingSpeak.setField(2, temperature);
    int x = ThingSpeak.writeFields(myChannelNumber, myApiKey);
    if (x == 200) {
      Serial.println("High temp data pushed to ThingSpeak successfully");
    } else {
      Serial.println("Error pushing high temp data: " + String(x));
    }
    delay(1000); // Wait for 1 seconds before continuing
  }
  
  // Check for high humidity condition
  else if (humidity >= 60) {
    digitalWrite(22, HIGH); // Turn on the warning RBG 
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(F("HIGH HUMIDITY!!!"));
    
    // Push warning data to ThingSpeak
    ThingSpeak.setField(1, humidity);
    int x = ThingSpeak.writeFields(myChannelNumber, myApiKey);
    if (x == 200) {
      Serial.println("High humidity data pushed to ThingSpeak successfully");
    } else {
      Serial.println("Error pushing high humidity data: " + String(x));
    }
    delay(1000); // Wait for 1 seconds before continuing
  }
  
  // Wait before the next reading
  delay(1000); // Wait for 1 seconds
}
NOCOMNCVCCGNDINLED1PWRRelay Module