Water level

Hi, I’m making a water level monitoring using esp8266 nodemcu v3, hc-sr04 and blynk app (to show me values), but i’m having some difficulties…

When I upload the code to nodemcu, that blue light on the board starts to blink really fast for a few seonds and then turns off. The project on blynk app go online, but widgets dont show any value as if the sensor wanst working (but its working, I already tried with arduino uno).

I think the problem is with the baud rate, Im using 1152000bps, but im not really sure… I changed it to 9600 in the sketch, but when i upload the code go into error

Can someone help me, please?

removed unformatted code - mod

When posting code it needs to be properly formatted. Otherwise it makes it really hard to read.

Your issue is that your code is not set-up properly. you have things all over the place, and I don’t even see a setup() function.

You need to take a look at some of the examples on the Sketch Builder, and understand how to properly structure your code. The way you would write it for BLYNK is a little different that what you would normally do. You need to utilize timers and such.

You should also use the wonderful search function for this forum. There are a ton of examples of using this sensor to report distances to BLYNK.

1 Like

I’m sorry. Im new on this. here is the code. I put the void setup, but it doesnt work anywway

//#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     = "Ivonete";
const char* password = "barcelona";
char auth[] = "332afb08e2d4424ab20bb2aabdda051b"; // 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
const int V_d=8;
const int V_p=9;
long duration, distance ;                         // Duration used to calculate distance
float percentage;                                    // Percentage of Water Level in the tank


void setup() 
{
  Serial.begin(115200);                                //baud rate should be set as 9600
 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;
percentage = 100 - (distance/3);
Serial.println(distance);
Serial.println(percentage);

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

if(distance <3  )  // 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 empty"); // You get a notification when tank is Empty. Make Sure you included Notification Widget in the App
  Serial.println("Tank is empty");
  }
  else if (distance > 12 ) // 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 full"); // You get a notification when tank is Full. Make Sure you included Notification Widget in the App
 Serial.println("Tank is full");
    }
 Serial.println(distance);
  // Initiates Blynk
  Blynk.virtualWrite(V_d,distance); // Write depth of water to App
  Blynk.virtualWrite(V_p,percentage);  // Write the percentage of water in tank
 Serial.println("distance written");
}


void loop() {

} 

Did you use an electric mixer fo your code???
Have a look at the examples using Blynk and change your code accordingly…

3 Likes

It appears you are trying to utilize deepsleep, but you put it to sleep before you do anything with the sensor values.

you could try moving this to the end.

Serial.println("Sleep");
ESP.deepSleep(SLEEP_LENGTH * 1000000,WAKE_RF_DEFAULT);   //try the default deep sleep mode
delay(100);
Serial.println("wake");

Honestly this is not how I would do it.

Use a timer and after you have sent your distances to BLYNK, then go into deepsleep.

1 Like

Thank you both. I will take a look on this, but I did another project to measure distance (a simpler one) and it doesnt work either. The blue light on nodemcu doesnt turn on. Like I said before it just blink for a few seconds.

#define TRIGGER 3
#define ECHO    4

// NodeMCU Pin D1 > TRIGGER | Pin D2 > ECHO

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "356b864de8fb4e42aeca4f4152674999";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Ivonete";
char pass[] = "barcelona";

void setup() {
  
  Serial.begin (115200);
  Blynk.begin(auth, ssid, pass);
  pinMode(TRIGGER, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(BUILTIN_LED, OUTPUT);
}

void loop() {
  
  long duration, distance;
  digitalWrite(TRIGGER, LOW);  
  delayMicroseconds(2); 
  
  digitalWrite(TRIGGER, HIGH);
  delayMicroseconds(10); 
  
  digitalWrite(TRIGGER, LOW);
  duration = pulseIn(ECHO, HIGH);
  distance = (duration/2) / 29.1;

   if (distance <= 150) {
    Blynk.virtualWrite(V0, 255);
}
  else {
    Blynk.virtualWrite(V0, 0);
  }

 if (distance <= 100) {
    Blynk.virtualWrite(V1, 255);
}
  else {
    Blynk.virtualWrite(V1, 0);
  }

   if (distance <= 80) {
    Blynk.virtualWrite(V2, 255);
}
  else {
    Blynk.virtualWrite(V2, 0);
  }

   if (distance <= 40) {
    Blynk.virtualWrite(V3, 255);
}
  else {
    Blynk.virtualWrite(V3, 0);
  }

   if (distance <= 20) {
    Blynk.virtualWrite(V4, 255);
}
  else {
    Blynk.virtualWrite(V4, 0);
  }

  
  
  Serial.print(distance);
  Serial.println("Centimeter:");
  Blynk.virtualWrite(V5, distance);
  delay(200);
  Blynk.run();
}

Dude, take everything out of the loop except Blynk stuff and use timers to check distance and refresh virtual pins.

Blynk Timer is your friend…learn the way to use it from the examples.

That is why we have the Welcome Topic with all those pesky beginner instructions :stuck_out_tongue:

Meanwhile take a look at this Ultrasonic example… perhaps you might see something useful in how the code is written to support both Blynk’s timing and the Ultrasonic timing needs

1 Like

I uploaded the code and blynk app worked, but widgets showed negatives and not corresponding values. I used small parameters (cm) and i dont know if the problem is because of that.

here is the code:

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Time.h>

#define PI 3.1415926535897932384626433832795

// defines pins numbers
const int echoPin = 4;                            
const int trigPin = 5;                            
// defines variables
const float Diameter =8.5;  
const int Depth = 13;
long Duration, Distance;                        
int Liters, DepthWater;
int Percentage;

const unsigned int Period = 1000;    

const int Area = PI * ((Diameter / 2) * (Diameter / 2));          

char auth[] = "********************";
char ssid[]     = "********";
char pass[] = "******";

 BlynkTimer timer; 

void sendSensorReadings()
{
 digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);

  digitalWrite(trigPin, LOW);
  Duration = pulseIn(echoPin, HIGH);

Distance = ((Duration / 2) / 29.1) * 10;
DepthWater = Depth - Distance;                               
Liters = (Area*DepthWater)/1000;                                   
Percentage = 100-((Distance*100)/Depth);

// Initiates Blynk
  Blynk.virtualWrite(1, Liters);            
  Blynk.virtualWrite(2, DepthWater);  
  Blynk.virtualWrite(3, Percentage);

 delay(50);

 Serial.println();
    Serial.println();
    Serial.println("Tanque Liters: " + String(Liters));                       
    Serial.println("Tank DepthWater: " + String(DepthWater));   
    Serial.println("Tank Percentage: " + String(Percentage));             

if(Distance <3  )  /
{  
  Blynk.notify("Tank is almost empty"); 
  Serial.println("Tank is almost empty");
  }
  else if (Distance > 12 ) 
  {
 Blynk.notify("Tank is almost full"); 
 Serial.println("Tank is almost full");
    }

}

void setup() 
{
  timer.setInterval(Period, sendSensorReadings);    
  delay(10);
  
  Serial.begin(9600);                                
 Serial.println("Start");
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 Blynk.begin(auth, ssid, pass);
    while (Blynk.connect() == false) {
     //Wait until connected
     //Serial.print("x");
   }

Blynk.setProperty(V1, "min", 100);
Blynk.setProperty(V1, "max", 600);

Blynk.setProperty(V2, "min", 3);
Blynk.setProperty(V2, "max", 13);

Blynk.setProperty(V3, "min", 0);
Blynk.setProperty(V3, "max", 100);
}

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

Delay here is absolutely unnecessary

And here is it just a simple NO NO… and also unnecessary

Math is not my forte… but why such a large Pi value when I don’t think a basic integer is able to account for that decimal range anyhow

Well now I put it like this and still getting negative and not correspondings values. Like the max depth of my bottle is 13cm and it’s showing -67cm on widget. I really dont understand…

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Time.h>

#define PI 3

// defines pins numbers
const int echoPin = 4;                            
const int trigPin = 5;                            
// defines variables
const int Diameter =8;  
const int Depth = 13;
long Duration, Distance;                        
int Liters, DepthWater;
int Percentage;

const unsigned int Period = 1000;    

const int Area = PI * ((Diameter / 2) * (Diameter / 2));          

char auth[] = "********************";
char ssid[]     = "********";
char pass[] = "******";

 BlynkTimer timer; 

void sendSensorReadings()
{
 digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);

  digitalWrite(trigPin, LOW);
  Duration = pulseIn(echoPin, HIGH);

Distance = ((Duration / 2) / 29.1) * 10;
DepthWater = Depth - Distance;                               
Liters = (Area*DepthWater);                                   
Percentage = 100-((Distance*100)/Depth);

// Initiates Blynk
  Blynk.virtualWrite(1, Liters);            
  Blynk.virtualWrite(2, DepthWater);  
  Blynk.virtualWrite(3, Percentage);

 Serial.println();
    Serial.println();
    Serial.println("Tanque Liters: " + String(Liters));                       
    Serial.println("Tank DepthWater: " + String(DepthWater));   
    Serial.println("Tank Percentage: " + String(Percentage));             

if(Distance <3  )  /
{  
  Blynk.notify("Tank is almost empty"); 
  Serial.println("Tank is almost empty");
  }
  else if (Distance > 12 ) 
  {
 Blynk.notify("Tank is almost full"); 
 Serial.println("Tank is almost full");
    }

}

void setup() 
{
  timer.setInterval(Period, sendSensorReadings);    
  
  Serial.begin(9600);                                
 Serial.println("Start");
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 Blynk.begin(auth, ssid, pass);
    while (Blynk.connect() == false) {
     //Wait until connected
     //Serial.print("x");
   }

Blynk.setProperty(V1, "min", 100);
Blynk.setProperty(V1, "max", 600);

Blynk.setProperty(V2, "min", 3);
Blynk.setProperty(V2, "max", 13);

Blynk.setProperty(V3, "min", 0);
Blynk.setProperty(V3, "max", 100);
}

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

Why are you multiplying it by 10?

If you would take the time to research the sensor you are using, you will find the equation to calculate the distance.

Dude, thank you so much!! now its working!! :wink:

1 Like