#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <WiFi.h>
#include <HTTPClient.h>
// Define the pins for the DHT22 sensor
#define DHTPIN 2 // Replace with the actual pin connected to DHT22
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2); // 0x27 is the I2C address of the LCD
const int potPin = 34; // Replace with the actual pin connected to the potentiometer
const int ledPin = 4; // Replace with the actual pin connected to the LED
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
void setup() {
// Initialize the LCD
lcd.init();
lcd.backlight();
// Initialize DHT sensor
dht.begin();
// Initialize the LED pin
pinMode(ledPin, OUTPUT);
// Connect to Wi-Fi
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
}
}
void loop() {
// Read temperature and humidity from the DHT22 sensor
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Read gas value from the potentiometer
int gasValue = analogRead(potPin);
// Create JSON payload
String payload = "{\"temperature\": " + String(temperature) +
", \"humidity\": " + String(humidity) +
", \"gasValue\": " + String(gasValue) + "}";
// Send data to the webhook
sendWebhookRequest(payload);
// Display temperature and humidity on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: " + String(temperature) + " C");
lcd.setCursor(0, 1);
lcd.print("Humidity: " + String(humidity) + " %");
delay(2000); // Display temperature and humidity for 2 seconds
// Display gas level and gas value on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Gas Level: " + getGasLevel(gasValue));
lcd.setCursor(0, 1);
lcd.print("Gas Value: " + String(gasValue));
delay(2000); // Display gas level and value for 2 seconds
// Control the LED based on air quality
if (getAirQuality(temperature, humidity, gasValue) == "Bad Air Quality") {
digitalWrite(ledPin, HIGH); // Turn on the LED
} else {
digitalWrite(ledPin, LOW); // Turn off the LED
}
}
String getGasLevel(int gasValue) {
if (gasValue >= 0 && gasValue <= 1364) {
return "Good";
} else if (gasValue >= 1365 && gasValue <= 2730) {
return "Normal";
} else {
return "Bad";
}
}
String getAirQuality(float temperature, float humidity, int gasValue) {
String airLevel;
if ((temperature >= 22 && temperature <= 30) && (humidity > 30 && humidity < 60)) {
airLevel = "Good";
} else if ((temperature >= 30 && temperature <= 40) && (humidity >= 60 && humidity <= 70)) {
airLevel = "Normal";
} else {
airLevel = "Bad";
}
String gasLevel = getGasLevel(gasValue);
if ((airLevel == "Good" || airLevel == "Normal") && (gasLevel == "Good" || gasLevel == "Normal")) {
return "Good Air Quality";
} else {
return "Bad Air Quality";
}
}
void sendWebhookRequest(String payload) {
HTTPClient http;
// Your webhook URL
const char* webhookUrl = "https://webhook.site/9bfc02ff-9501-4358-ba65-d616bd146f45";
http.begin(webhookUrl); // Specify the URL
http.addHeader("Content-Type", "application/json"); // Specify content-type header
// Send the POST request
int httpResponseCode = http.POST(payload);
// Check for response
if (httpResponseCode > 0) {
String response = http.getString();
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
Serial.print("Response: ");
Serial.println(response);
} else {
Serial.print("Error on sending POST request: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}