Blynk + wemos d1 mini + JSN SR04T ( water level monitor)

Currently this code allows me to read the level in percentage in my sump pit. thats great. however i need your help in modifying it so that it will notify me in case the level rises above a certain percent. also i would like it to continue sending notification every 1 min or so.

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

char auth[] = "    ";
SimpleTimer timer;
#define echoPin D7 // Echo Pin
#define trigPin D6 // Trigger Pin
 
long duration, distance; // Duration used to calculate distance
 
void setup()
{
Serial.begin (9600);
Blynk.begin(auth, "  ", "  ");
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
timer.setInterval(1000L, program);
}

 
void program()
{
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;
distanceinches = distance*0.393701 //convert cm to inches
LevelPercent = distanceinches/28.5 //28.5 inches deep sump pit

}

BLYNK_READ(V8)
{
Blynk.virtualWrite(8,distance);// virtualpin 8 distance
}


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

First of all, do you really need to check the water level every second?

Your void program function needs an if statement once you’ve taken the distance reading. If the result (presumably you’ll check distanceinches) is less than your acceptable level then you’ll want to trigger your alert routine.

If you can live with taking water level readings once every minute then one alert per water level reading cycle is what you’ll get. Personally I hate repeated alerts, but that’s your choice.
If you need to read the water level more frequently than once per minute you’ll need to use a flag, counter or timer to ensure that you don’t send one alert every time the water level is checked. How you implement that is up to you.

Pete,

1 Like

so…

timer.setInterval(60*1000L, program);

and

.
.
.
LevelPercent = distanceinches/28.5 //28.5 inches deep sump pit
if(LevelPercent > 70) Blynk.notify("The subject is drowning");

or

bool warnMeOnce = false;
void program()
{
.
.
.
if(LevelPercent > 70 && !warnMeOnce){
  Blynk.notify("The subject is drowning");
  warnMeOnce = true
}

there are a 101 other methods to limit the warning amount or change the repeatedness, but this is the general idea. As pete said: how is up to you.

Thank you. I will give it a try and let you know how it works out.