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
}