BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Lag response using the blynk app as well as the physical buttons

HI to all developers

I’m a Beginner in blynk world

I have this code which I tested using the blynk app as well as the physical button on my ESP32 development board and there was a lag or slaw response in turning on and off the relays, please help me if you can by revising such code and to by pass any delays or lag issues

Regards

  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************

  You’ll need:
   - Blynk App (download from AppStore or Google Play)
   - ESP32 board
   - Decide how to connect to Blynk
     (USB, Ethernet, Wi-Fi, Bluetooth, ...)

  There is a bunch of great example sketches included to show you how to get
  started. Think of them as LEGO bricks  and combine them as you wish.
  For example, take the Ethernet Shield sketch and combine it with the
  Servo example, or choose a USB sketch and add a code from SendData
  example.
 *************************************************************/

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


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include "settings.h"           
#include "secret.h" 
BlynkTimer timer;
#define MQ2 34

void checkPhysicalButton();
int relay1State = HIGH;
int pushButton1State = HIGH;

int relay2State =HIGH;
int pushButton2State = HIGH;
boolean state = false;
int sensorValue = 0;


// Every time we connect to the cloud...
// Every time we connect to the cloud...

BLYNK_CONNECTED() {

  // Request the latest state from the server

  Blynk.syncVirtual(VPIN_BUTTON_1);
  Blynk.syncVirtual(VPIN_BUTTON_2);


  // Alternatively, you could override server state using:
  //Blynk.virtualWrite(VPIN_BUTTON_1, relay1State);
  //Blynk.virtualWrite(VPIN_BUTTON_2, relay2State);
  //Blynk.virtualWrite(VPIN_BUTTON_3, relay3State);
  //Blynk.virtualWrite(VPIN_BUTTON_4, relay4State);

}

// When App button is pushed - switch the state

BLYNK_WRITE(VPIN_BUTTON_1) {
  relay1State = param.asInt();
  digitalWrite(RELAY_PIN_1, relay1State);
  digitalWrite(RELAY_PIN_2, relay1State);
  delay(2000);
  digitalWrite(RELAY_PIN_2, HIGH);
 
}

void checkPhysicalButton()
{
  if (digitalRead(PUSH_BUTTON_1) == LOW) {
    // pushButton1State is used to avoid sequential toggles
    if (pushButton1State != LOW) {

      // Toggle Relay state
      relay1State = !relay1State;
      digitalWrite(RELAY_PIN_1, relay1State);
      digitalWrite(RELAY_PIN_2, relay1State);

      // Update Button Widget
      Blynk.virtualWrite(VPIN_BUTTON_1, relay1State);
      Blynk.virtualWrite(VPIN_BUTTON_2, relay2State);
      delay(2000);
      digitalWrite(RELAY_PIN_2, HIGH);


    }
    pushButton1State = LOW;
  } else {
    pushButton1State = HIGH;
  }

  }

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "---------";

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

void setup()
{
  pinMode(RELAY_PIN_1, OUTPUT);
  pinMode(PUSH_BUTTON_1, INPUT_PULLUP);
  digitalWrite(RELAY_PIN_1, relay1State);
  pinMode(RELAY_PIN_2, OUTPUT);
  digitalWrite(RELAY_PIN_2, relay1State);
  pinMode(MQ2, INPUT);

  // Setup a function to be called every 100 ms
  timer.setInterval(500L, checkPhysicalButton);
  timer.setInterval(1000L, sendUptime);

  // Debug console
  Serial.begin(115200);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
}
void sendUptime()
{
  
  sensorValue = analogRead(MQ2);
  Blynk.virtualWrite(V1, sensorValue);
  Serial.println(sensorValue);

  if (sensorValue > 1000)
  {
    Blynk.notify("Gas Detected!");
    relay1State = HIGH;
    digitalWrite(RELAY_PIN_1, relay1State);
    Blynk.virtualWrite(VPIN_BUTTON_1, relay1State);

    }
}
void loop()
{
  Blynk.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!
  timer.run();


}

void loop()

The delay(2000) commands in your code are blocking commands - all code execution stops at that point until the 2 second delay has completed.

With your BLYNK_WRITE callback functions this happens regardless of whether it was an on or an off event from the button widget.

You need to replace these with BlynkTimer timeout timers, preferably as lambda functions.
A bit of searching g of the forum will explain exactly how to do this.

Pete.

Thank you for your help, please can you rewrite the code based on your solution?

I’m afraid I don’t have time to do that. As I suggested, if you do a search you’ll find the info you need to do it yourself.

Pete.

HI again
this is how I improve the code after a lot of searching

/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************

  You’ll need:
   - Blynk App (download from AppStore or Google Play)
   - ESP32 board
   - Decide how to connect to Blynk
     (USB, Ethernet, Wi-Fi, Bluetooth, ...)

  There is a bunch of great example sketches included to show you how to get
  started. Think of them as LEGO bricks  and combine them as you wish.
  For example, take the Ethernet Shield sketch and combine it with the
  Servo example, or choose a USB sketch and add a code from SendData
  example.
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
BlynkTimer timer;
#define MQ2 34
#define RELAY_PIN_1      12
#define RELAY_PIN_2      13
#define PUSH_BUTTON_1    19 
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "LZn00S06P6uCHmk1BXA2uTzFlkz1KQf3";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "afif.khoury";
char pass[] = "jaad2013";
// Every time we connect to the cloud...
// Every time we connect to the cloud...
void checkPhysicalButton();
int relay1State = LOW;
int pushButton1State = HIGH;
int relay2State =LOW;
int pushButton2State = HIGH;
boolean state = false;
int sensorValue = 0;
int fire_sensor = 4;    // used for ESP32

BLYNK_CONNECTED() {

  // Request the latest state from the server

  //Blynk.syncVirtual(V12);
  //Blynk.syncVirtual(V13);


  // Alternatively, you could override server state using:
  Blynk.virtualWrite(V12, relay1State);
  Blynk.virtualWrite(V13, relay2State);
  //Blynk.virtualWrite(VPIN_BUTTON_3, relay3State);
  //Blynk.virtualWrite(VPIN_BUTTON_4, relay4State);

}

// When App button is pushed - switch the state

BLYNK_WRITE(V12) {
  relay1State = param.asInt();
  digitalWrite(RELAY_PIN_1, relay1State);
  digitalWrite(RELAY_PIN_2, relay1State);
  //delay(500);
  //digitalWrite(RELAY_PIN_2, LOW);
}
void checkPhysicalButton()
{
  if (digitalRead(PUSH_BUTTON_1) == LOW) {
    // pushButton1State is used to avoid sequential toggles
    if (pushButton1State != LOW) {

      // Toggle Relay state
      relay1State = !relay1State;
      digitalWrite(RELAY_PIN_1, relay1State);
      digitalWrite(RELAY_PIN_2, relay1State);

      // Update Button Widget
      Blynk.virtualWrite(V12, relay1State);
      Blynk.virtualWrite(V13, relay2State);
      //delay(500);
      //digitalWrite(RELAY_PIN_2, LOW);
    }
    pushButton1State = LOW;
  } else {
    pushButton1State = HIGH;
  }
}
void setup()
{
  // Debug console
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
  pinMode(RELAY_PIN_1, OUTPUT);
  pinMode(PUSH_BUTTON_1, INPUT_PULLUP);
  digitalWrite(RELAY_PIN_1, relay1State);
  pinMode(RELAY_PIN_2, OUTPUT);
  digitalWrite(RELAY_PIN_2, relay1State);
  pinMode(MQ2, INPUT);
  pinMode(fire_sensor, INPUT);
  // Setup a function to be called every 100 ms
  timer.setInterval(10L, checkPhysicalButton);
  timer.setInterval(1000L, sendUptime);
  timer.setInterval(10L, fire);
  timer.setInterval(500L, RELAYPIN2);

}
void sendUptime()
{
  
  sensorValue = analogRead(MQ2);
  Blynk.virtualWrite(V1, sensorValue);
  Serial.println(sensorValue);

  if (sensorValue > 1000)
  {
    Blynk.notify("Gas Detected!");
    relay1State = LOW;
    digitalWrite(RELAY_PIN_1, relay1State);
    Blynk.virtualWrite(V12, relay1State);

    }
}
void fire()
{
int firesensor = digitalRead(fire_sensor);
Serial.print(firesensor);

if (firesensor == 0)
 {
    Blynk.notify("Fire!");
    relay1State = LOW;
    digitalWrite(RELAY_PIN_1, relay1State);
    Blynk.virtualWrite(V12, relay1State);

    }  
}
void RELAYPIN2()
{
if (digitalRead(RELAY_PIN_2) == HIGH)
{
digitalWrite(RELAY_PIN_2, HIGH);
timer.setTimeout(5000L, []() //5 secs on
{digitalWrite(RELAY_PIN_2, LOW);
  
});
}
}
void Blynk_Delay(int milli)
{
  int end_time = millis() + milli;
  while (millis() < end_time)
  {
    if (Blynk.connected())
    {
      Blynk.run();
    }
    yield();
  }
}
void loop()
{
  Blynk.run();
  timer.run();
}

@Afif_Khoury please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Pete.

So you didn’t go down the lambda timer route then.

Your current code has some major issues. Calling two separate timers at exactly the same time, 100 times per second, isn’t good practice and I guess that it will cause you issues in future - probably in the form of unwanted disconnections or lock-ups.

Also, there is no need to debounce switch contacts if you poll them with a timer, especially if the polling frequency is in the 50-100ms range.

Pete.

ok what about this small modification I remove one timer I found out that when I push the button again the other timer running with 1 second instead of 5 second
this is my code now

/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************

  You’ll need:
   - Blynk App (download from AppStore or Google Play)
   - ESP32 board
   - Decide how to connect to Blynk
     (USB, Ethernet, Wi-Fi, Bluetooth, ...)

  There is a bunch of great example sketches included to show you how to get
  started. Think of them as LEGO bricks  and combine them as you wish.
  For example, take the Ethernet Shield sketch and combine it with the
  Servo example, or choose a USB sketch and add a code from SendData
  example.
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
BlynkTimer timer;
#define MQ2 34
#define RELAY_PIN_1      12
#define RELAY_PIN_2      13
#define PUSH_BUTTON_1    19 
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "LZn00S06P6uCHmk1BXA2uTzFlkz1KQf3";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "afif.khoury";
char pass[] = "jaad2013";
// Every time we connect to the cloud...
// Every time we connect to the cloud...
void checkPhysicalButton();
int relay1State = LOW;
int pushButton1State = HIGH;
int relay2State =LOW;
int pushButton2State = HIGH;
boolean state = false;
int sensorValue = 0;
int fire_sensor = 4;    // used for ESP32

BLYNK_CONNECTED() {

  // Request the latest state from the server

  //Blynk.syncVirtual(V12);
  //Blynk.syncVirtual(V13);


  // Alternatively, you could override server state using:
  Blynk.virtualWrite(V12, relay1State);
  //Blynk.virtualWrite(V13, relay2State);
  //Blynk.virtualWrite(VPIN_BUTTON_3, relay3State);
  //Blynk.virtualWrite(VPIN_BUTTON_4, relay4State);

}
void RELAYPIN2()
{
if (digitalRead(RELAY_PIN_2) == HIGH)
{
//digitalWrite(RELAY_PIN_2, HIGH);
//timer.setTimeout(5000L, []() //5 secs on
digitalWrite(RELAY_PIN_2, LOW);
 
}
}
// When App button is pushed - switch the state

BLYNK_WRITE(V12) {
  relay1State = param.asInt();
  digitalWrite(RELAY_PIN_1, relay1State);
  digitalWrite(RELAY_PIN_2, relay1State);
  //delay(500);
  //digitalWrite(RELAY_PIN_2, LOW);
}
void checkPhysicalButton()
{
  if (digitalRead(PUSH_BUTTON_1) == LOW) {
    // pushButton1State is used to avoid sequential toggles
    if (pushButton1State != LOW) {

      // Toggle Relay state
      relay1State = !relay1State;
      digitalWrite(RELAY_PIN_1, relay1State);
      digitalWrite(RELAY_PIN_2, relay1State);

      // Update Button Widget
      Blynk.virtualWrite(V12, relay1State);
      Blynk.virtualWrite(V13, relay2State);
      //delay(500);
      //digitalWrite(RELAY_PIN_2, LOW);
    }
    pushButton1State = LOW;
  } else {
    pushButton1State = HIGH;
  }
}
void setup()
{
  // Debug console
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
  pinMode(RELAY_PIN_1, OUTPUT);
  pinMode(PUSH_BUTTON_1, INPUT_PULLUP);
  digitalWrite(RELAY_PIN_1, relay1State);
  pinMode(RELAY_PIN_2, OUTPUT);
  digitalWrite(RELAY_PIN_2, relay1State);
  pinMode(MQ2, INPUT);
  pinMode(fire_sensor, INPUT);
  // Setup a function to be called every 100 ms
  timer.setInterval(10L, checkPhysicalButton);
  timer.setInterval(1000L, sendUptime);
  timer.setInterval(10L, fire);
  timer.setInterval(5000L, RELAYPIN2);

}
void sendUptime()
{
  
  sensorValue = analogRead(MQ2);
  Blynk.virtualWrite(V1, sensorValue);
  Serial.println(sensorValue);

  if (sensorValue > 1000)
  {
    Blynk.notify("Gas Detected!");
    relay1State = LOW;
    digitalWrite(RELAY_PIN_1, relay1State);
    Blynk.virtualWrite(V12, relay1State);

    }
}
void fire()
{
int firesensor = digitalRead(fire_sensor);
Serial.print(firesensor);

if (firesensor == 0)
 {
    Blynk.notify("Fire!");
    relay1State = LOW;
    digitalWrite(RELAY_PIN_1, relay1State);
    Blynk.virtualWrite(V12, relay1State);

    }  
}
void Blynk_Delay(int milli)
{
  int end_time = millis() + milli;
  while (millis() < end_time)
  {
    if (Blynk.connected())
    {
      Blynk.run();
    }
    yield();
  }
}
void loop()
{
  Blynk.run();
  timer.run();
}

What about it. How does it address the issues that I highlighted?

Pete.