#define BLYNK_TEMPLATE_ID "TMPL3PkisgMb9"
#define BLYNK_TEMPLATE_NAME "Hello LED 2"
#define BLYNK_AUTH_TOKEN "wX9R0qNKEtPLSmaUMjQdFtG5IpZrXhEN"
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
#define DHTPIN 4
#define DHTTYPE DHT22 //Temperature //
DHT dht(DHTPIN, DHTTYPE);
#define MQPIN 34 //Gas//
#define TRIG 5
#define ECHO 18 //Ultrasonic//
#define TEMP V2
#define Hum V1
#define MQSEN V0
float h;
float t;
int threshold=50;
int gasThresh = 50;
bool tempAlertSent = false;
bool gasAlertSent = false;
void setup()
{
Serial.begin(115200);
dht.begin();
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
}
void loop()
{
Blynk.run();
// ........TEMPERATURE SENSOR........//
h = dht.readHumidity();
t = dht.readTemperature();
Blynk.virtualWrite(TEMP,t);
Blynk.virtualWrite(Hum,h);
if(t>50 && !tempAlertSent)
{
Serial.println("Temperature is high");
Blynk.logEvent("temp_alertsent", "High Temperature");
tempAlertSent= true;
}
else if(t<50)
{
Serial.println("Temperature is low");
tempAlertSent=false;
}
//. ......MQ SENSOR.......//
int mqvalue=analogRead(34);
int mapvalue=map(mqvalue,0,4095,0,100);
Blynk.virtualWrite(MQSEN, mapvalue);
if (mapvalue> gasThresh && !gasAlertSent)
{
Serial.println("High Gas");
Serial.print(mapvalue);
Blynk.logEvent("gas_alert", "High level of Gas");
gasAlertSent = true;
}
else if (mapvalue < gasThresh)
{
gasAlertSent = false;
}
Serial.print("Temperature :");
Serial.print(t);
Serial.print("||");
Serial.print("Humidity: ");
Serial.print(h);
Serial.print("||");
Serial.print("MQ Value: ");
Serial.println(mapvalue);
delay(1000);
}