Watering flower on esp32 via WIFI + capacitive soil moisture sensor

Hello

I am making a project in which i will be watering (via module relay 5v) a flower on esp32s via wifi on blynk 2.0 in three modes. the first mode is the presentation mode
to check if everything is working properly. I wonder if it would be better to give a push button instead switch but I’m don’t know how do it properly
in mode 1. In the second mode after switching on the switch I would like to use the slider to determine how many days to water the flower and I want to see this value on gauge.
In the third mode after pressing the switch I would like the soil moisture to decide to water the flower
I have measured that from 640 it is dry and from 270 it is wet. I am using a capacitive soil moisture sensor and they are on the analog pins on the esp32s board
I specifically checked which GPIO is intended for this and chose pin 25.

for each mode i want to add a switch to make it work when switched on. at first the project worked but after
adding this condition, complications started to appear.
Can someone please take a look why this code is not working properly and tell me what I write wrong and what to do to make it correct.


// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
// See the Device Info tab, or Template settings
#define BLYNK_TEMPLATE_ID ""
#define BLYNK_DEVICE_NAME ""
#define BLYNK_AUTH_TOKEN ""


// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

char auth[] = BLYNK_AUTH_TOKEN;

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";

BlynkTimer timer; 

#define relay1_pin 13 // water pump // on board 5 pin left
#define sensor 25 // soil moisture // 11 pin left

const int dry = 640;
const int wet = 270;

int relay1_state = 0; // state mode 1
int relay2_state = 0; // state mode 2 auto
int pinValue = 0; // state  v4 - gauge
int sensorVal = 0; // state for soil moisture

//Change the virtual pins according the rooms
#define button1_vpin    V1 // 1st mode on/off
#define button2_vpin    V2 // auto 
#define button3_vpin    V3 // slider
#define button4_vpin    V4 // gauge slider
#define button5_vpin    V5 // soil moisture
//------------------------------------------------------------------------------
// This function is called every time the device is connected to the Blynk.Cloud
// Request the latest state from the server
BLYNK_CONNECTED() {
  Blynk.syncVirtual(button1_vpin);
  Blynk.syncVirtual(button2_vpin);
  Blynk.syncVirtual(button3_vpin);
  Blynk.syncVirtual(button4_vpin);
  Blynk.syncVirtual(button5_vpin);
}
//--------------------------------------------------------------------------
// This function is called every time the Virtual Pin state change
//i.e when web push switch from Blynk App or Web Dashboard

BLYNK_CONNECTED() {
    Blynk.syncAll();
}


BLYNK_WRITE(button1_vpin)
  { 
    if(button1_vpin == HIGH)
    {
 Blynk.virtualWrite(button2_vpin, LOW);
     button2_vpin = LOW; 
    Serial.println("11111  "); // test this function in console
    relay1_state = param.asInt(); 
    
//   Blynk.notify("You just watered your plant."); // for info ??
if(relay1_state == HIGH) {// if mode 1 is ON
  digitalWrite(relay1_pin, relay1_state); // instead state1 write LOW??
  Serial.println("water flow 2 sek:");
  delay(2000); 
  } 
else  { 
  relay1_state = LOW;
  digitalWrite(relay1_pin, HIGH);
  Serial.print("no water");
    }
  }
//--------------------------------------------------------------------------
BLYNK_WRITE(button2_vpin) {  
  Serial.print("2222:  "); // check if function works
if(button2_vpin == HIGH)
  {
    button1_vpin = relay1_state; // or low??
  relay2_state = param.asInt();
  digitalWrite(relay1_pin, relay2_state); 
   Serial.print("mode 2 auto is on");
}
 
//--------------------------------------------------------------------------
BLYNK_WRITE(button3_vpin)
{ 
Serial.print(" 3333i :  "); // check if function work
  if(relay2_state == HIGH) {
  pinValue = param.asInt(); // assigning incoming value from pin V3 to a variable
  Serial.print("auto switch is ON - confirmation");
 // analogWrite(miernik, pinValue); // not analogwrite on esp32 is something different
  Blynk.virtualWrite(button4_vpin, pinValue);  // v4 is gauge slidera
  Serial.print("V3 Slider value is: ");
  Serial.println(pinValue);
      }
}

// ---- //  moisture sensor v5
BLYNK_WRITE(button5_vpin) {  
Serial.print(" moisture lvl :  ");
 sensorVal = analogRead(sensor); // can i use analogRead here on virtual pins
 int proWilg = map(sensorVal, wet, dry, 100, 0); 
 Serial.print(sensorVal); 
if(sensorVal > 640 ){ // moisture sensor on this lvl is dry
  digitalWrite(relay1_pin, LOW); // turn on water pump
  Serial.print("watering ");
}
  else {
    digitalWrite(relay1_pin, HIGH);
    Serial.println("end watering");
    Serial.print(proWilg);
     Serial.println("%");
     delay(100);
  }
 }

//--------------------------------------------------------------------------
void setup()
{
  // Debug console
  Serial.begin(115200);
  //--------------------------------------------------------------------
  pinMode(relay1_pin, OUTPUT);
  pinMode(sensor, OUTPUT);
  //--------------------------------------------------------------------
  //During Starting all Relays should TURN OFF
  digitalWrite(relay1_pin, HIGH);
  digitalWrite(sensor, HIGH);
  //--------------------------------------------------------------------
  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
  //--------------------------------------------------------------------
  //update button states 
  //Blynk.virtualWrite(button1_vpin, relay1_state);
 // Blynk.virtualWrite(button2_vpin, relay2_state);
 // Blynk.virtualWrite(button3_vpin, relay2_state);
 // Blynk.virtualWrite(button4_vpin, pinValue);
 // Blynk.virtualWrite(button5_vpin, relay1_state);
  //--------------------------------------------------------------------
}

void loop()
{
  Blynk.run(); 
  timer.run(); 
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!

  Blynk.virtualWrite(button1_vpin, relay1_state);
  Blynk.virtualWrite(button2_vpin, relay2_state);
  Blynk.virtualWrite(button3_vpin, relay2_state);
  Blynk.virtualWrite(button4_vpin, pinValue);
  Blynk.virtualWrite(button5_vpin, relay1_state);
  delay(1000);

}

I’d suggest that you check again!
GPIO25 is attached to ADC2
ADC2 pins cannot be used when Wi-Fi is used.

See here for more info…

and don’t miss the big blue box that says…

I’d suggest that you use a Segmented Switch for your mode selector, it makes life much easier.

Pete.

also, you have to read this
https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

Thank you for answer . Yes but when i bought my board it wasn’t ESP32 but nodeMCU-32s and pinout in this is a little bit different. I got this pinout image to help yourself recognize which pin is right .P13 is for relay and P25 is for water pump and on it is write to ADC18 and when I check it I could use this for I/O analog pins . but if I’m wrong then how I should choose right pin for my purpose ?

Have you bothered to read the link I provided?

Pete.

Yes, I read it, I just didn’t understand it at first.
I didn’t know that the esp32 needs 3.3 volts
to send a signal and not 5 volts like an arduino. So, thank you
For the hint.

I now have another question in mode 3 where I have
soil moisture sensor on the virtual button5_vpin (v5),
which is turned on by a switch on vpin (v6) , I am trying to
BLYNK_WRITE(button6_vpin) in a loop but not quite
as I read “How can I run BLYNK_WRITE in a loop?” unfortunately this does not
solve my problem or I don’t know how to write it correctly
I want that when I press the switch to activate mode 3 with the sensor
BLYNK_WRITE(button6_vpin) should be repeated and the water pump switched on
until the sensor value drops. To do this, I added
calling the soil_loop() function where I want the condition to be checked
in a loop but i don’t know if it would be best to use while
where I have read to avoid these loops in combination with blynk2.0, or the for
for but I don’t know how to do it properly

For any hints and help I will be very grateful, I am not
programmer and writing code is not my strong point
greetings and send a piece of code with which I have a problem

// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
// See the Device Info tab, or Template settings
#define BLYNK_TEMPLATE_ID "xxxx"
#define BLYNK_DEVICE_NAME "xxxx"
#define BLYNK_AUTH_TOKEN "xxxx"


// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

char auth[] = BLYNK_AUTH_TOKEN;

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxx";
char pass[] = "xxxx";

BlynkTimer timer; 

#define relay1_pin 16 // water pump
#define sensor 39 // soil moisture 

const int dry = 3450; //value when is dry
const int mid = 3000; // value to if statment
const int wet = 1400; //value when is wet

int relay1_state = 0; // state mode 1
int relay2_state = 0; // state mod3 2 auto
int relay3_state = 0; // state to soil moisture

int pinValue = 0; // state to gauge v4 
int sensorVal = 0; // state sensor

int timee = 0; // state for set time to mode  2 auto


//Change the virtual pins according the rooms
#define button1_vpin    V1 // 1st mode on/off
#define button2_vpin    V2 // auto 
#define button3_vpin    V3 // slider
#define button4_vpin    V4 // led to mode 1
#define button5_vpin    V5 // gauge moistyre
#define button6_vpin    V6 // on off sensor
//------------------------------------------------------------------------------
// This function is called every time the device is connected to the Blynk.Cloud
// Request the latest state from the server
BLYNK_CONNECTED() {
  Blynk.syncVirtual(button1_vpin);
  Blynk.syncVirtual(button2_vpin);
  Blynk.syncVirtual(button3_vpin);
  Blynk.syncVirtual(button4_vpin); 
  Blynk.syncVirtual(button5_vpin);
  Blynk.syncVirtual(button6_vpin);
}
//--------------------------------------------------------------------------
// This function is called every time the Virtual Pin state change
//i.e when web push switch from Blynk App or Web Dashboard


BLYNK_WRITE(button1_vpin)
  { 
  
    relay1_state = param.asInt(); 
    
//   Blynk.notify("You just watered your plant."); //later
if(relay1_state == HIGH) {// if switch 1 is on
  digitalWrite(relay1_pin, relay1_state); 
  Blynk.virtualWrite(button4_vpin, HIGH); 
  Serial.println("mode 1 on");
  delay(2000); 
  //------- 
  relay1_state = LOW;
  digitalWrite(relay1_pin, relay1_state); 
  Blynk.virtualWrite(button1_vpin, LOW); 
  Blynk.virtualWrite(button4_vpin, LOW); 
  Serial.println("mode 1 off");
  delay(500);
  
  }

  else{ 
  relay1_state = LOW;
  digitalWrite(relay1_pin, relay1_state);
  Serial.println("mode 1 off");
  delay(100); 
    }
  }
//--------------------------------------------------------------------------
BLYNK_WRITE(button2_vpin) {  

relay2_state = param.asInt();
if(relay2_state == HIGH) {
  Serial.println("mode auto on - set a time ");
}
else{
  Serial.println(" mode auto off ");
  digitalWrite(relay1_pin, relay2_state);
 }
}
//--------------------------------------------------------------------------
BLYNK_WRITE(button3_vpin)
{ 
  
  if(relay2_state == HIGH) {
  pinValue = param.asInt(); // assigning incoming value from pin V to a variable

 
  Serial.print("water will on for ");
  Serial.print(pinValue);
  Serial.println(" days ");
  
  timee = (pinValue*1000); // for tests days = seconds - is faster
  Serial.println(timee);
  delay(timee);
  Serial.println(" test");
  digitalWrite(relay1_pin, HIGH); //pinValue
  delay(2000); 
  digitalWrite(relay1_pin, LOW);
  
  Blynk.virtualWrite(button2_vpin, LOW); 
  delay(1000);
  Blynk.virtualWrite(button3_vpin, LOW); 
    }
}

//--------------------------------------------------------------------------

BLYNK_WRITE(button6_vpin) {   // on off soil moisture // https://docs.blynk.io/en/blynk.edgent/api/blynk-timer
  relay3_state = param.asInt();
  
  if(relay3_state == HIGH) { 
  Serial.println(" mode with sensor on ");
  delay(500); 
  //------------------------------------------------------------ 

    if (sensorVal >= mid){
      // sensorVal = param.asInt();
      soil_loop();// call loop function - to check statment 
        }
    
    else{
        digitalWrite(relay1_pin, LOW); 

  Serial.print("water is enough -  ");
  Serial.println(sensorVal);
  delay(1500);
  Blynk.virtualWrite(button6_vpin, LOW);
    }
      }
 
   //------------------------------------------------------------- koniec 
    if(relay3_state == LOW){
    Serial.println(" mode with senosor off ");
  }
}

//--------------------------------------------------------------------------

void soil_loop()  // this part is realy wrong and I don't know how to write loop to ...
{
   
        Serial.println("check call soil_loop ");
        Serial.print("sensor value -  ");
        Serial.println(sensorVal);
        Blynk.syncVirtual(V5); //   I need this function ??
        //Serial.println("sync v5 ");
        delay(1000);
        Serial.println(sensorVal);
        delay(1000);
        Serial.println(sensorVal);
        


        
        //while (sensorVal >= mid)
       // {
       // digitalWrite(relay1_pin, HIGH);
        //Serial.print("watering  - ");
        //Serial.println(sensorVal);
 
       // }
 
}

//--------------------------------------------------------------------------

void myTimerEvent() 
{
  sensorVal = analogRead(sensor);
  // You can send any value at any time.
  // Please don't send more that 10 values per second.

  Blynk.virtualWrite(button5_vpin, sensorVal); 
 
}

//--------------------------------------------------------------------------
void setup()
{
  // Debug console
  Serial.begin(115200);
  //--------------------------------------------------------------------
  pinMode(relay1_pin, OUTPUT);
  pinMode(sensor, OUTPUT);
  //--------------------------------------------------------------------
  //During Starting all Relays should TURN OFF
  digitalWrite(relay1_pin, HIGH);
  digitalWrite(sensor, HIGH);
  //--------------------------------------------------------------------
  //https://github.com/blynkkk/blynk-library/blob/master/examples/GettingStarted/PushData/PushData.ino#L30
  // Setup a function to be called every second
  timer.setInterval(1250L, myTimerEvent);

  //timer.setInterval(timee, soil_loop);

   //--------------------------------------------------------------------
  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
  //--------------------------------------------------------------------
  //update button states 
  Blynk.virtualWrite(button1_vpin, relay1_state);
  Blynk.virtualWrite(button2_vpin, relay2_state);
  Blynk.virtualWrite(button3_vpin, relay2_state);
  Blynk.virtualWrite(button4_vpin, pinValue);
  Blynk.virtualWrite(button5_vpin, relay1_state);
  //--------------------------------------------------------------------
}

void loop()
{
  Blynk.run(); 
  timer.run(); 
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
  
  }

First,

GPIO 34
GPIO 35
GPIO 36
GPIO 39
are input only pins

Second, you can read the soil moisture sensor use this example

void sendSensor()
{

  sensorData = analogRead(A0); //reading the sensor on A0

  if ( isnan(sensorData) ){
    // Serial.println("Failed to read from Hygrometer Soil Moisture sensor!");
    return;
  } else {
    // Serial.println(sensorData);
    // When the plant is watered well the sensor will read a value 380~400, I will keep the 400 
    // value but if you want you can change it below. 
  
    sensorData = constrain(sensorData,400,1023);  //Keep the ranges!
    output = map(sensorData,400,1023,100,0);  //Map value : 400 will be 100 and 1023 will be 0
    // Serial.println(output);

    Blynk.virtualWrite(V0, output);
  
  }
}

or just search the forum, there’s a lots of examples how to use the soil moisture sensor

BLYNK_WRITE(vPin) functions trigger once, when the value of the vPin changes on the Blynk server.
The sensible thing to do is to have a very concise BLYNK_WRITE(button6_vpin) function which simply sets a variable to true or false (1 or 0 if you prefer). This should be a GLOBAL variable (not declared within the BLYNK_WRITE(button6_vpin) function.
You then have a function that is called using a timer, which checks the state of this global variable and performs the appropriate actions depending on whether the variable is true or false.

But, before you do that, you need to get rid of these blocking delays…

Pete.

1 Like

Hi, I just fixed the code in mode 3 and mode 3 works very well.
I wanted to change mode 1 so that its code would not contain delay() function too
but when I have mode 1 enabled in " timer.setInterval "
mode 3 is not working properly (although I checked it before and it worked correctly). I assume that it is because two loops
are working at the same time

 timer.setInterval(1250L, myTimerEvent);
  timer.setInterval(2250L, myTimerEvent2);
  timer.setInterval(100L, modeone);

I want to apply the conditions:
that when one mode is running the other two
are switched off. I just don’t know how to stop the loop which is checked
all the time in setup(). I was thinking to use the Return function; but I don’t really know how to write it properly.
I would really appreciate a hint
or some example with similar problem.
greetings

// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
// See the Device Info tab, or Template settings
#define BLYNK_TEMPLATE_ID "xxxx"
#define BLYNK_DEVICE_NAME "xxxx"
#define BLYNK_AUTH_TOKEN "xxxx"


// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

char auth[] = BLYNK_AUTH_TOKEN;

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxx";
char pass[] = "xxxx";

BlynkTimer timer; 

#define relay1_pin 16 // water pump
#define sensor 39 // soil moisture input for analog

const int dry = 3450; //value when is dry
const int mid = 3000; // value to if statment
const int wet = 1400; //value when is wet

int relay2_state = 0; // state mod3 2 auto
// bool 
bool relay1_state = 0; // state mode 1
bool relay3_state = 0; // state mode 3

int sensorData = 0; // for % but not now 
int output = 0;

int pinValue = 0; // state to gauge v4 
int sensorVal = 0; // state sensor

int timee = 0; // state for set time to mode  2 auto


//Change the virtual pins according the rooms
#define button1_vpin    V1 // 1st mode on/off
#define button2_vpin    V2 // auto 
#define button3_vpin    V3 // slider
#define button4_vpin    V4 // led to mode 1
#define button5_vpin    V5 // gauge moistyre
#define button6_vpin    V6 // on off sensor
//------------------------------------------------------------------------------
// This function is called every time the device is connected to the Blynk.Cloud
// Request the latest state from the server
BLYNK_CONNECTED() {
  Blynk.syncVirtual(button1_vpin);
  Blynk.syncVirtual(button2_vpin);
  Blynk.syncVirtual(button3_vpin);
  Blynk.syncVirtual(button4_vpin); 
  Blynk.syncVirtual(button5_vpin);
  Blynk.syncVirtual(button6_vpin);
}
//--------------------------------------------------------------------------
// This function is called every time the Virtual Pin state change
//i.e when web push switch from Blynk App or Web Dashboard


//--------------------------------------------------------------------------
//############## code for mode 1 ###########################################

BLYNK_WRITE(button1_vpin) 
  { 
    relay1_state = param.asInt(); // bool


    if(relay1_state == 1){

// statment for turn off another modes
     // Blynk.virtualWrite(button6_vpin, LOW); //
	 
    }
// acually I don't know how to block another modes when one of them working


    if(relay1_state == 0)
    {
      Serial.println("test ");
    }
    
  }
//--------------------------------------------------------------------------
void modeone() 
{
      if(relay1_state == 1) {
        digitalWrite(relay1_pin, relay1_state); 
        Blynk.virtualWrite(button4_vpin, HIGH); 
        Serial.println("mode  1 on");
  }
  else{
    digitalWrite(relay1_pin, relay1_state); //  relay1_state or LOW
  Blynk.virtualWrite(button4_vpin, LOW); // led off
  Serial.println("mode  1 off");
}
}
//############## code for mode 1 ###########################################
//--------------------------------------------------------------------------
BLYNK_WRITE(button2_vpin) {  

relay2_state = param.asInt();
if(relay2_state == HIGH) {
  Serial.println("mode auto on - set a time ");
}
else{
  Serial.println(" mode auto off ");
  digitalWrite(relay1_pin, relay2_state);
 }
}
//--------------------------------------------------------------------------
BLYNK_WRITE(button3_vpin)
{ 
  
  if(relay2_state == HIGH) {
  pinValue = param.asInt(); // assigning incoming value from pin V to a variable

 
  Serial.print("water will on for ");
  Serial.print(pinValue);
  Serial.println(" days ");
  
  timee = (pinValue*1000); // for tests days = seconds - is faster
  Serial.println(timee);
  delay(timee);
  Serial.println(" test");
  digitalWrite(relay1_pin, HIGH); //pinValue
  delay(2000); 
  digitalWrite(relay1_pin, LOW);
  
  Blynk.virtualWrite(button2_vpin, LOW); 
  delay(1000);
  Blynk.virtualWrite(button3_vpin, LOW); 
    }
}

//--------------------------------------------------------------------------

//#### code for mode 3##################################################### code for mode 3

BLYNK_WRITE(button6_vpin) {   // on of soil moisture // https://docs.blynk.io/en/blynk.edgent/api/blynk-timer
  
  relay3_state = param.asInt(); // bool

  if(relay3_state != 1){
    Serial.println(" mode 3 off ");   
   digitalWrite(relay1_pin, LOW);
    }
  }
//--------------------------------------------------------------------------
void myTimerEvent2() 
{
      if(relay3_state == 1) 
      {
           if(sensorVal >= mid)
       {        
             sendSensor();// call function - pump on
       }
     else{
            sendSensor2(); // call function - pump off
      }
  }
  if(relay3_state == 0){
    Serial.println("test3 ");
  }
}
//--------------------------------------------------------------------------

void sendSensor() // pump on
{
  sensorVal = analogRead(sensor); //reading the sensor on 39

  if ( isnan(sensorVal) ){
     Serial.println("Failed to read from Hygrometer Soil Moisture sensor!"); // security
    return;
  } else {
    Serial.print("sensor lvl value - ");
     Serial.println(sensorVal);
    
       
        digitalWrite(relay1_pin, HIGH);
  }
}
//--------------------------------------------------------------------------

void sendSensor2(){ // pump off

  sensorVal = analogRead(sensor); //reading the sensor on 39

  if ( isnan(sensorVal) ){
     Serial.println("Failed to read from Hygrometer Soil Moisture sensor!"); // security
    return;
  } else {
    Serial.print("sensor lvl value - ");
     Serial.println(sensorVal);
       
        digitalWrite(relay1_pin, LOW);
        switch6off();
  }
}
//--------------------------------------------------------------------------

void switch6off(){ 
  if(sensorVal >= mid){
     Blynk.virtualWrite(button6_vpin, LOW); // switch mode 3 off
  }
  else{
     Blynk.virtualWrite(button6_vpin, HIGH);  // switch mode 3 on
  }
}
//--------------------------------------------------------------------------

void myTimerEvent() 
{
  sensorVal = analogRead(sensor); 
   
  Blynk.virtualWrite(button5_vpin, sensorVal); 
 

} 
//#### code for mode 3##################################################### code for mode 3
//--------------------------------------------------------------------------
void setup()
{
  // Debug console
  Serial.begin(115200);
  //--------------------------------------------------------------------
  pinMode(relay1_pin, OUTPUT);
  pinMode(sensor, OUTPUT);
  //--------------------------------------------------------------------
  //During Starting all Relays should TURN OFF
  digitalWrite(relay1_pin, HIGH);
  digitalWrite(sensor, HIGH);
  //--------------------------------------------------------------------
  //https://github.com/blynkkk/blynk-library/blob/master/examples/GettingStarted/PushData/PushData.ino#L30
  // Setup a function to be called every second
  timer.setInterval(1250L, myTimerEvent);
  timer.setInterval(2250L, myTimerEvent2);
  timer.setInterval(100L, modeone);// mode 1
   //--------------------------------------------------------------------
  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
  //--------------------------------------------------------------------
  //update button states 
  Blynk.virtualWrite(button1_vpin, relay1_state);
  Blynk.virtualWrite(button2_vpin, relay2_state);
  Blynk.virtualWrite(button3_vpin, relay2_state);
  Blynk.virtualWrite(button4_vpin, pinValue);
  Blynk.virtualWrite(button5_vpin, relay1_state);
  //--------------------------------------------------------------------
}

void loop()
{
  Blynk.run(); 
  timer.run(); 
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
  
  }

Personally, I wouldn’t “turn off” (Enable/Disable or Toggle) a timer, it isn’t worth the effort.

Far better to use a flag which is checked at the beginning of each function that is called by a timer, as described in the “Overriding a Timer (auto/manual mode)” section of this topic…

If you do choose to go down the Enable/Disable or Toggle route then remember that you’ll need to create a dummy “sacrificial” timer in slot 0 to avoid issues with the SimpleTimer bug.

However, I don’t think I’d use either of these approaches.
Instead I’d have a single timer which calls a function that checks the current “mode” value, then calls the appropriate function from there.

Also, your variable names are very messy. I’d shop doing this:

because the relay1_state name is very confusing. It’d not the state of Relay 1 until you update the relay1 GPIO to the appropriate status. At this stage it’s simply the mode1_button state, and that’s what you should call it.
If you need another variable to track the current state of relay1 then create one, but don’t re-use variables in the way you are.

The pseudo code would look like this…

BLYNK_WRITE(button1_vpin) 
{ 
  mode1_button = param.asInt(); 
  Blynk.virtualWrite(button2_vpin,0); // Turn Button 2 off
  Blynk.virtualWrite(button3_vpin,0); // Turn Button 3 off
  // Update the other button variables to avoid confusion...
  mode2_button = 0; 
  mode3_button = 0; 
}
my_timed_loop
{
  if(mode1_button==1)
  {
   \\ call the function that handles Mode 1
  }

  if(mode2_button==1)
  {
   \\ call the function that handles Mode 2
  }

  if(mode1_button==3)
  {
   \\ call the function that handles Mode 3
  }
}

Pete.

1 Like