#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <WiFi.h>
#include <ThingSpeak.h> // Add the ThingSpeak library
// Wi-Fi credentials
char ssid[] = "Wokwi-GUEST"; // Wi-Fi credentials (can be empty for Wokwi simulation)
char pass[] = "";
// ThingSpeak channel information
long myChannelNumber = 2615086;
const char * myWriteAPIKey = "IFVF2JYDL0AM31GJ";
WiFiClient client;
int statusCode;
// 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);
// LCD display
LiquidCrystal_I2C lcd(0x27, 16, 2); // 0x27 is the I2C address of the LCD
// Define pins for the potentiometer, LED, and relays
const int potPin = 34; // Potentiometer pin
const int ledPin = 4; // LED pin
const int sprinklerRelayPin = 12; // Sprinkler relay pin
const int airCleanserRelayPin = 14; // Air cleanser relay pin
void setup() {
// Initialize LCD
lcd.init();
lcd.backlight();
// Initialize DHT sensor
dht.begin();
// Initialize pins
pinMode(ledPin, OUTPUT);
pinMode(sprinklerRelayPin, OUTPUT);
pinMode(airCleanserRelayPin, OUTPUT);
// Turn off relays initially (active low)
digitalWrite(sprinklerRelayPin, LOW);
digitalWrite(airCleanserRelayPin, LOW);
// Connect to Wi-Fi
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
}
// Connect to ThingSpeak
ThingSpeak.begin(client);
Serial.begin(115200);
Serial.println("Connected to Wi-Fi and ThingSpeak.");
}
void loop() {
// Read temperature and humidity from DHT22 sensor
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Read potentiometer value (gas sensor simulation)
int gasValue = analogRead(potPin);
// Determine air level based on the specified conditions
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";
}
// Determine gas level based on the potentiometer value
String gasLevel;
if (gasValue >= 0 && gasValue <= 1364) {
gasLevel = "Good";
} else if (gasValue >= 1365 && gasValue <= 2730) {
gasLevel = "Normal";
} else {
gasLevel = "Bad";
}
// Determine air quality based on air and gas levels
String airQuality;
if ((airLevel == "Good" || airLevel == "Normal") && (gasLevel == "Good" || gasLevel == "Normal")) {
airQuality = "Good Air Quality";
} else {
airQuality = "Bad Air Quality";
}
// Display sensor readings and air quality on 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 for 2 seconds
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Air Level: " + airLevel);
delay(2000); // Display air level for 2 seconds
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Gas Level: " + gasLevel);
lcd.setCursor(0, 1);
lcd.print("Gas Value: " + String(gasValue));
delay(2000); // Display gas level and value for 2 seconds
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Air Quality: ");
lcd.setCursor(0, 1);
lcd.print(airQuality);
delay(2000); // Display air quality for 2 seconds
// Control the LED based on air quality
if (airQuality == "Bad Air Quality") {
digitalWrite(ledPin, HIGH); // Turn on the LED
} else {
digitalWrite(ledPin, LOW); // Turn off the LED
}
// Control the sprinkler and air cleanser relays based on air quality
int sprinklerStatus = 0;
int airCleanserStatus = 0;
if (airQuality == "Bad Air Quality") {
digitalWrite(sprinklerRelayPin, HIGH); // Turn on sprinkler
digitalWrite(airCleanserRelayPin, HIGH); // Turn on air cleanser
sprinklerStatus = 1;
airCleanserStatus = 1;
} else {
digitalWrite(sprinklerRelayPin, LOW); // Turn off sprinkler
digitalWrite(airCleanserRelayPin, LOW); // Turn off air cleanser
sprinklerStatus = 0;
airCleanserStatus = 0;
}
// Display sprinkler and air cleanser status
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Sprinkler: " + String(sprinklerStatus));
lcd.setCursor(0, 1);
lcd.print("Cleanser: " + String(airCleanserStatus));
delay(2000); // Display for 2 seconds
// Send data to ThingSpeak
ThingSpeak.setField(1, temperature);
ThingSpeak.setField(2, humidity);
ThingSpeak.setField(3, gasValue); // You can add another field for gas sensor or air quality
statusCode = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (statusCode == 200) {
Serial.println("Channel update successful.");
} else {
Serial.println("Problem Writing data. HTTP error code: " + String(statusCode));
}
delay(15000); // Update ThingSpeak every 15 seconds (adjustable)
}