Programming Blynk and normal code simultaneously

Hi,

I am using the Nodemcu 12E board to connect to blynk and control a pump and view sensor data (temperature, humidity (DHT11) and water levels (HC-SR04))
I am using Blynk library ver0.6.1.

I can view sensor data and control the pump with the Blynk app on my phone but how do I programme my pump to switch on and off automatically according to the water level in the background so as to only use the App sort of like an override
Regards

You could expand your sendSensor function to look something like this:

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); 
  float W= distanceSensor.measureDistanceCm();

 if(W<10) // If the water level is less than 10cm from the sensor...
 {
   digitalWrite(Waterpump,HIGH); // Turn the pump on
 }
 else
 {
   digitalWrite(Waterpump,LOW); // If the water is >=10cm then we'll turn the pump off
 }

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
  Blynk.virtualWrite(V4,W);
}

Thank you @PeteKnight . I tried it and works perfectly.
Much appreciated.

1 Like

Excellent!

Instead of hard-coding the 10cm trigger level (or whatever number you chose instead) you could use a global variable instead, like this:

int trigger_level =10
void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); 
  float W= distanceSensor.measureDistanceCm();

 if(W<trigger_level ) // If the water level is less than the specified distance from the sensor...

You could then change the trigger_level value from within the app. If you had a numeric input widget attached to pin V10, then you could have something like this:

BLYNK_WRITE(V10)
{
  trigger_level = param.asInt();
}

You’d also need to add this to the end of your void setup:

Blynk.syncVirtual(V10);

This would get the latest value for your numeric input when your devices starts-up and connects to Blynk.

You could also add a labelled value widget that shows you the pump status. If this widget was connected to V11 it would look like this…

 if(W<trigger_level ) // If the water level is less than the specified distance from the sensor...
 {
   digitalWrite(Waterpump,HIGH); // Turn the pump on
   Blynk.virtualWrite(V11, "Pump Running");
 }
 else
 {
   digitalWrite(Waterpump,LOW); // If the water is >=10cm then we'll turn the pump off
   Blynk.virtualWrite(V11, "Pump Stopped");
 }

Have fun!

Pete.

1 Like

Okay, I think there’s a couple of things happening here…

Firstly (asuming that this code is all within your sendSensor function), you’re setting previousMillis to zero every time you enter the function:

so this is overwriting the line that says:

at the end of your function.

Secondly, I think that maybe you have this logic the wrong way around:

Is the pump filling the tank until it’s 3cm from the sensor then switching off, or is it a pump meant to drain a sump when the level; gets to high?
If it’s filling a tank then your logic is correct, but if it’s a sump pump (as I originally assumed) then the logic is the wrong way around.

Pete.

Good day @PeteKnight.

With regards to setting previousmillis() to 0 I thought of it as a starting value for my timer, hence why I placed it there. I moved it out of the function and placed it as a global variable but that doesn’t help either. I also didn’t set previousmillis(); to 0 but my timer still doesn’t work.

Finally, I am filling a tank so there are no issues there.

Regards,
Darshal

Just noticed that you have ‘=‘ in there, not ‘-‘

Pete.

Noticed and changed. Still no difference. What happens is that the pump stays on and forgets about the level sensor.

I want my system to detect water level and get the pump to respond every 5 minutes. Is there a better way of doing this without adding more components such as a real time clock? (Delays make Blynk go wonky)

Addi g a RTC module won’t help in the slightest.

Debug what’s happening by adding informative serial print statements into your code to track the program flow and tell you the value of key variables.

Pete.

Any good?

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

#define TRIG 14
#define ECHO 12
#define DHTPIN 5     
#define DHTTYPE DHT11
#define Waterpump 0

DHT dht(DHTPIN,DHTTYPE);
UltraSonicDistanceSensor distanceSensor(TRIG, ECHO); 

char auth[] = "my token";
char ssid[] = "myssid";
char pass[] ="mypassword";
BlynkTimer timer;

void setup () {
    Serial.begin(115200);
    dht.begin();
    pinMode(Waterpump,OUTPUT);
    Blynk.begin(auth, ssid, pass);
    timer.setInterval(1000L, sendSensor);
    timer.setInterval(5 * 60 * 1000L, [](){});
}

void sendSensor()
{
    float h = dht.readHumidity();
    float t = dht.readTemperature();
    float W = distanceSensor.measureDistanceCm();

    if (W > 5) digitalWrite(Waterpump, LOW); // Turn the pump on
    if (W < 3) digitalWrite(Waterpump, HIGH); // If the water is < 3cm then we'll turn the pump off

    if (isnan(h) || isnan(t)) {
        Serial.println("Failed to read from DHT sensor!");
        return;
    }

    Blynk.virtualWrite(V5, h);
    Blynk.virtualWrite(V6, t);
    Blynk.virtualWrite(V4,W);
}

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

No this doesn’t work either but what I understand you did was add an interval of 5 minutes for any thing that is placed in [] () or {} ?

Yes I was going to do it but thought against it haha.

That’s a lambda expression. You can put code directly between the curly braces { }

What happened with the other code?

Edit:

Try changing:

if (W > 5)

To:

if (W > 5.0f)

(Same with the 3 -> 3.0f)

What code would I put in the curly braces? Do you have any extra documentation that I could read up for better understanding?

When I use if (W > 5.0f) I get an error on the Arduino IDE which says “expected ‘)’ before numeric constant” …

Regards,
Darshal

I’d forget this lambda timer function, I can’t see how it will help.
Just add some debug serial prints as I suggested earlier and see what this tells you.

Pete.

@Darshal try removing the ‘f’

@PeteKnight I left that in by accident.m when I was messing. :crazy_face:

You have to consider another important thing. If the internet or the blynk server is down, your sketch is not working as you expect. Lags a lot and maybe doesn’t switch your pump in time.