Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

ESP8266 NodeMCU Controlled Water Flow Monitoring and Leak Detection System

View and Edit Project In Cirkit DesignerImage of ESP8266 NodeMCU Controlled Water Flow Monitoring and Leak Detection System

Circuit Documentation

Summary

This circuit is designed to monitor and control water flow using an ESP8266 NodeMCU microcontroller. It includes two water flow sensors to measure the flow rate, a pushbutton to manually start and stop the water flow, a relay to control a water pump, and a power supply to drive the pump. The ESP8266 NodeMCU is programmed to read the flow rates from the sensors, control the relay based on the pushbutton input, and automatically stop the water flow if a leak is detected.

Component List

ESP8266 NodeMCU

Pushbutton

Water Flow Sensor (x2)

Water Pump

Relay 4 Channel 5v

Power 220v

Wiring Details

ESP8266 NodeMCU

Pushbutton

Water Flow Sensor

Water Pump

Relay 4 Channel 5v

Power 220v

Documented Code

const int flowSensor1Pin = D7;  
const int flowSensor2Pin = D5;  
const int relayPin = D2;        
int flag = 0;
int Bvar = D3;

volatile int pulseCount1 = 0;
volatile int pulseCount2 = 0;
float flowRate1 = 0.0;
float flowRate2 = 0.0;
const float calibrationFactor = 7.5;  
unsigned long lastPulseTime = 0;
const unsigned long interval = 3000;  
int flowDifference = 0;
bool leakDetected = false;  // Variable to determine if a leak has been detected

void ICACHE_RAM_ATTR pulseCounter1() {
  pulseCount1++;
}

void ICACHE_RAM_ATTR pulseCounter2() {
  pulseCount2++;
}

void setup() {
  Serial.begin(115200);  

  pinMode(flowSensor1Pin, INPUT);
  pinMode(flowSensor2Pin, INPUT);
  pinMode(Bvar, INPUT);

  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, HIGH);

  attachInterrupt(digitalPinToInterrupt(flowSensor1Pin), pulseCounter1, FALLING);
  attachInterrupt(digitalPinToInterrupt(flowSensor2Pin), pulseCounter2, FALLING);
}

void loop() {
  unsigned long currentTime = millis();
  int buttonState = digitalRead(Bvar);

  if (leakDetected) {
      Serial.println("Leak Detected! Water flow stopped.");
      digitalWrite(relayPin, HIGH); // Stop water flow
      Serial.println("WARNING: Water flow has been stopped due to a detected leak!"); // Warning
  } else {
      if (buttonState == LOW) {
          digitalWrite(relayPin, LOW);
          Serial.println("Water Flow Started");
      } else {
          digitalWrite(relayPin, HIGH);
          Serial.println("Water Flow Stopped");
      }
  }

  if (currentTime - lastPulseTime >= interval) {
    noInterrupts();

    flowRate1 = (pulseCount1 / calibrationFactor);
    flowRate2 = (pulseCount2 / calibrationFactor);
    pulseCount1 = 0;
    pulseCount2 = 0;
    flowDifference = flowRate2 - flowRate1;

    interrupts();

    Serial.print("Flow Rate 1: ");
    Serial.print(flowRate1);
    Serial.println(" L/min");
    Serial.print("Flow Rate 2: ");
    Serial.print(flowRate2);
    Serial.println(" L/min");

    if (flowDifference >= 1000) {  // Threshold changed to 1000 L/min
        leakDetected = true;  // Leak detected
        Serial.println("Leak Detected! Water flow stopped.");
        digitalWrite(relayPin, HIGH); // Automatically stop water flow
        Serial.println("WARNING: Water flow has been stopped due to a detected leak!"); // Warning
    } else if (buttonState == LOW) {
        if (relayPin == HIGH)
          flag = 0;
        else
          flag = 1;
        switch (flag) {
          case 0:
            digitalWrite(relayPin, LOW);
            Serial.println(" System Stable");
            delay(1000);
            break;
          case 1:
            digitalWrite(relayPin, HIGH);
            Serial.println(" System Not Stable");
            flag = 0;
            delay(1000);
            break;
        }
        delay(200);
    } else {
        if (flowDifference >= 1000) {  // Threshold updated to 1000 L/min
          digitalWrite(relayPin, HIGH); // Automatically stop water
          Serial.println("System Failure - Water flow stopped");
          delay(1000);
        } else {
          digitalWrite(relayPin, HIGH);
          Serial.println("System Failure");
          delay(1000);
        }
    }

    lastPulseTime = currentTime;
  }
}

This code is responsible for reading the water flow rates from the sensors, controlling the relay based on the pushbutton state, and automatically stopping the water flow if a significant difference in flow rates is detected, indicating a potential leak. The relay is controlled to start or stop the water pump, and the system status is communicated via serial output.