0–10sec time lag to turn on LED: ESP8622

Hi all,

I am a very beginner and now trying to control LED using Blynk LoT in my Android smartphone and an ESP8600 connected to my mobile Wifi router. After struggling I somehow succeeded to turn the LED on, but the response is slow. The time lag seems to be random, and between 0–10sec, so I suspect it is an issue of the communication interval between Blynk and ESP8600. Please give me some solutions or suggestions. My code is as following:

const int rellay_output = 12;
BlynkTimer timer;
int input;
int safety;

BLYNK_WRITE(V0)
{
  if(safety==1)
  {
    Serial.println("Signal Received!");
    digitalWrite(rellay_output, HIGH);
    timer.setTimeout(500L, switch_off);
  }else
  {
    Serial.println("Safety is ON!");
  }
}

BLYNK_WRITE(V1)
{
  safety = param.asInt();
}

void switch_off()
{
  digitalWrite(rellay_output, LOW);
}

void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  pinMode(rellay_output, OUTPUT);
}

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

Thank you very much for paying your attention!

@Shin-Ichiro_Nakayama 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.

Sorry for bothering you, is it okay now?

Yes, its fine now thanks.

You’ve omitted some of your code, and it would be helpful to dee the entire sketch (with your Blynk and WiFi credentials redacted)

It would help if you explained a little more about what you are trying to achieve. What widgets are attached to V0 and V1, how are they configured, and what is their intended functionality?

Pete.

Thank you for your quick reply. My entire code is like this:

#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "xxx"
#define BLYNK_DEVICE_NAME "xxx"
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "xxx";
char ssid[] = "xxx";
char pass[] = "xxx";
const int rellay_output = 12;
BlynkTimer timer;
int input;
int safety;

BLYNK_WRITE(V0)
{
  if(safety==1)
  {
    Serial.println("Signal Received!");
    digitalWrite(rellay_output, HIGH);
    timer.setTimeout(500L, switch_off);
  }else
  {
    Serial.println("Safety is ON!");
  }
}

BLYNK_WRITE(V1)
{
  safety = param.asInt();
}

void switch_off()
{
  digitalWrite(rellay_output, LOW);
}

void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  pinMode(rellay_output, OUTPUT);
}

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

Here, what I want to make is:
The LED turns on for 0.5sec and the turns off only when the safety is unlocked and the trigger is pushed.
A button (A) with Switch control data stream, push mode is assigned to V0 virtual pin. This button is the trigger.
A button (B) with Switch value data stream, switch mode is assigned to V1 virtual pin. This button is the safety.
In addition, the massage “Signal Received!” comes also with lags, in a same pattern to the LED.
Thank you.

I don’t understand this.
How can it turn on (then presumably off again) for 0.5 seconds, but then be turned off by other events?

Pete.

Sorry, there were a typo and unclear explanations.
The LED turns on for 0.5sec and theN turns off. These actions happen only when the safety is unlocked and the trigger is pushed.

Once the LED turns on, it automatically turns off after 0.5sec by the timer.setTimeout(500L, switch_off).

Okay, that makes sense now.

The two issues that I can see is that I can see are:

  1. The BLYNK_WRITE(V0) (Trigger) function has no test to see if the incoming value (param.asInt()) is a 1 or a 0. This means that the routine will trigger on both a press and release if the button widget attached to pin V0

  2. Which is related to the first issue - The timeout timer that you are using is non-blocking, which is good from a Blynk point of view, but could lead to issues if the trigger is actuated whilst the timer is executing.
    One solution to this is to add a flag to the timer routine, which is set when the timer begins and cleared when the timer is completed. You could then check that this flag is not set (no timer currently executing) before re-triggering the next timer.

I’m not sure that these issues add-up to a 10 second lack of responsiveness though, even if the trigger button is pressed and released repeatedly in a short space of time.

When your device connects to the Blynk server, what sort of ping time is it showing?
And, is this consistent if you repeatedly reboot the device and monitor the ping results?

Ideally, you should be seeing pings of below 100ms. If I try this I get…

[4349] Ready (ping: 11ms).
[4351] Ready (ping: 11ms).
[4365] Ready (ping: 17ms).
[4354] Ready (ping: 13ms).
[4368] Ready (ping: 14ms).
[4356] Ready (ping: 18ms).
[4354] Ready (ping: 12ms).

which are all nice and low, and consistent.

Pete.

I’m embarrassed that I didn’t know the BLYNK_WRITE() function works also when the button is released. and,

Can this be solved by simply inserting delay() in the switch_off() function? I read that delay() is usually not recommended in this kind of coding, but in this case it seems to be suitable??? Anyway, I rewrite my code as follows:

#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "xxxxxxxx"
#define BLYNK_DEVICE_NAME "xxxxxxxx"
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "xxxxxxxx";
char ssid[] = "xxxxxxx";
char pass[] = "xxxxxxxxx";
const int rellay_output = 12;
BlynkTimer timer;
int input;
int safety;
int trigger;

BLYNK_WRITE(V0)
{
  trigger = param.asInt();
  if(safety==1 && trigger==1)
  {
    Serial.println("Signal Received!");
    digitalWrite(rellay_output, HIGH);
    timer.setTimeout(500L, switch_off);
  }else if(safety==0 && trigger==1)
  {
    Serial.println("Safety is ON!");
  }
}

BLYNK_WRITE(V1)
{
  safety = param.asInt();
  if(safety==1)
  {
    Serial.println("Safety is released!");
  }else
  {
    Serial.println("Safety is locked!");
  }
}

void switch_off()
{
  digitalWrite(rellay_output, LOW);
  //timer.setTimeout(2000L, null_func);
  delay(2000);
}

void null_func()
{
  
}

void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  pinMode(rellay_output, OUTPUT);
}

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

Now it works as intended, except the lag.

And the last point… How can I check the ping speed?
It appears on the serial monitor every time I push the reset button of ESP8622. For now, I have observed it five times and the values were 101, 117, 102, 152, and 97ms. It is obviously larger than yours. Are there any solutions for this?

You should t be using delays in your code.

The ping times are really a function of your physical distance from the server where your project lives, your internet connection latency and your ISP’s infrastructure.

It seems that your project most likely lives on the Singapore server, and according to this post there are currently some issues with that server…

I guess this may be contributing to the issue.

Pete.

OK, so I try to fix it as you suggested before.

Thank you for information. Actually I live in Japan, near SGP, does my place belong to SGP server?

Shin.

It tells you in the bottom right hand corner of your web dashboard, or in the “About” section of the app.

Pete.

I have confirmed mine is connecting to SGP server. I don’t know if the server trouble has been fixed, but the delay still occurs.
Anyway, thank you very much for your great advise! That helped me so much!

I think you need to start from the simplest sketch possible, with a single VIRTUAL_WRITE method and single digitalWrite. So you can make sure the problem is in the network or in your code. When you narrow down this main question we can move in the next direction.

Thank you Dmitriy for your advice, and accordingly I have tested. As a result, it occurred again. Then I changed my mobile router to a stationary (means not a “mobile” one, sorry for my poor english) one and it reacted immediately. So, the problem seems to be the connection between ESP8622 and the router or the router and server.
Any reason that mobile router leads to the delay?

I think that’s something you’d need to discuss with our mobile ISP.

Pete.

You need to start with “ping” command + “traceroute” command. With one and another router. After that we’ll know better where to dig.