How to modify default timeout and avoid WiFi disconnect

Hi Blynkers,

I used Blynk app and Arduino panel to control my garage door. It works magically.
However, sometimes my app disconnect with a “Exclamation mark” on app interface.
It happens like every week. Don’t really have a regular interval.
I need to manually cut off my panel’s power, reset the panel, and reset app. So it can connect again.
I doubt if it is due to my house’s unstable WiFi. Also, Blynk set its Default timeout in 30 seconds.

Leaned from the manual

Blynk.connect()

This functions will continue trying to connect to Blynk server. Returns true when connected, false if timeout have been reached. Default timeout is 30 seconds.

That is, my app will crash if my WiFi is out for more than 30 sec.

I’m a newbie for coding, so I would like to have your help to

  1. Extend Default timeout to 5 mins?
  2. Create a loop to reconnect after timeout?

Not sure which way is easier/more efficient, cuz both are difficult for me…

Version -
Android 9 (2.20.709.2)
Blynk 2.27.21
NodeMCU Arduino 1.8.13

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "+++++++++++++++";
char ssid[] = "++++++++";
char pass[] = "+++++++++";

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

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

Thanks in advance for your time and kindly help!
&
Happy New Year!!

Sue

1 Like

Simply resetting or power-cycling your NodeMCU should be sufficient. I’m not sure what you mean by “reset the app”.

I think you’re misunderstanding the documentation, and anyway, you aren’t using Blynk.connect in your sketch.

Blynk.begin (which is what you are using) is a blocking process. This means that the code execution will not progress beyond that point unless the Blynk.begin command can successfully connect to both the WiFi and Blynk server.
Blynk.connect is an alternative solution which is non-blocking. It requires you to manually manage the WiFi connection in your code, then you call Blynk.config to establish the server/auth code settings and Blynk.connect to set-up that connection. It will try to connect for the default timeout period (30 seconds) then continue with the code execution if this is unsuccessful. Switching to Blynk.Coinfig/Connect and extending the timeout isn’t the solution to your problem.

If you search the forum for re-connection routines you’ll see how to automatically manage reconnections after a connection loss.

You may also want to add some code that sends the current WiFi signal strength (RSSI) to your app, which will allow you to understand how good the WiFi signal strength is. Obviously this data will only be updated when the NodeMCU is connected to the app, but it will provide valuable information about possible causes of the issue you are facing.

Pete.

Hi Pete,

Simply resetting or power-cycling your NodeMCU should be sufficient. I’m not sure what you mean by “reset the app”.

I did what I could do to troubleshooting.
(1) Reboot button on the relay.
(2) Power on/off the relay.
(3) Plug/Unplug micro-usb on the relay.
(4) Re-edited “D0” 1/0 on Blynk app.

For now, my fifth step. Very thanks for your clarification.

I replaced Blynk.begin with Blynk.connect in the sketch.
For WiFi re-connection, I referred to the link below and made some little change:
http://community.heltec.cn/t/solved-wifi-reconnect/1396/3

Here is my updated sketch for your reference.

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "++++++++++++++++++";
char ssid[] = "++++++++";
char pass[] = "++++++++";

void setup()
{
  Serial.begin(9600);
  delay(10);
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    }
  WiFi.setAutoReconnect(true);
  WiFi.persistent(true);
  delay(500); 
  Blynk.config(auth);
  Blynk.connectWiFi(ssid, pass);  
  pinMode(D0, OUTPUT);
}

void loop()
{
  delay(1000);
  if(WiFi.status() == WL_CONNECTED){
  Blynk.run();  
  }
  else{
      int retry_count = 0;
      const int retry_limit = 300;
      while (retry_count < retry_limit) {
        WiFi.reconnect();
        delay(2000);
        Blynk.connect();
        Blynk.run(); 
        if (Blynk.connected())
        break;
        delay(2000);
        retry_count += 1;
      }
      }
}

Since I’m still not sure the root cause of my problem, I will keep tracking if my garage’s door can work for the whole new year.

Sue

What relay is this?

It’s your NodeMCU that you need to be rebooting.

I’d still add-in the RSSI info, it will help you identify any problems with signal strength.

Pete.

The default Blynk connect timeout is 30s and it’s not enough for me. Where can I increase that value in BlynkEdgent arduino code pls?

I’m not sure what you mean by “is 30s and it’s not enough for me”.

Each time Blynk encounters a Blynk.run command it will attempt to do a Blynk.connect(), so in reality the Blynk.connect() command is redundant in most (if not all) situations. I still advocate the use of Blynk.connect(), as it makes the code more readable and understandable.

I suggest that you start a new “need help with my project” topic and provide all of the information that is requested when you create the topic, and explain exactly what issues you are facing.

Pete.

1 Like