[SOLVED] Cannot get a basic BLYNK_WRITE working

Hi everyone,

I wrote a simple code to watering my plants. Watering is defined by a frequency (frequence) and a watering period (delai). Both have to be modified by 2 different widget. I use Numeric Imput for now but tried stepper and slider.

I can get the frequency working, but I’m struggling to make the watering period working well. Code below

//*********BRUMISATION***********//
int freq;
int delai;
int timerV0START1;
int timerV0START2;
int timerV0STOP1;
int timerV0STOP2;

BLYNK_WRITE(V1){
  int delai = param.asInt();
  Serial.print(delai);
}
void brumicycle (){
  brumistart();
  timerV0STOP1 = timer.setTimeout(delai*1000L, brumistop);
  timerV0START2 = timer.setTimeout(delai*1000L+5000L, brumistart);
  timerV0STOP2 = timer.setTimeout(delai*1000L+5000L+delai*1000L, brumistop);
}
void brumistart(){
  Blynk.virtualWrite(V22,0);
  digitalWrite( waterrelayPin,LOW );
}
void brumistop(){
  Blynk.virtualWrite(V22,1);
  digitalWrite( waterrelayPin,HIGH );
}

BLYNK_WRITE(V0){
  int freq = param.asInt();
  if (freq != 0) {
timer.deleteTimer(timerV0START1);
timerV0START1 = timer.setInterval(freq*60000L, brumicycle);
  } else {
timer.deleteTimer(timerV0START1);
  }
}
void setup()
{
  //**********DEBUG CONSOLE**********//
  Serial.begin(115200);

  //**********ESP SETUP (OK)**********//
  Blynk.begin(auth, ssid, pass);
  delay(200);

  //**********BME SENSOR (OK)**********//
  bme.begin();
  delay(100);
  timer.setInterval(1000L, sendSensor);

  //**********SWITCH HEAT ON/OFF**********//
  pinMode(heatrelayPin, OUTPUT);

  //**********SWITCH WATER ON/OFF**********//
  pinMode(waterrelayPin, OUTPUT);
  
  //**********SWITCH LIGHT ON/OFF**********//
  pinMode(lightrelayPin, OUTPUT);
}

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

What I don’t understand is why the brumicycle () function does not take the ‘delai’ value into account as it should. If I write int delai = 3; instead of int delai; the value of delai in brumicycle () will actually be 3, but I won’t be able to change it by the widget.

Hope someone can help !

So what does the Serial.print(delai); print in the serial monitor?

You’re re-declaring the delai variable as local, by putting int in front of it in the BLYNK_WRTE(V1) function.

Change it to this:

BLYNK_WRITE(V1){
  delai = param.asInt();
  Serial.print(delai);
}

Pete.

@blynkola It prints the value of the widget, that’s why I was a bit coffuse.
@PeteKnight Thanks you, it works now ! I need to learn how to use variables well I guess :slight_smile:

1 Like