/*
______________________________________________________________________________
|                               Smart Appliance Manager                      |
|-> This device utilizes DHT22 sensor to measure temperature and LDR sensor  |
|    for monitoring light intensity in a room.                               |
|-> It adjusts the brightness of an LED depending on ambient light intensity.|
|-> It controls a fan and an air conditioner (AC) based on the temparature.  |
|-> The current temparature, LED Brightness, fan and AC status are displayed |
|    on a 20x4 I2C LCD screen.                                               |
|-> Further, the fan and AC status are indicated using an LED.               |
|____________________________________________________________________________|

*/

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

LiquidCrystal lcd(0, 1, 2, 3, 4, 5); // Initialize LCD object

#define DHTPIN 8
#define DHTTYPE DHT22 
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor object
#define ledPin 9 // LED pin
#define ldrPin 28 // LDR pin
#define fanRelayPin 22 // Fan relay pin
#define acPin 17 // AC relay pin

int ledBrightness = 0;
bool isFanOn = 0;
bool isACOn = 0;

// WiFi Setup
const char* ssid = "Wokwi-GUEST"; 
const char* password = ""; 
const char* thingSpeakApiKey = "QXP1TNU4OU8UEZTK";
const unsigned long thingSpeakChannelId = 2545155;

WiFiClient piClient;

// Function to check WiFi connection status
void setup_wifi() {
  delay(10);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void setup() {
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);
  pinMode(ldrPin, INPUT);
  pinMode(fanRelayPin, OUTPUT);
  pinMode(acPin, OUTPUT);
  
  dht.begin();
  lcd.begin(20, 4);
  setup_wifi();
  ThingSpeak.begin(piClient);
}

void loop() {
  readSensors(); 
  controlDevices();
  displayStatus(); 
  delay(1);
}

// Function to read sensor values
void readSensors() {
  // Reading LDR value
  int ldrValue = analogRead(ldrPin);
  ledBrightness = map(ldrValue, 170, 1023, 0, 255);
  int ledPercentage = map(ledBrightness, 0, 255, 0, 100);
  if (ledPercentage<0){
    ledPercentage=0;
  }
  // Reading temperature sensor value
  int tempValue = dht.readTemperature();
  lcd.setCursor(2, 2);
  lcd.print("LED");
  lcd.setCursor(8, 2);
  lcd.print("FAN");
  lcd.setCursor(14, 2);
  lcd.print("AC");
  lcd.setCursor(2,3);
  lcd.print(ledPercentage);
  lcd.print(" %");
  lcd.setCursor(0,0);
  lcd.print("TEMP : ");
  lcd.print(tempValue);
  lcd.print(" C");
  
  // Controlling fan and AC based on temperature
  if (tempValue >= 30) { 
    isFanOn = 0;
    isACOn = 1;
  } else if (tempValue < 30 && tempValue >= 20) {
    isFanOn = 1;
    isACOn = 0;
  } else if (tempValue < 20) {
    isFanOn = 0;
    isACOn = 0;
  }
  // Updating values in ThingSpeak
  ThingSpeak.setField(1, tempValue);
  ThingSpeak.setField(2, ledPercentage);
  ThingSpeak.setField(3, isFanOn);
  ThingSpeak.setField(4, isACOn);
  int statusCode = ThingSpeak.writeFields(thingSpeakChannelId, thingSpeakApiKey);
}

// Function to control devices based on sensor readings
void controlDevices() {
  analogWrite(ledPin, ledBrightness);
  digitalWrite(fanRelayPin, isFanOn ? HIGH : LOW);
  digitalWrite(acPin, isACOn ? HIGH : LOW);
}

// Function to display device status on LCD
void displayStatus() {
  lcd.setCursor(8, 3);
  lcd.print(isFanOn ? "On " : "Off");
  lcd.setCursor(14, 3);
  lcd.print(isACOn ? "On " : "Off");
}

NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module