#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <LiquidCrystal_I2C.h>

// Define DHT Sensor
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

// Define pins for other sensors
#define MQ2_PIN 34  // Gas sensor pin
#define REED_PIN 13  // Reed switch (Door sensor)
#define LDR_PIN 35   // LDR pin
#define PIR_PIN 14   // PIR sensor pin

// Define LCD I2C
LiquidCrystal_I2C lcd(0x27, 16, 2); // LCD address 0x27 for a 16 chars and 2 line display

void setup() {
  // Initialize LCD
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Initializing...");
  
  // Start DHT sensor
  dht.begin();

  // Set pin modes
  pinMode(MQ2_PIN, INPUT);
  pinMode(REED_PIN, INPUT);
  pinMode(LDR_PIN, INPUT);
  pinMode(PIR_PIN, INPUT);

  // Wait a few seconds before starting
  delay(2000);
}

void loop() {
  lcd.clear();  // Clear the display for new data

  // Read temperature and humidity from DHT22
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) {
    lcd.setCursor(0, 0);
    lcd.print("DHT Fail!");
  } else {
    lcd.setCursor(0, 0);
    lcd.print("T:");
    lcd.print(t);
    lcd.print("C H:");
    lcd.print(h);
    lcd.print("%");
  }
  
  delay(1000);  // Wait for a second

  // Read Gas sensor (MQ-2)
  int gasValue = analogRead(MQ2_PIN);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Gas: ");
  lcd.print(gasValue);

  delay(1000);  // Wait for a second

  // Read Magnetic Door Switch (Reed Switch)
  int doorState = digitalRead(REED_PIN);
  lcd.clear();
  if (doorState == HIGH) {
    lcd.setCursor(0, 0);
    lcd.print("Door: Closed");
  } else {
    lcd.setCursor(0, 0);
    lcd.print("Door: Open");
  }

  delay(1000);  // Wait for a second

  // Read LDR (Light Dependent Resistor)
  int ldrValue = analogRead(LDR_PIN);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Light: ");
  lcd.print(ldrValue);

  delay(1000);  // Wait for a second

  // Read PIR sensor for motion detection
  int motionState = digitalRead(PIR_PIN);
  lcd.clear();
  if (motionState == HIGH) {
    lcd.setCursor(0, 0);
    lcd.print("Motion: Detected");
  } else {
    lcd.setCursor(0, 0);
    lcd.print("Motion: None");
  }

  delay(1000);  // Wait for a second
}
mq2Breakout