#include "DHTesp.h"
#include <WiFi.h>
#include "PubSubClient.h"
#include <LiquidCrystal_I2C.h>
#define BENZENE_THRESHOLD 5.0 // Example threshold for benzene in ppm
#define AMMONIA_THRESHOLD 25.0 // Example threshold for ammonia in ppm
#define CO2_THRESHOLD 1000.0 // Example threshold for CO2 in ppm
#define SMOKING_THRESHOLD 50.0 // Example threshold for smoking in ppm
#define ALCOHOL_THRESHOLD 100.0 // Example threshold for alcohol in ppm
const char *ssid = "Wokwi-GUEST";
const char *password = "";
const char *mqttServer = "test.mosquitto.org";
const int port = 1883;
const int DHT22_PIN = 18;
const int BUZZER_PIN = 19;
const int mq135_simulator = 32;
DHTesp dhtSensor;
WiFiClient espClient;
PubSubClient client(espClient);
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 20, 4);
// Decorations while connecting to WiFi.
void spinner()
{
static int8_t counter = 0;
const char *glyphs = "\xa1\xa5\xdb";
LCD.setCursor(15, 1);
LCD.print(glyphs[counter++]);
if (counter == strlen(glyphs))
{
counter = 0;
}
}
// Connect to WiFi.
void wifiConnect()
{
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
spinner();
Serial.print(".");
}
Serial.println(" Connected");
}
// Initialize LCD
void LCD_init()
{
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
LCD.print("Connecting to ");
LCD.setCursor(0, 1);
LCD.print("WiFi ");
}
// Print local IP on LCD after connected.
void LCD_show_localIP()
{
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Online");
LCD.setCursor(0, 1);
LCD.print("Local IP: ");
LCD.print(WiFi.localIP());
LCD.setCursor(0, 2);
LCD.print("Getting data in ");
LCD.print("3");
delay(1000);
LCD.setCursor(16, 2);
LCD.print("2");
delay(1000);
LCD.setCursor(16, 2);
LCD.print("1");
delay(1000);
LCD.clear();
}
// Print data on LCD
void LCD_handle(int temperature, int humidity, int mq135_value)
{
LCD.setCursor(0, 0);
LCD.print("Current data: ");
LCD.setCursor(0, 1);
LCD.print("Temperature: ");
LCD.print(String(temperature) + "oC");
LCD.setCursor(0, 2);
LCD.print("Humidity: ");
LCD.print(String(humidity) + "%");
LCD.setCursor(0, 3);
LCD.print("MQ135: ");
LCD.print(String(mq135_value) + "PPM");
}
float Read_ppm_data()
{
int value = analogRead(mq135_simulator);
int to_5V_value = map(value, 0, 4095, 0, 1023);
float to_ppm = to_5V_value * 1200 / 1023;
return to_ppm;
}
// Function placeholders for reading sensor data
float Read_benzene_data() { return 0.0; } // Implement this function to read benzene sensor
float Read_ammonia_data() { return 0.0; } // Implement this function to read ammonia sensor
float Read_co2_data() { return 0.0; } // Implement this function to read CO2 sensor
float Read_smoking_data() { return 0.0; } // Implement this function to read smoking sensor
float Read_alcohol_data() { return 0.0; } // Implement this function to read alcohol sensor
void Warning(float temp, float humidity, float ppm, float benzene, float ammonia, float co2, float smoking, float alcohol)
{
bool gasWarning = false;
bool poorAirQuality = false;
// Check if any gas threshold is exceeded
if (benzene > BENZENE_THRESHOLD || ammonia > AMMONIA_THRESHOLD || co2 > CO2_THRESHOLD || smoking > SMOKING_THRESHOLD || alcohol > ALCOHOL_THRESHOLD)
{
gasWarning = true;
}
// Check for poor air quality based on ppm value
if (temp > 40 && temp <= 70 || ppm > 800 && ppm <= 1000)
{
poorAirQuality = true;
}
// Activate buzzer based on conditions
if (gasWarning || poorAirQuality)
{
tone(BUZZER_PIN, 262, 250); // Activate buzzer
}
else
{
noTone(BUZZER_PIN); // Deactivate buzzer
}
// Print sensor readings and warnings
Serial.println("Temp: " + String(temp, 2) + "°C");
Serial.println("Humidity: " + String(humidity, 1) + "%");
Serial.println("PPM In Air: " + String(ppm, 2) + "ppm");
Serial.println("Benzene: " + String(benzene, 2) + " ppm");
Serial.println("Ammonia: " + String(ammonia, 2) + " ppm");
Serial.println("CO2: " + String(co2, 2) + " ppm");
Serial.println("Smoking: " + String(smoking, 2) + " ppm");
Serial.println("Alcohol: " + String(alcohol, 2) + " ppm");
Serial.println("--------");
}
void mqttReconnect()
{
while (!client.connected())
{
Serial.println("Attempting MQTT reconnection...");
if (client.connect("21127147_21127365_21127644"))
{
Serial.println("Connected to your device!");
}
else
{
Serial.println("Try again in 5 seconds...");
delay(5000);
}
}
}
void setup()
{
Serial.begin(115200);
dhtSensor.setup(DHT22_PIN, DHTesp::DHT22);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(mq135_simulator, INPUT);
LCD_init();
WiFi.begin(ssid, password);
wifiConnect();
LCD_show_localIP();
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
client.setServer(mqttServer, port);
}
void loop()
{
if (!client.connected())
{
mqttReconnect();
}
client.loop();
TempAndHumidity data = dhtSensor.getTempAndHumidity();
float ppm = Read_ppm_data();
LCD_handle(data.temperature, data.humidity, ppm);
// Read values of benzene, ammonia, CO2, smoking, and alcohol sensors
float benzeneValue = Read_benzene_data();
float ammoniaValue = Read_ammonia_data();
float co2Value = Read_co2_data();
float smokingValue = Read_smoking_data();
float alcoholValue = Read_alcohol_data();
// Call Warning() function with all sensor values
Warning(data.temperature, data.humidity, ppm, benzeneValue, ammoniaValue, co2Value, smokingValue, alcoholValue);
char buffer[50];
sprintf(buffer, "{\"temperature\": %f, \"humidity\": %f, \"ppm\": %f}", data.temperature, data.humidity, ppm);
client.publish("21127147_21127365_21127644/Data", buffer);
delay(5000);
}