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);
}
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:
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");
}
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.
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.
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.
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.
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.