How to use If Else statement in param.asInt?

Hi

I am new to Blynk and need help in working on a simple project which can operate a water pump using button in Blynk app.

I am trying to make the water pump to be controllable through two options where,

  1. manually control by pressing the first button.
  2. automatically pump the water based on the soil moisture sensor value having the second button to be activated (switch button) first.

While I have no problem working on the first option, however the second option is the one I am struggling with… I have tried to use IF ELSE statement so that the pump will operates when the soil moisture is at a specific level or else it will not. When I pressed the second button, the water pump will only process based on the first value that come up upon the button is activated.

Have the value of the moisture is at the ON level on the first time the button is pressed, the water pump will operates and does not turn off even though the moisture level has changed to OFF level. If the button is switched off and on again only then it will read the latest moisture level and turn off the water pump if the level is at OFF level.

What I am trying to achieve here in the program is that if the second button is switched on, the user can rely on the system to operate the water pump automatically based on the soil moisture level.

I am using Arduino UNO+ESP8266 as the WiFI shield and running the latest Blynk version through its app.

Any help on correction, suggestions and recommendation would be much appreciated. Thanks.


#define BLYNK_TEMPLATE_ID "TMPLBH_UJakl"
#define BLYNK_DEVICE_NAME "Water pump Test1"
#define BLYNK_AUTH_TOKEN "4MQeEtG41MXPU8Bejr0gf2n3oGwI3Jw8"
#define BLYNK_PRINT Serial
                          
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

#include <SoftwareSerial.h> //Uno-ESP Communication
SoftwareSerial EspSerial(2, 3); // RX, TX

#define ESP8266_BAUD 38400 //ESP8266 baud rate:
ESP8266 wifi(&EspSerial);

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "user";
char pass[] = "pass";

const int pump1 = 8;


// Button for (OPTION1)(MANUAL)
BLYNK_WRITE(V7){                              
  if(param.asInt() == 1)
  {
  // Current Flowing (Relay)
  digitalWrite(pump1,HIGH);                   
  delay(3500); // 3.5 secs ~around 100ml

  // Current Not Flowing (Relay)
  digitalWrite(pump1,LOW);
  delay(500); // 0.5 sec
  }
}

// Button for (OPTION2)(AUTO)
BLYNK_WRITE(V6){                              
  if(param.asInt() == 1)
  {
  int value = analogRead(A0);
  value = map(value, 0, 1023, 0, 100);
  Blynk.virtualWrite(V2, value);

  if(value >=45 && value <=70){
  // Current Flowing (Relay)
  digitalWrite(pump1,HIGH);                   
  }
  else
  {
  // Current Not Flowing (Relay)
  digitalWrite(pump1,LOW);
  }
 }
}
    
void setup() {
  Serial.begin(9600);
  EspSerial.begin(ESP8266_BAUD);
  Blynk.begin(auth, wifi, ssid, pass);       
  pinMode(pump1, OUTPUT);
  }

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


BLYNK_WRITE is a function called every time device gets an update of Virtual Pin value from the server or app. You should create a separate void function and call it using a timer instead.

2 Likes

First of all, you shouldn’t be using delays in your Blynk code. Delays block all code execution during the delay period and so in effect they cut the device off from Blynk during this period, making it unresponsive during the delay period. If your delays are long enough then the Blynk server will think the device is MIA and mark it as offline, and the device itself will go offline when it hasn’t heard from the Blynk server.

Also, you’re using 38400 baud for communication with your ESP-01 using a SoftwareSerial ’virtual’ COM port. Your Uno doesn’t have enough processing power to consistently emulate a COM port at this baud rate.

On to your issue…
The BLYNK_WRITE(vPin) callbacks are only triggered when the value of the virtual pin changes on the server, so in Auto mode there is no looping process that takes readings and compares them to your target value and takes the appropriate action based on this info.
To get around this you need to create a timed process using BlynkTimer which called your automatic code, maybe once every 5 seconds. This timed function should start by doing a check to see if your device is in Auto or a Manual mode. To achieve this, your auto/manual switch simply needs to set a global variable to the appropriate value.

Reading material…

Using timers to call a function…

Option to use Timeout Timers if a delay() equivalent is needed, and discussion on auto/manual mode…

Info on baud rates when using SoftwareSerial…

Also, if you’re using one of the paid subscriptions then you might want to think about using the Segmented Switch widget to give you auto/manual on/manual off settings with a single switch.

Pete.

1 Like

Thank you very much for the prompt reply, Pete. I’ve spent quite some time searching for solutions before finally decided to ask for help here in the forum… I’m definitely taking your advice and hope to get it done soon.