#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
#include <Servo.h> // Include Servo library
// Blynk Authentication
char auth[] = "Your-Blynk-Auth-Token";
// WiFi credentials
char ssid[] = "Your-WiFi-SSID";
char pass[] = "Your-WiFi-Password";
// Pin definitions
#define PH_PIN 34
#define TURBIDITY_PIN 35
#define DHTPIN 5 // Pin for DHT22 sensor
#define RELAY_PIN 13
#define TRIG_PIN 14
#define ECHO_PIN 12
#define SERVO_PIN 27 // Pin for Servo motor
// Initialize DHT sensor
DHT dht(DHTPIN, DHT22);
// Initialize Servo
Servo valveServo;
// Constants
float pHThresholdLow = 7.0; // Minimum safe pH level
float pHThresholdHigh = 8.0; // Maximum safe pH level
int turbidityThreshold = 500; // Adjust as per the sensor calibration
int waterLevelThreshold = 20; // Minimum water level in cm
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Initialize Blynk
Blynk.begin(auth, ssid, pass);
// Initialize DHT sensor
dht.begin();
// Relay setup
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);
// Ultrasonic setup
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
// Servo setup
valveServo.attach(SERVO_PIN);
valveServo.write(0); // Ensure valve is initially closed
Serial.println("Setup complete.");
}
void loop() {
Blynk.run();
// Read pH level
float pHValue = analogRead(PH_PIN) * (5.0 / 1024.0);
Serial.print("pH Value: ");
Serial.println(pHValue);
Blynk.virtualWrite(V1, pHValue);
// Read turbidity level
int turbidityValue = analogRead(TURBIDITY_PIN);
Serial.print("Turbidity Value: ");
Serial.println(turbidityValue);
Blynk.virtualWrite(V2, turbidityValue);
// Read DHT22 data (temperature and humidity)
float tempValue = dht.readTemperature(); // Temperature in Celsius
float humidity = dht.readHumidity(); // Humidity
if (isnan(tempValue) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Temperature: ");
Serial.println(tempValue);
Serial.print("Humidity: ");
Serial.println(humidity);
Blynk.virtualWrite(V3, tempValue); // Send temperature to Blynk
Blynk.virtualWrite(V4, humidity); // Send humidity to Blynk
// Measure water level
int waterLevel = measureWaterLevel();
Serial.print("Water Level: ");
Serial.println(waterLevel);
Blynk.virtualWrite(V5, waterLevel);
// Check conditions and trigger relay and servo if needed
if (pHValue < pHThresholdLow || pHValue > pHThresholdHigh || turbidityValue > turbidityThreshold || waterLevel < waterLevelThreshold) {
Serial.println("Conditions unsafe, activating pump and opening valve.");
digitalWrite(RELAY_PIN, HIGH);
valveServo.write(90); // Open valve (adjust angle as per requirement)
Blynk.notify("Unsafe swimming pool conditions detected! Pump activated, valve opened.");
} else {
digitalWrite(RELAY_PIN, LOW);
valveServo.write(0); // Close valve
}
delay(2000);
}
int measureWaterLevel() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH);
int distance = duration * 0.034 / 2;
return distance;
}