Tech Gyan Set
Published © MIT

IoT-Based Smart Water Tank Monitor with Leak & Overuse Alert

An IoT system that tracks daily water use, detects silent leaks, and alerts overuse—scalable for homes, cities, and villages.

BeginnerShowcase (no instructions)8 hours214
IoT-Based Smart Water Tank Monitor with Leak & Overuse Alert

Things used in this project

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

1️⃣ Pin Configuration & Global Variables

C/C++
// ====== PIN CONFIGURATION ======
#define FLOW_SENSOR_PIN 27
#define BUZZER_PIN 23
#define TRIG_PIN 5
#define ECHO_PIN 18

// ====== FLOW SENSOR ======
volatile int pulseCount = 0;
float flowRate;
float totalLiters = 0;

// ====== TIME & LOGGING ======
unsigned long lastTime = 0;
float yesterdayLiters = 0;

// ====== NIGHT LEAK CHECK ======
bool leakDetected = false;

// ====== WIFI ======
#include <WiFi.h>
const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASSWORD";

🔟 Send Data to Server (Optional – HTTP)

C/C++
#include <HTTPClient.h>

void sendDataToServer() {
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;
    String url = "http://YOUR_SERVER/save.php?water=" + String(totalLiters);

    http.begin(url);
    int httpCode = http.GET();

    if (httpCode > 0) {
      Serial.println("Data sent to server");
    }
    http.end();
  }
}

1️⃣1️⃣ Main Loop

C/C++
📞 Support & Training

Agar:

Code samajh na aaye

Wiring me doubt ho

Ya complete project banana sikhna ho

📲 Contact:
+91 9336183481

👉 Low cost me:

Complete code

Live explanation

Project build training
void loop() {
  calculateWaterUsage();
  checkNightLeak();
  readTankLevel();

  // send data every 60 seconds
  static unsigned long lastSend = 0;
  if (millis() - lastSend > 60000) {
    sendDataToServer();
    lastSend = millis();
  }
}

2️⃣ Flow Sensor Interrupt Function

C/C++
void IRAM_ATTR pulseCounter() {
  pulseCount++;
}

3️⃣ Ultrasonic Sensor (Tank Level – Optional)

C/C++
long getDistanceCM() {
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  long duration = pulseIn(ECHO_PIN, HIGH, 30000);
  long distance = duration * 0.034 / 2;
  return distance;
}

4️⃣ WiFi Connection Code

C/C++
void connectWiFi() {
  WiFi.begin(ssid, password);
  Serial.print("Connecting to WiFi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nWiFi Connected");
  Serial.println(WiFi.localIP());
}

5️⃣ Setup Function

C/C++
void setup() {
  Serial.begin(115200);

  pinMode(FLOW_SENSOR_PIN, INPUT_PULLUP);
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);

  attachInterrupt(digitalPinToInterrupt(FLOW_SENSOR_PIN), pulseCounter, FALLING);

  connectWiFi();

  lastTime = millis();
}

6️⃣ Water Consumption Calculation (Main Logic)

C/C++
void calculateWaterUsage() {
  if (millis() - lastTime >= 1000) {
    detachInterrupt(digitalPinToInterrupt(FLOW_SENSOR_PIN));

    // Formula for YF-S201
    flowRate = (pulseCount / 7.5);
    float litersPerSecond = flowRate / 60.0;
    totalLiters += litersPerSecond;

    Serial.print("Flow Rate (L/min): ");
    Serial.print(flowRate);
    Serial.print(" | Total Water Used (L): ");
    Serial.println(totalLiters);

    pulseCount = 0;
    lastTime = millis();

    attachInterrupt(digitalPinToInterrupt(FLOW_SENSOR_PIN), pulseCounter, FALLING);
  }
}

7️⃣ Night Time Leak Detection Logic

C/C++
void checkNightLeak() {
  int currentHour = (millis() / 3600000) % 24; // simple logic

  if (currentHour >= 0 && currentHour <= 4) {
    if (flowRate > 0.2) { // very small flow bhi detect
      leakDetected = true;
      digitalWrite(BUZZER_PIN, HIGH);
      Serial.println("⚠️ LEAK ALERT: Night water flow detected!");
    }
  } else {
    digitalWrite(BUZZER_PIN, LOW);
    leakDetected = false;
  }
}

8️⃣ Daily Overuse Comparison Logic

C/C++
void checkOveruse() {
  if (currentDayChanged()) {
    float percentIncrease = 0;

    if (yesterdayLiters > 0) {
      percentIncrease = ((totalLiters - yesterdayLiters) / yesterdayLiters) * 100;
    }

    Serial.print("Yesterday: ");
    Serial.print(yesterdayLiters);
    Serial.print(" L | Today: ");
    Serial.print(totalLiters);
    Serial.print(" L | Change: ");
    Serial.print(percentIncrease);
    Serial.println(" %");

    yesterdayLiters = totalLiters;
    totalLiters = 0;
  }
}

// Simple day change logic
bool currentDayChanged() {
  static int lastDay = -1;
  int today = (millis() / 86400000);
  if (today != lastDay) {
    lastDay = today;
    return true;
  }
  return false;
}

9️⃣ Tank Level Display (Optional)

C/C++
void readTankLevel() {
  long distance = getDistanceCM();
  Serial.print("Tank Distance (cm): ");
  Serial.println(distance);
}

Credits

Tech Gyan Set
9 projects • 1 follower
Thanks to Tech Gyan Set .

Comments