#include <DHT.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <WiFiClient.h>
#define FAN 26
#define GLED 18
#define RLED 21
#define DUST 5
#define MQ7 33
#define DHTPIN 23
#define TESTP 35
LiquidCrystal_I2C lcd(0x27, 16, 2);
DHT dht(DHTPIN, DHT22, 15);
char notification[50] = "";
float coAmount = 0, dustAmount = 0, Humidity = 0, Temperature = 0;
const int maxTemperature = 50, maxcoAmount = 50, maxdustAmount = 50;
unsigned long duration = 0;
unsigned long starttime = 0;
unsigned long endtime = 0;
unsigned long sampletime_ms = 30000;
unsigned long lowpulseoccupancy = 0;
float ratio = 0;
float concentration = 0;
void setup() {
lcd.init();
lcd.backlight();
dht.begin();
Serial.begin(115200);
pinMode(GLED, OUTPUT);
pinMode(RLED, OUTPUT);
pinMode(FAN, OUTPUT);
pinMode(DUST, INPUT);
digitalWrite(FAN, LOW);
digitalWrite(GLED, HIGH);
digitalWrite(RLED, LOW);
starttime = millis();
}
void loop() {
checkCO();
checkDust();
tempHum();
// Check for dangerous conditions
bool danger = false;
if (coAmount > maxcoAmount) {
handleDanger("CO GAS DETECTED");
danger = true;
}
if (dustAmount > maxdustAmount) {
handleDanger("DUST DETECTED");
danger = true;
}
if (Temperature > maxTemperature) {
handleDanger("HIGH TEMPERATURE");
danger = true;
}
if (!danger) {
resetLEDs();
}
homedisplay();
}
void handleDanger(const char* alertMessage) {
digitalWrite(GLED, LOW);
digitalWrite(RLED, HIGH);
digitalWrite(FAN, HIGH);
snprintf(notification, sizeof(notification), "%s", alertMessage);
alert_display(alertMessage);
}
void resetLEDs() {
digitalWrite(FAN, LOW);
digitalWrite(RLED, LOW);
digitalWrite(GLED, HIGH);
}
void homedisplay() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("TEMP:");
lcd.print(Temperature, 1);
lcd.setCursor(7, 0);
lcd.print("HUMID:");
lcd.print(Humidity, 1);
lcd.setCursor(0, 1);
lcd.print("DUST:");
lcd.print(dustAmount, 1);
lcd.setCursor(7, 1);
lcd.print("GAS:");
lcd.print(coAmount, 1);
}
void alert_display(const char* textAlert) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("EMERGENCY ALERT");
lcd.setCursor(0, 1);
lcd.print(textAlert);
}
void checkDust() {
duration = pulseIn(DUST, LOW);
lowpulseoccupancy += duration;
endtime = millis();
if ((endtime - starttime) > sampletime_ms) {
ratio = (lowpulseoccupancy / (sampletime_ms * 10.0));
concentration = 1.1 * pow(ratio, 3) - 3.8 * pow(ratio, 2) + 520 * ratio + 0.62;
dustAmount = map(analogRead(TESTP), 0, 4095, 0, 100);
Serial.print("Dust Amount: ");
Serial.println(dustAmount);
lowpulseoccupancy = 0;
starttime = millis();
}
}
void checkCO() {
int sensorValue = analogRead(MQ7);
coAmount = map(sensorValue, 0, 4095, 0, 100);
Serial.print("CO Amount: ");
Serial.println(coAmount);
}
void tempHum() {
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Temperature = t;
Humidity = h;
Serial.print("Temperature: ");
Serial.print(Temperature);
Serial.print(" °C, Humidity: ");
Serial.print(Humidity);
Serial.println(" %");
}