// Blynk credentials
#define BLYNK_TEMPLATE_ID "TMPL3HS1M5wZH"
#define BLYNK_TEMPLATE_NAME "SWIMMING POOL MAINTENANCE SYSTEM"
#define BLYNK_AUTH_TOKEN "LvdnLxXFiyLjjHi96myYvumpNf5SJnVl"
// Pin definitions
#define PH_PIN 34 // Simulating pH sensor using potentiometer
#define DHTPIN 4
#define TRIG_PIN 14
#define ECHO_PIN 12
#define SERVO_PIN 27
#define DHTTYPE DHT22
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
#include <ESP32Servo.h>
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
DHT dht(DHTPIN, DHTTYPE);
Servo valveServo;
// Constants
float pHThresholdLow = 7.0;
float pHThresholdHigh = 8.0;
int waterLevelThreshold = 300;
bool isServoOn = false;
unsigned long previousMillis = 0;
const long interval = 2000;
void setup() {
Serial.begin(9600);
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(BLYNK_AUTH_TOKEN, ssid, pass);
// Initialize DHT sensor
dht.begin();
// Ultrasonic setup
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
// Servo setup
valveServo.attach(SERVO_PIN);
valveServo.write(0); // Default: Closed
// Potentiometer setup (simulating pH sensor)
pinMode(PH_PIN, INPUT);
Serial.println("Setup complete.");
}
// Function to control servo via Blynk
BLYNK_WRITE(V2) {
int servoState = param.asInt(); // For button state (OFF - 0, ON - 1)
if (servoState == 1) {
valveServo.write(90); // Open valve
isServoOn = true;
Serial.println("Servo motor turned ON.");
Blynk.logEvent("servo_on", "Servo motor turned ON.");
} else {
valveServo.write(0); // Close valve
isServoOn = false;
Serial.println("Servo motor turned OFF.");
}
}
// Constants for temperature and humidity thresholds
float tempThresholdLow = 20.0;
float tempThresholdHigh = 35.0;
float humidityThresholdLow = 30.0;
float humidityThresholdHigh = 70.0;
void loop() {
Blynk.run();
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi connection lost. Reconnecting...");
WiFi.reconnect();
}
// Read potentiometer value as pH level
int rawValue = analogRead(PH_PIN);
float pHValue = (rawValue / 4095.0) * 14.0; // Simulating pH range from 0-14
Serial.print("pH Value: ");
Serial.println(pHValue);
Blynk.virtualWrite(V1, pHValue);
// Check pH condition
if (pHValue < pHThresholdLow) {
Serial.println("Alert: pH level too low!");
Blynk.logEvent("ph_low_alert", "Alert: pH level too low!");
} else if (pHValue > pHThresholdHigh) {
Serial.println("Alert: pH level too high!");
Blynk.logEvent("ph_high_alert", "Alert: pH level too high!");
}
// Read DHT22 data
float tempValue = dht.readTemperature();
float humidity = dht.readHumidity();
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);
Blynk.virtualWrite(V4, humidity);
// Check temperature and humidity thresholds
if (tempValue < tempThresholdLow) {
Serial.println("Warning: Temperature too low!");
} else if (tempValue > tempThresholdHigh) {
Serial.println("Warning: Temperature too high!");
}
if (humidity < humidityThresholdLow) {
Serial.println("Warning: Humidity too low!");
} else if (humidity > humidityThresholdHigh) {
Serial.println("Warning: Humidity too high!");
}
// Measure water level
int waterLevel = measureWaterLevel();
Serial.print("Water Level: ");
Serial.print(waterLevel);
Serial.println(" cm");
Blynk.virtualWrite(V5, waterLevel);
// Check water level condition
if (waterLevel<=5) {
Serial.println("Pool is full.");
valveServo.write(0); // Close valve
isServoOn = false;
Blynk.logEvent("water_full", "Pool is full. Valve closed.");
} else if (waterLevel > waterLevelThreshold) {
Serial.println("Water level low! Opening valve...");
valveServo.write(90); // Open valve
isServoOn = true;
Blynk.logEvent("water_low", "Water level low! Valve opened.");
} else {
valveServo.write(0); // Close valve
isServoOn = false;
}
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;
}