Bhargav  Veepuri
Published © GPL3+

Water Level Monitoring On ESP12 Using Blynk

We often face the problem of water getting spilled out from our overhead water tanks. So, let us make a solution to monitor water level.

IntermediateProtip1 hour11,181
Water Level Monitoring On ESP12 Using Blynk

Things used in this project

Hardware components

NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1
ESP8266 ESP-12E
Espressif ESP8266 ESP-12E
×1
HC SR04
×1
Arduino UNO & Genuino UNO
Arduino UNO & Genuino UNO
×1

Software apps and online services

Blynk
Blynk

Story

Read more

Code

Water Level Sensor on Blynk

Arduino
//#define BLYNK_PRINT Serial        // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
//#include <TimeLib.h>
//#include <WidgetRTC.h>
//#include <DateTime.h>
//#include <DateTimeStrings.h>
#include <Time.h> 
#define SLEEP_LENGTH 15
//#define SLEEP_NIGHT 3600                        // Comment this out to enable night time sleep        

//WidgetRTC rtc;                                  // Real time clock Widget
//BLYNK_ATTACH_WIDGET(rtc, V5);                   // For virtual pin

const int led_pin = 12;                           //LED to test, connect to D6 
const char* ssid     = "Sowmith";
const char* password = "1234abcd";
char auth[] = "5abfcab6d5924fcc8fadabdb2c6b9c6b"; // Blynk authorization key
const int echoPin = 4;                            // Connect the echo pin to D2 on nodemcu
const int trigPin = 5;                            // Connect the echo pin to D1 on nodemcu
const int V_o=6;                                  // Virtual pin on Blynk app to indicate water level is LOW
const int V_t=7;                                  // Virtual pin on Blynk app to indicate water level is HIGH
long duration, distance ;                         // Duration used to calculate distance
float percent;                                    // Percentage of Water Level in the tank

// Disable the comments to display the current time

/*
void clockDisplay()
{
  // You can call hour(), minute(), ... at any time
  // Please see Time library examples for details
  BLYNK_LOG("Current time: %02d:%02d:%02d %02d %02d %d",
            hour(), minute(), second(),
            day(), month(), year());

  int gmthour = hour();
//if(gmthour>21 ||gmthour<6)
//ESP.deepSleep(SLEEP_NIGHT*1000000,WAKE_RF_DEFAULT);
  if (gmthour == 24){
     gmthour = 0;
  }
  String displayhour =   String(gmthour, DEC);
  int hourdigits = displayhour.length();
  if(hourdigits == 1){
    displayhour = "0" + displayhour;
  }
  String displayminute = String(minute(), DEC);
  int minutedigits = displayminute.length();  
  if(minutedigits == 1){
    displayminute = "0" + displayminute;
  }  
  String displaysecond = String(second(), DEC);
  int seconddigits = displaysecond.length();  
  if(seconddigits == 1){
    displaysecond = "0" + displaysecond;
  }
  String displaycurrenttime = displayhour + ":" + displayminute+ ":" +displaysecond;
  String currenttime = "Time is " + displayhour + ":" + displayminute + ":" + displaysecond;
  Serial.println(currenttime);
  Blynk.virtualWrite(V11, displaycurrenttime);          
}
*/

void setup() 
{
  Serial.begin(74880);                                //baud rate should be set as 74880 
 Serial.println("Start");
 pinMode(led_pin, OUTPUT);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 Blynk.begin(auth, ssid, password);
    while (Blynk.connect() == false) {
     //Wait until connected
     //Serial.print("x");
   }


// Disable comments to Display time on Blynk App 

/*  
 rtc.begin();
  clockDisplay();
  delay(50); 
*/

digitalWrite(led_pin, HIGH);                                // turn the LED on (HIGH is the voltage level)
Serial.println("High");
delay(2000);                                                // wait for a second
digitalWrite(led_pin, LOW);                                 // turn the LED off by making the voltage LOW
Serial.println("Low");
delay(2000); 
                    //........................ Start of code for Ultra Sonic sensor.....................//
digitalWrite(trigPin, LOW); 
delayMicroseconds(2); 

digitalWrite(trigPin, HIGH);
delayMicroseconds(10); 
 
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
                    //........................ End of code for Ultra Sonic sensor.......................//
Serial.println("Sleep");
ESP.deepSleep(SLEEP_LENGTH * 1000000,WAKE_RF_DEFAULT);   //try the default deep sleep mode
delay(100);
Serial.println("wake");
  
                    //......................Calculate the distance (in cm) based on the speed of sound..//
distance = duration/58.2;
percent = 100 - (distance/3);
Serial.println(distance);
Serial.println(percent);

Blynk.run();        // Main function to run Blynk app 

if(distance <25  )  // depending on the tank parameters change these values,"25"
{
  Blynk.virtualWrite(V_o,1023); // Low LED is ON
  Blynk.virtualWrite(V_t,0);    // High LED is OFF   
  Blynk.notify("Tank is almost empty. please turn on the motor"); // You get a notification when tank is Empty. Make Sure you included Notification Widget in the App
  Serial.println("Tank is almost empty. please turn on the motor");
  }
  else if (distance > 200 ) // depending on the tank parameters change these values,"200"
  {
  Blynk.virtualWrite(V_t,1023); // High LED is ON
  Blynk.virtualWrite(V_o,0);    // Low LED is OFF  
 Blynk.notify("Tank is almost full. please turn off the motor"); // You get a notification when tank is Full. Make Sure you included Notification Widget in the App
 Serial.println("Tank is almost full. please turn off the motor");
    }
 Serial.println(distance);
  // Initiates Blynk
  Blynk.virtualWrite(8,distance); // Write depth of water to App
  Blynk.virtualWrite(9,percent);  // Write the percentage of water in tank
 Serial.println("distance written");
}


void loop() {

} 

Credits

Bhargav  Veepuri

Bhargav Veepuri

2 projects • 11 followers

Comments

Add projectSign up / Login