Oveya Kiruba
Published

Water Flow Controller

In this activity, we will be reading and displaying the water flow meter on blynk using NodeMCU and also control the tap with the solenoid.

AdvancedFull instructions provided5 hours547
Water Flow Controller

Things used in this project

Hardware components

Flow Sensor, Analog Output
Flow Sensor, Analog Output
×1
Solenoid Valve, 2 Way
Solenoid Valve, 2 Way
×1
SparkFun Qwiic Single Relay
SparkFun Qwiic Single Relay
×1
Jumper wires (generic)
Jumper wires (generic)
×1
12 v adapter
×1
Female DC Power Plug
×1
Plastic Enclosure, Project Box
Plastic Enclosure, Project Box
×1
USB-A to Mini-USB Cable
USB-A to Mini-USB Cable
×1
NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1
Raspberry Pi 2 Model B
Raspberry Pi 2 Model B
×1
Breadboard (generic)
Breadboard (generic)
×1
Heat Shrink Tube
×1

Software apps and online services

Arduino IDE
Arduino IDE
Blynk
Blynk

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Solder Wire, Lead Free
Solder Wire, Lead Free

Story

Read more

Schematics

Circuit

Code

Code for water flow controller

C/C++
#define BLYNK_PRINT Serial           // Uncomment for debugging 

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#include <ESP8266mDNS.h>  
#include <WiFiUdp.h>  
#include <ArduinoOTA.h>  


char auth[] = "Enter your Token";


char ssid[] = "wifi name ";
char pass[] = "wifi password ";


SimpleTimer timer;
WidgetTerminal terminal(V2);
WidgetLCD lcd(V7);
bool simulation = false;     

byte sensorInterrupt = 4;  
byte sensorPin       = 4;  
byte pumpInterrupt   = 5; 
byte pumpPin         = 5; 


float calibrationFactor = 4.5;
volatile byte pulseCount;  
float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;
unsigned long oldTime;

#define averageperiod 5         
int countseconds = 0;           
int averageflow = 0;            
bool notificationsent = false;  
bool pumpState = false;         
bool masterState = false;       
bool flowoffprintonce = false;  
int rowIndex = 0;                
String currentDate  ;
String daybefore;

int menu=0;
int s;



void ICACHE_RAM_ATTR MyInterruptHandler();

void setup()
{

  Serial.begin(115200);
  Serial.println();
 pinMode(sensorPin, INPUT);
 digitalWrite(sensorPin, HIGH);
  pinMode(pumpPin, OUTPUT);
  digitalWrite(pumpPin, LOW);  /



 Blynk.virtualWrite(V10,"clr");
   timer.setInterval(1000L,table);


  
 pinMode(TRIGGER, OUTPUT);
  pinMode(ECHO, INPUT);
  flowMilliLitres   = 0;
  totalMilliLitres  = 0;
  oldTime           = 0;
  flowRate          = 0.0;
  if(simulation == true){
    pulseCount        = 47;
  }
  else{
    pulseCount        = 0;
  }

  
  attachInterrupt(sensorInterrupt, pulseCounter, FALLING);

  
  attachInterrupt(pumpInterrupt, pumpToggle, CHANGE);
  //Blynk.begin(auth, ssid, pass);FOR WITH OUT SERVER 
  Blynk.begin(auth, ssid, pass, IPAddress("YOUR IPADRESS"), 8080);

  terminal.println("Connected to Blynk");
  terminal.println(WiFi.localIP());
  terminal.flush(); 
  timer.setInterval(1000L, showFlow);
  timer.setInterval(100L, pumpControl); 
}
BLYNK_CONNECTED() {
Blynk.syncVirtual(V5);
Blynk.syncVirtual(V4);
}
void showFlow()  {
    detachInterrupt(sensorInterrupt);  
    flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
    oldTime = millis();
    flowMilliLitres = (flowRate / 60) * 1000;
    totalMilliLitres += flowMilliLitres;
    
    
    Serial.print("Flow rate: ");
    Serial.print(int(flowRate));  
    Serial.print("L/min");
    Serial.print("\t");          
    Serial.print("Output Liquid Quantity: ");        
    Serial.print(totalMilliLitres);
    Serial.print("mL"); 
    Serial.print("\t");      
    Serial.print(totalMilliLitres/1000);
    Serial.println("L");
    if(simulation != true){  
      pulseCount = 0; 
    }
    countseconds++;
    if(countseconds > 0){    
      averageflow = averageflow + flowRate;  
    }
    if(countseconds == averageperiod){
      Serial.print("Average water flow in litres / M is ");
      Serial.println(averageflow / averageperiod);
      Blynk.virtualWrite(V0, int(averageflow) / averageperiod);
      Blynk.virtualWrite(V1, totalMilliLitres/1000);
      countseconds = 0;  
      chkFlow();           
      averageflow = 0;    
    }
    attachInterrupt(sensorInterrupt, pulseCounter, FALLING);  
    }

void chkFlow(){
  if((averageflow > 3) && (notificationsent == false)){  
    Serial.println("Water IS flowing.");
    lcd.clear();
    lcd.print(0,0,"Water is flowing");
    Blynk.email("Water Flow Sensor", "Water IS flowing.");
    Blynk.notify("Sensor: Water IS flowing.");
    notificationsent = true;         
    flowoffprintonce = false;       
    
  }
  if((averageflow <= 3)&& (flowoffprintonce == false)){
    Serial.println("Water is NOT flowing.");
    lcd.clear();
    lcd.print(1,0,"Water is NOT");
    lcd.print(4,1,"flowing");
    notificationsent = false;  
    flowoffprintonce = true;      
    s=0;
  }
 if(totalMilliLitres/1000 <= 50){
     digitalWrite(pumpPin, HIGH);   
   }
if(totalMilliLitres/1000 >50 && menu == 1){
    digitalWrite(pumpPin, LOW);   
    Blynk.virtualWrite(V8, "OFF");
    Blynk.virtualWrite(V4, 0);     
   }


if(totalMilliLitres/1000 <= 50){
     digitalWrite(pumpPin, HIGH);   
   }
if(totalMilliLitres/1000 >50 && menu == 0){
  Blynk.email("Water Flow Sensor", "Over water usage pl turn off the tap");
 Blynk.notify("Sensor: Over water usage pl turn off the tap and also do reset ");   
    
   }


}
void pulseCounter()
{
  pulseCount++;  
}

void pumpToggle(){               
  pumpState = !pumpState;         
  Serial.println(pumpState);      
}

void pumpControl()               
{
  detachInterrupt(pumpInterrupt);  
  if(pumpState == masterState){
    
  }
  else{  
    masterState = pumpState;
    if(pumpState == true){
    Blynk.virtualWrite(V4, 1);
    Blynk.virtualWrite(V8, "ON");
   
    Serial.println("Pump turned ON"); 
    }
    else{
      Blynk.virtualWrite(V4, 0);
      Blynk.virtualWrite(V8, "OFF");
   
      Serial.println("Pump turned OFF"); 
    }
  }
  terminal.flush();
  attachInterrupt(pumpInterrupt, pumpToggle, CHANGE);   
}

void table()                  
{
  
    Blynk.virtualWrite(V10, "add", rowIndex,"totalMilliLitres"," liters"); 
    Blynk.virtualWrite(V10, "pick", rowIndex);
    Blynk.virtualWrite(V1, 0);
    Serial.println("working");
    rowIndex++;
    }

  
BLYNK_WRITE(V3){   // reset button
  int resetdata = param.asInt();
  if(resetdata == 1){
    Serial.println("Clearing data");
    Blynk.virtualWrite(V0, 0);
    Blynk.virtualWrite(V1, 0);
    averageflow = 0;
    countseconds = 0;
    flowMilliLitres = 0;
    totalMilliLitres = 0;
  }
}
BLYNK_WRITE(V20){   
  int resetdata = param.asInt();
  if(resetdata == 1){
    Serial.println("Clearing table data");
    Blynk.virtualWrite(V10, "clr");
   
  }
}
BLYNK_WRITE(V4){   
  int controlRelay = param.asInt();
  if(controlRelay == 1){
    digitalWrite(pumpPin, HIGH);  
    Blynk.virtualWrite(V8, "OFF"); 
  }
  else{
    digitalWrite(pumpPin, LOW);  
     Blynk.virtualWrite(V8, "ON"); 
  }
  pumpControl();
}
BLYNK_WRITE(V5)
{
 switch(param.asInt()){
  case 1:{
     lcd.clear();
    lcd.print(1,0,"Automatic Mode");
    lcd.print(5,1,"Selected");
    menu=1;
  

    
    break;
  }
  case 2:{
    lcd.clear();
    lcd.print(3,0,"Manual Mode");
    lcd.print(5,1,"Selected");
    menu=0;
    

    break;
  }
 }
}
void loop(){
  Blynk.run();
  timer.run();
}

Credits

Oveya Kiruba

Oveya Kiruba

0 projects • 0 followers
Thanks to saurabh47.

Comments

Add projectSign up / Login