Dont start pump if water tank empty

Hello to everyone,
I know it is not a big deal what the title says but I have ZERO knowledge about programming but I still love this systems and trying to do something…

So I have terrarium and I want to turn on of lights and misting shower. All nice, I found a great instructables at
I modified a code a little so it fits my needs and it works nice. Also programmed timer with inventor.

But I don’t know how to prevent runing water pump if there is no water in a tank.
I search a lot for something similar but didn’t find what I would recognize as usable for me-my knowledge.

I would really appreciate if some nice guy could help me on modifying code
I have NodeMCU ESP-E12
What I would like to do is that when red LED - named Voda, is on (water alarm) water pump is not allowed to start.

Here we can see it is connected to virtual pin6

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

/*------------------ YOUR WIFI AND BLYNK SETTINGS ------------------*/
const char ssid[] = "xxxxx";
const char pass[] = "xxxx";
char auth[] = "xxxxx";

/*------------------ DEFINE DHT SENSOR PIN ------------------*/
#define SENSOR_IN D6
#define SENSOR_IN_Type DHT22
#define SENSOR2_IN D7
#define SENSOR2_IN_Type DHT22
const int sensorPin= A0; //sensor pin connected to analog pin A0

/*------------------ DEFINE RELAY PINS ------------------*/
#define relayLight D1
#define relayPump D2
//#define relayFan D9 //RX pin on NodeMCU 
//#define relayHeat2 D5

/*---------- OTHER ----------*/
float humIN, tempIN;
float hum2IN, temp2IN;
SimpleTimer timer;

float liquid_level;
int liquid_percentage;
int top_level = 600;//Maximum water level
int bottom_level = 6;//Minimum water level

/*---------- SYNC ALL SETTINGS ON BOOT UP ----------*/
bool isFirstConnect = true;
  if (isFirstConnect) {
    isFirstConnect = false;
/*---------- LIGHT CONTROL ----------*/
  if (param.asInt()) {
    digitalWrite(D1, LOW);
    Blynk.virtualWrite(V0, 255);
  } else {
    digitalWrite(D1, HIGH);
    Blynk.virtualWrite(V0, 0);
/*---------- PUMP CONTROL ----------*/
  if (param.asInt()) {
    digitalWrite(D2, LOW);
    Blynk.virtualWrite(V1, 255);
  } else {
    digitalWrite(D2, HIGH);
    Blynk.virtualWrite(V1, 0);
// /*---------- HEAT2 CONTROL ----------*/
//  if (param.asInt()) {
//    digitalWrite(D5, LOW);
//    Blynk.virtualWrite(V2, 255);
//  } else {
//    digitalWrite(D5, HIGH);
//    Blynk.virtualWrite(V2, 0);
//  }
// /*---------- FAN CONTROL ----------*/
//  if (param.asInt()) {
//    digitalWrite(D9, LOW);
//    Blynk.virtualWrite(V3, 255);
//  } else {
//    digitalWrite(D9, HIGH);
//    Blynk.virtualWrite(V3, 0);
//  }
/*----------READ DHT SENSOR ----------*/
void sensorRead() {
  tempIN = dht_IN.readTemperature();
  humIN = dht_IN.readHumidity();
  temp2IN = dht2_IN.readTemperature();
  hum2IN = dht2_IN.readHumidity();
  Blynk.virtualWrite(V8, tempIN);
  Blynk.virtualWrite(V9, humIN);
  Blynk.virtualWrite(V12, temp2IN);
  Blynk.virtualWrite(V13, hum2IN);
  Blynk.virtualWrite(V4, liquid_level); //Virtualni pin za nivo vode direktna vrednost
  Blynk.virtualWrite(V5, liquid_percentage);//This wil show the percentage of water in the container in a virtual pin V1

void setup()
  Blynk.begin(auth, ssid, pass);
  Blynk.notify("Terarij uspesno povezan v: " + String(ssid));
  pinMode(relayLight, OUTPUT);
  pinMode(relayPump, OUTPUT);
  pinMode(sensorPin, INPUT);
//  pinMode(relayFan, OUTPUT);
//  pinMode(relayHeat2, OUTPUT);
  timer.setInterval(1000L, sensorRead);

void loop()
  liquid_level = analogRead(sensorPin);
  liquid_percentage = ((liquid_level-bottom_level)/top_level)*100;//Percentage of water in the container 
  Serial.println(liquid_level);//This will print the liquid level in the monitor 
  Serial.println(liquid_percentage);//This will print the percentage of liquid in the monitor
  Blynk.virtualWrite(V4, liquid_level);
  Blynk.virtualWrite(V5, liquid_percentage);

First off, for Blynk to work well you should move all the liquid level reads and prints out of void loop (and get rid of the dreaded delay) and into their own function.
You should then call this function every so often (every 5 seconds maybe). Once you have this working successfully then add the logic to your new function which checks if the water level is at or below a certain level (maybe <=10) and of it is then turn the pump off by changing the state of your Blynk switch which controls the pump.
This way, you’ll be able to turn the pump on regardless of the water level, but it will go off after a maximum of 5 seconds.
To make it a bit more foolproof, you’ll need to modify the pump control code to prevent it from turning the pump on at all if the liquid level is too low. There are lots of ways to do this, but my preference would be to declare a global Boolean variable that indicates if there is sufficient water or not. Set this to false at the start then true in your new liquid level function if the level is >10 (and false again if/when this liquid level test fails). Prevent the pump button widget from turning the pump on if the Boolean variable is false.


1 Like

Hi @Sajo
In reply to your private e-mail where you said…

“Thanks for your reply in my post.

I reaad your post very cerefully, but have no idea how to do that.

Like I say I have no idea about programming.
I am wiling to pay for changing the code to suite my needs.”

I’m not interested in being paid to write code, and I doubt that many - if any - of the regulars here are either.
We’ve all been in your position and learned via trial and error how to write code that will do what we want. Because of that, many people here will be willing to offer advice, support and troubleshooting tips on how to achieve what you want - provided you make the effort to lear enough about programming to get you started. In the end you’ll be glad you made the effort, as the skills you learn will be useful in the future.

Try using the Sketch Builder (link at the top of the page) to get a simple sketch working then try tweaking it to do something else as well. If you mess it up then just start again.



Is it possible to use eventor widget for this? Do something like: if v6 = 1 set pin waterpump to 0 ? I don’t know if that would overrule other signals trying to start the pump?

1 Like

Thanks for helping.
I am currently using that option in eventor, but it does not overrule start command it only stops the pump if currently running.
For now that is ok since doughters terrarium can’t be without control, and I am waiting for second nodemcu unit, for playing and trying.
From what i found out it has to be done by the coding… Slowly will get there.