From 09ec95bdedeb90658025cd930a64d133a965c475 Mon Sep 17 00:00:00 2001 From: Safina345 Date: Wed, 24 Jul 2024 11:48:22 +0700 Subject: [PATCH] g --- coba-gabung.ino | 274 ++++++++++++++++++++++++++++++++++++++++++++++++ kombinasi.ino | 192 +++++++++++++++++++++++++++++++++ 2 files changed, 466 insertions(+) create mode 100644 coba-gabung.ino create mode 100644 kombinasi.ino diff --git a/coba-gabung.ino b/coba-gabung.ino new file mode 100644 index 0000000..b05320b --- /dev/null +++ b/coba-gabung.ino @@ -0,0 +1,274 @@ +#define BLYNK_PRINT Serial +#define BLYNK_TEMPLATE_ID "TMPL6pmSmTWSq" +#define BLYNK_TEMPLATE_NAME "Sensor" + +#include +#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 float tempThreshold = 30.0; +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 tempNotified = false; // Flag to track if temp notification was sent +bool gasNotified = false; // Flag to track if gas notification was sent + +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; + sendDataToServer(); + } +} + +void sensorReadings() { + int sensorValue = analogRead(mq135Pin); + 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(); + int fanState = digitalRead(relayPin) == LOW ? 1 : 0; + int servoState = myServo.read(); + + 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); + Blynk.virtualWrite(VPIN_SERVO, servoState); + + 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(ppm); + + // Automatic fan control based on temperature + if (t > tempThreshold) { + digitalWrite(relayPin, LOW); // Turn on fan + fanState = 1; + Serial.println("Temp > 30°C. Fan ON"); + if (!tempNotified) { + Blynk.logEvent("high_temp", "Warning: Temperature above safe limit"); + tempNotified = true; + } + turnFanOn(); + } else { + digitalWrite(relayPin, HIGH); // Turn off fan + fanState = 0; + Serial.println("Temp <= 30°C. Fan OFF"); + tempNotified = false; + turnFanOff(); + } + + // Gas detection logic + if (ppm > thresholdPPM) { + Serial.println("Gas detected above safe limit"); + if (!gasNotified) { + Blynk.logEvent("high_gas", "Warning: Gas concentration above safe limit"); + gasNotified = true; + } + openWindow(); // Open window + } else { + Serial.println("Gas not detected"); + gasNotified = false; + closeWindow(); // Close window + } +} + +void openWindow() { + myServo.write(openPosition); + Serial.println("Window opened."); + sendServoCommand("ON"); // Open window command +} + +void closeWindow() { + myServo.write(closedPosition); + Serial.println("Window closed."); + sendServoCommand("OFF"); // Close window command +} + +void turnFanOn() { + digitalWrite(relayPin, LOW); // Turn on fan + sendFanCommand("ON"); // Send fan ON status to server +} + +void turnFanOff() { + digitalWrite(relayPin, HIGH); // Turn off fan + sendFanCommand("OFF"); // Send fan OFF status to server +} + +void sendDataToServer() { + 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 + int fanState = digitalRead(relayPin) == LOW ? 1 : 0; + int servoState = myServo.read(); + + if (isnan(h) || isnan(t)) { + Serial.println("Failed to read from sensor"); + return; + } + + sendSensorDataToServer(t, h, ppm, fanState, servoState); +} + +void sendSensorDataToServer(float temp, float hum, int ppm, int fanState, int servoState) { + if (WiFi.status() == WL_CONNECTED) { + WiFiClient client; + HTTPClient http; + + http.begin(client, "http://192.168.85.242/db/dbsensor.php"); + http.addHeader("Content-Type", "application/x-www-form-urlencoded"); + + String postData = "suhu=" + String(temp) + "&kelembapan=" + String(hum) + "&udara=" + String(ppm) + + "&Fan=" + String(fanState) + "&jendela=" + String(servoState); + + Serial.print("Sending POST data: "); + Serial.println(postData); + + int httpResponseCode = http.POST(postData); + if (httpResponseCode > 0) { + String response = http.getString(); + Serial.println(httpResponseCode); + Serial.println(response); + } else { + Serial.print("Error on sending POST: "); + Serial.println(httpResponseCode); + } + http.end(); + } else { + Serial.println("WiFi disconnected"); + } +} + +void sendServoCommand(String command) { + if (WiFi.status() == WL_CONNECTED) { + WiFiClient client; + HTTPClient http; + + http.begin(client, "http://192.168.85.242/db/dbsensor.php"); + http.addHeader("Content-Type", "application/x-www-form-urlencoded"); + + String postData = "servoCommand=" + command; + + Serial.print("Sending POST data: "); + Serial.println(postData); + + int httpResponseCode = http.POST(postData); + if (httpResponseCode > 0) { + String response = http.getString(); + Serial.println(httpResponseCode); + Serial.println(response); + } else { + Serial.print("Error on sending POST: "); + Serial.println(httpResponseCode); + } + http.end(); + } else { + Serial.println("WiFi disconnected"); + } +} + +void sendFanCommand(String command) { + if (WiFi.status() == WL_CONNECTED) { + WiFiClient client; + HTTPClient http; + + http.begin(client, "http://192.168.85.242/db/dbsensor.php"); + http.addHeader("Content-Type", "application/x-www-form-urlencoded"); + + String postData = "fanCommand=" + command; + + Serial.print("Sending POST data: "); + Serial.println(postData); + + int httpResponseCode = http.POST(postData); + if (httpResponseCode > 0) { + String response = http.getString(); + Serial.println(httpResponseCode); + Serial.println(response); + } else { + Serial.print("Error on sending POST: "); + Serial.println(httpResponseCode); + } + http.end(); + } else { + Serial.println("WiFi disconnected"); + } +} diff --git a/kombinasi.ino b/kombinasi.ino new file mode 100644 index 0000000..d8aca89 --- /dev/null +++ b/kombinasi.ino @@ -0,0 +1,192 @@ +#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(); + } +}