#define BLYNK_PRINT Serial #define BLYNK_TEMPLATE_ID "TMPL6pmSmTWSq" #define BLYNK_TEMPLATE_NAME "Sensor" #include #include #include #include #include #include #include // Blynk authentication token and WiFi credentials char auth[] = "RV_-8PWtIpIC09kAYSfGtZT6r6ezJNCk"; // Blynk auth token char ssid[] = "realme 5 Pro"; // Wi-Fi SSID char pass[] = "tugasakhir"; // Wi-Fi password // Pin Definitions const int servoPin = D6; // Pin for the servo const int mq135Pin = A0; // Pin for the MQ-135 sensor #define DHTPin D3 // Pin for the DHT sensor const int relayPin = D4; // Pin for the relay controlling the fan #define VPIN_TEMP V0 #define VPIN_HUM V1 #define VPIN_UDARA V2 // Blynk virtual pin #define VPIN_FAN V3 #define VPIN_SERVO V4 const int thresholdPPM = 65; // New threshold for MQ-135 in PPM // Servo Setup Servo myServo; int closedPosition = 0; // Position when window is closed int openPosition = 90; // Position when window is open #define DHTTYPE DHT11 DHT dht(DHTPin, DHTTYPE); LiquidCrystal_I2C lcd(0x27, 16, 2); // Address 0x27, 16x2 LCD unsigned long previousMillis = 0; // Stores the last time data was sent const long interval = 60000; // Interval to send data (1 minute) bool fanState = false; // State of the fan bool windowState = false; // State of the window void setup() { Serial.begin(115200); Serial.println("Connecting to Wi-Fi..."); Blynk.begin(auth, ssid, pass); pinMode(relayPin, OUTPUT); digitalWrite(relayPin, HIGH); // Turn off relay initially myServo.attach(servoPin); // Attach servo to the specified pin myServo.write(closedPosition); // Initial servo position dht.begin(); // Initialize DHT sensor lcd.init(); // Initialize LCD lcd.backlight(); // Turn on LCD backlight Serial.println("Setup completed"); } void loop() { Blynk.run(); sensorReadings(); unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; sendDataToBlynk(); } } void sensorReadings() { int sensorValue = analogRead(mq135Pin); // Adding a delay to stabilize the readings delay(200); sensorValue = analogRead(mq135Pin); int ppm = map(sensorValue, 0, 1023, 0, 500); // Adjusted range for MQ-135 float h = dht.readHumidity(); float t = dht.readTemperature(); if (isnan(h) || isnan(t)) { Serial.println("Failed to read from sensor"); return; } Blynk.virtualWrite(VPIN_TEMP, t); Blynk.virtualWrite(VPIN_HUM, h); Blynk.virtualWrite(VPIN_UDARA, ppm); Serial.print("Humidity: "); Serial.print(h); Serial.print("%\t"); Serial.print("Temperature: "); Serial.print(t); Serial.print("°C\t"); Serial.print("PPM: "); Serial.println(ppm); lcd.setCursor(0, 1); lcd.print("Temp: "); lcd.print(t); lcd.print("C"); lcd.setCursor(2, 0); lcd.print("ppm: "); lcd.print(String(ppm)); } void openWindow() { myServo.write(openPosition); windowState = true; Serial.println("Window opened."); } void closeWindow() { myServo.write(closedPosition); windowState = false; Serial.println("Window closed."); } void turnFanOn() { digitalWrite(relayPin, LOW); // Turn on fan fanState = true; } void turnFanOff() { digitalWrite(relayPin, HIGH); // Turn off fan fanState = false; } void sendDataToBlynk() { float h = dht.readHumidity(); float t = dht.readTemperature(); int sensorValue = analogRead(mq135Pin); int ppm = map(sensorValue, 0, 1023, 0, 500); // Adjusted range for MQ-135 if (isnan(h) || isnan(t)) { Serial.println("Failed to read from sensor"); return; } Blynk.virtualWrite(VPIN_TEMP, t); Blynk.virtualWrite(VPIN_HUM, h); Blynk.virtualWrite(VPIN_UDARA, ppm); Blynk.virtualWrite(VPIN_FAN, fanState ? 1 : 0); Blynk.virtualWrite(VPIN_SERVO, windowState ? 1 : 0); Serial.print("Humidity: "); Serial.print(h); Serial.print("%\t"); Serial.print("Temperature: "); Serial.print(t); Serial.print("°C\t"); Serial.print("PPM: "); Serial.println(ppm); lcd.setCursor(0, 1); lcd.print("Temp: "); lcd.print(t); lcd.print("C"); lcd.setCursor(2, 0); lcd.print("ppm: "); lcd.print(String(ppm)); } // Blynk function to control the fan via button BLYNK_WRITE(VPIN_FAN) { int fanControl = param.asInt(); if (fanControl == 1) { turnFanOn(); } else { turnFanOff(); } } // Blynk function to control the servo via button BLYNK_WRITE(VPIN_SERVO) { int servoControl = param.asInt(); if (servoControl == 1) { openWindow(); } else { closeWindow(); } }