#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
#include <LiquidCrystal_I2C.h>
#include <HTTPClient.h>
#define BLYNK_TEMPLATE_ID "TMPLwToQUqRw"
#define BLYNK_TEMPLATE_NAME "Air Quality Monitoring"
#define BLYNK_AUTH_TOKEN "7kuX0IEEPHLVRSK2Jhgf81qpCgL3D0Nr"
#define BLYNK_PRINT Serial
LiquidCrystal_I2C lcd(0x27, 16, 2);
byte degree_symbol[8] = {
0b00111,
0b00101,
0b00111,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000
};
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST"; // type your WiFi name
char pass[] = ""; // type your WiFi password
BlynkTimer timer;
int gas = 32;
int sensorThreshold = 100;
#define DHTPIN 2 // Connect Out pin to D2 in NODE MCU
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
const char* beeceptorURL = "https://akmns.free.beeceptor.com";
void sendDataToBeeceptor(float temperature, float humidity, int gasValue) {
HTTPClient http;
// Construct the URL with the data you want to send
String url = beeceptorURL;
url += "?temperature=" + String(temperature);
url += "&humidity=" + String(humidity);
url += "&gasValue=" + String(gasValue);
http.begin(url);
// Send the GET request
int httpCode = http.GET();
if (httpCode > 0) {
Serial.printf("HTTP response code: %d\n", httpCode);
String payload = http.getString();
Serial.println(payload);
} else {
Serial.printf("HTTP request failed: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
void sendSensor() {
float h = dht.readHumidity();
float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
int analogSensor = analogRead(gas);
Blynk.virtualWrite(V2, analogSensor);
Serial.print("Gas Value: ");
Serial.println(analogSensor);
Blynk.virtualWrite(V0, t);
Blynk.virtualWrite(V1, h);
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" Humidity: ");
Serial.println(h);
// Call the function to send data to Beeceptor
sendDataToBeeceptor(t, h, analogSensor);
}
void setup() {
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
dht.begin();
timer.setInterval(30000L, sendSensor);
lcd.begin(16, 2);
lcd.setCursor(3, 0);
lcd.print("Air Quality");
lcd.setCursor(3, 1);
lcd.print("Monitoring");
delay(2000);
lcd.clear();
}
void loop() {
Blynk.run();
timer.run();
float h = dht.readHumidity();
float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
int gasValue = analogRead(gas);
lcd.setCursor(0, 0);
lcd.print("Temperature ");
lcd.setCursor(0, 1);
lcd.print(t);
lcd.setCursor(6, 1);
lcd.write(1);
lcd.createChar(1, degree_symbol);
lcd.setCursor(7, 1);
lcd.print("C");
delay(4000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Humidity ");
lcd.print(h);
lcd.print("%");
delay(4000);
lcd.clear();
Serial.println("Gas Value");
Serial.println(gasValue);
if (gasValue < 1200) {
lcd.setCursor(0, 0);
lcd.print("Gas Value: ");
lcd.print(gasValue);
lcd.setCursor(0, 1);
lcd.print("Fresh Air");
Serial.println("Fresh Air");
delay(4000);
lcd.clear();
} else if (gasValue > 1200) {
lcd.setCursor(0, 0);
lcd.print("Gas Value: ");
lcd.print(gasValue);
lcd.setCursor(0, 1);
lcd.print("Bad Air");
Serial.println("Bad Air");
delay(4000);
lcd.clear();
}
if (gasValue > 1200) {
Blynk.logEvent("pollution_alert", "Bad Air");
}
}