Need Help to calculate Correct Values of Ultrasonic Sensor on blynkioT App

Recently I am making a water level control and monitor project via esp8266-01 and HC-SR04P(3.3V-5V HC-SR04P) module but i did,t get the correct values on blynktioT.

  • Note imp: We are getting correct values using the mentioned sensors without the BlynkioT application, but implementing it to the app itself gives us incorrect and fluctuating values.
  • Sensors show minimum 60cm distance everytime.
#include <Wire.h> 
#include <Blynk.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define BLYNK_PRINT Serial

#define BLYNK_TEMPLATE_ID "TMPL6pkvgpM38"
#define BLYNK_DEVICE_NAME "1hp Monoblock Smart Switch"
#define BLYNK_AUTH_TOKEN "x_mBpR4HaKIxNDaY_v1aoSFE_MKuZDTt"
char auth[] = "x_mBpR4HaKIxNDaY_v1aoSFE_MKuZDTt";//Enter your Auth token  Blynk 2.0 
char ssid[] = "";//Enter your WIFI name
char pass[] = "";//Enter your WIFI password

BlynkTimer timer;
int error;
// Define the component pins


#define relay 3   // Relay on GPIO3 esp8266-01  Logic: Level 0
#define TRIGGER 0 // for esp8266-01 GPIO 0
#define ECHO 2 //    for esp8266-01 GPIO 2


int maximumRange = 117; // maximum height of tank in cm
long duration;
float v; // volume in cubic cm
int distance; // height of water level in cm
int r = 53.2; // radius of tank in cm
double cap; // capacity in liters
int actual_height; // actual height in cm


// Keep this flag not to re-sync on every reconnection
bool isFirstConnect = true;

// This function will run every time Blynk connection is established
BLYNK_CONNECTED() {
if (isFirstConnect) {
// Request Blynk server to re-send latest values for all pins
Blynk.syncAll();
isFirstConnect = false;
}
}

void setup() {
 
 Serial.begin(115200);
 Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);   
pinMode(TRIGGER, OUTPUT);
pinMode(ECHO, INPUT);
pinMode(relay, OUTPUT);
digitalWrite(relay, HIGH);
  //Call the functions
//  timer.setInterval(100L, ultrasonic);
}
//Get the button value
BLYNK_WRITE(V2) {
  bool Relay = param.asInt();
  if (Relay == 1) {
    digitalWrite(relay, LOW);

  } else {
    digitalWrite(relay, HIGH);
}}


void loop() {
  
//double duration, distance;
digitalWrite(TRIGGER, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER, LOW);

duration = pulseIn(ECHO, HIGH);
distance= duration/58.2;

delay (50);
actual_height = maximumRange - distance;
v = ((3.14*(r*r))*(actual_height)); // formula to calculate volume in cubic cm
cap = v/1000; // final capacity in liters
Blynk.virtualWrite(V0, cap);


delay(200);

if (distance <= 115)
{
Blynk.virtualWrite(V4 , 255);

}
else
{
Blynk.virtualWrite(V4, 0);

}

if (distance <= 50)
{
Blynk.virtualWrite(V3, 255);

}
else
{
Blynk.virtualWrite(V3, 0);

}


if (distance <= 18)
{
Blynk.virtualWrite(V1, 255);
//digitalWrite(relay, HIGH);
//Blynk.virtualWrite(V2,0);
}
else
{
Blynk.virtualWrite(V1, 0);

} 
  Blynk.run();//Run the Blynk library

}

@maliarif Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

Thanks for the info Sir.

First of all, your code is a mess, and despite you saying…

the code you posted contains no Serial.print statements to show the readings you are getting from the sensors with this code.

It looks like the code you originally started out with was structured correctly, with the ultrasonic readings being taken then sent to Blynk in a separate function that was called with a BlynkTimer…

but you’ve changed that and dumped all of the code in the void loop and removed the timer.run() command and added blocking delays to control the frequency of the readings.

You also have dodgy stuff like this…

where you’re using Blynk.syncAll() instead of simply using Blynk.syncVirtual(V2). I’m also unsure why it’s necessary to use the issFirstConnect flag, the logic behind this seems strange.

I’d recommend that you go back to the original code and add-in some serial print commands and compare your readings.

Pete.