#define BLYNK_TEMPLATE_ID "TMPL3iQmLIC35"
#define BLYNK_TEMPLATE_NAME "Dht22 sensor using ESP32"
#define BLYNK_AUTH_TOKEN "1pCaKGYmFpyb0EdVrGcAd51NyVgOXv_4"
//dht sensor interface with blynk app
#include <WiFi.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_Sensor.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include "DHT.h"
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = BLYNK_AUTH_TOKEN;
//Paste auth token you copied
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Wokwi-GUEST";///Enter your wifi name
char pass[] = "";// Enter wifi password
#define DHTPIN 12 // What digital pin we're connected to select yours accordingly
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21 // DHT 21, AM2301
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int potPin = 34;
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
float humidity = dht.readHumidity();
float temperature = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
// Read gas value from the potentiometer
int gasValue = analogRead(potPin);
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V1, humidity); // select your virtual pins accordingly
Blynk.virtualWrite(V0, temperature); // select your virtual pins accordingly
Blynk.virtualWrite(V2, gasValue);
}
void displayMessage(String line1, String line2, int delayTime = 2000) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(line1);
lcd.setCursor(0, 1);
lcd.print(line2);
delay(delayTime);
}
void setup()
{
// Debug console
lcd.init();
lcd.backlight();
Serial.begin(115200);
delay(1000);
Blynk.begin(auth, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
dht.begin();
// Setup a function to be called every second
timer.setInterval(1000L, sendSensor);
}
void loop()
{
Blynk.run();
timer.run();
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
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 criteria
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 the criteria
String airQuality;
if ((airLevel == "Good" || airLevel == "Normal") && (gasLevel == "Good" || gasLevel == "Normal")) {
airQuality = "Good Air Quality";
} else {
airQuality = "Bad Air Quality";
}
// 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 air level on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Air Level: " + airLevel);
delay(2000); // Display air level for 2 seconds
// Display gas level and gas value on the LCD
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
// Display air quality on the LCD
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
}