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

MKR 1010 frequent disconnects from server -> best practices used

ios
arduino

#1

Hi everybody,

after extensive search in the forums I still couldn’t find a fix for my issue. Most disconnection issues seems to be around the ESP.

Hardware used:

  • Arduino MKR1010 over wifi
  • IOS App
  • Blynk server
  • Library version 0.6.1

The issue is that I’m having frequent disconnects from server. Sometimes the connection holds up for 10 Minutes, sometimes for 1 hour but never longer.
It is always able to recover, but it takes time. I used a watchdog timer in my loop set to 16 seconds and it tripped it regularly-> something keeps the code stuck for this amount of time.

As I’m planning to use it for monitoring purposes, a 16+ seconds blocking function is of course a problem. I want the device to work even when there is no internet connection without blocking for a considerable amount of time when Blynk gets stuck.

That is why I manage the wifi-connection separately from the Blynk-Connection.
What I tried so far:

  • Stripped back the code to the bare minimum for testing purposes
  • used a “clean” loop
  • Tried different wifi-networks
  • monitored wifi connection ->keeps connection, should be OK
  • Tried to power it by USB and externally/battery.
  • Tried increasing BLYNK_TIMEOUT_MS and drecreasing HEARTBEAT

It changed the hiccups slightly but I could never get more than 2 hours of continuous connection out of it.

Any help would be very much appreciated.

Thank you!

#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>
BlynkTimer timer;

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

int status = WL_CONNECTED;

void wifiConnect() {
  if (WiFi.status() != status) {
    WiFi.end();
    WiFi.begin(ssid, pass);
  }
}

void BlynkConnect() {
  
  if (WiFi.status() != status) {
    wifiConnect();
  }
  if (Blynk.connect()) {
    digitalWrite(LED_BUILTIN, HIGH);
  }
  else {
    digitalWrite(LED_BUILTIN, LOW);
  }
}


BLYNK_CONNECTED() {
  Blynk.syncAll();
}

void ArduinoPower() {
  int pwr = 10;
  Blynk.virtualWrite(V10, pwr);
}


void setup(void)
{

  pinMode(LED_BUILTIN, OUTPUT);

  Blynk.config(auth);
  //Try to connect to Blynk
  BlynkConnect();

  //Set timer
  timer.setInterval(1000L, ArduinoPower);

  //Notify
  Blynk.notify("restart!");

}

void loop() {
  unsigned long start = millis();

  Blynk.run();
  timer.run();

  if (millis() - start > 3000) {
    Blynk.notify("Long delay " + String(millis() - start));
  }

}


#2

That’s not what I’d call a clean void loop.

Pete.


#3

Added the time-tracking later. Same problem occurs with nothing in the loop but blynk.run() and timer.run()…


#4

Here is the debug when disconnect happens:

[1554650] <[14|06|9A|00|0C]vw[00]10[00]10.230
[1555651] <[14|06|9B|00|0C]vw[00]10[00]10.230
[1556051] <[06|06|9C|00|00]
[1579167] Connecting to blynk-cloud.com:80
[1579208] <[1D|00|01|00 …authentication
[1579244] >[00|00|01|00|C8]
[1579246] Ready (ping: 34ms).
[1579246] Free RAM: 26715

It sends out heartbeat. After that it waits for 20 seconds, doesn’t get a response and reconnects to server. The reconnect works immediately and every time.
It happens at random times between a couple of minutes and 1 hour…

Can somebody give me a hint where the problem could lie?


#5

I did some further investigation on the problem. The disconnects always happen after Blynk pings the server, never when actually sending or receiving data.

I used the Ping-example of the WIFININA library and pinged a server at 1 sec intervals. It ran for a day without problems.
I feel confident, that I do not have any connectivity issues.

When looking at the debug print of Blynk it looks like Blynk is pinging the server in intervals set by the heartbeat. As standard, it should be set to 10 seconds.

I looked it up in the code. The fragment should be the following within BlynkProtocol.h for the function run():

if (t - lastActivityIn > (1000UL * BLYNK_HEARTBEAT + BLYNK_TIMEOUT_MS*3)) {
#ifdef BLYNK_DEBUG
            BLYNK_LOG6(BLYNK_F("Heartbeat timeout: "), t, BLYNK_F(", "), lastActivityIn, BLYNK_F(", "), lastHeartbeat);
#else
            BLYNK_LOG1(BLYNK_F("Heartbeat timeout"));
#endif
            internalReconnect();
            return false;
        } else if ((t - lastActivityIn  > 1000UL * BLYNK_HEARTBEAT ||
                    t - lastActivityOut > 1000UL * BLYNK_HEARTBEAT) &&
                    t - lastHeartbeat   > BLYNK_TIMEOUT_MS)
        {
            // Send ping if we didn't either send or receive something
            // for BLYNK_HEARTBEAT seconds
            sendCmd(BLYNK_CMD_PING);
            lastHeartbeat = t;
        }

As far as I can tell, I would expect periodic pings only when there is no activity going in or out for heartbeat-seconds. I’m sending out data every 1 second. If I read the code correctly, it shouldn’t ping the server in this case.

I greatly increased the heartbeat timing for testing purpose (3600 seconds).
I didn’t get any disconnects ever since.
The connection is very stable for as long as I could test it yesterday (12+ hours) without any problems at all.

It could happen that a ping and the calling of a timer coincides almost exactly in some cases and causes the connection to crash.
Depending on the setup of the timer that could explain the “randomness” of the disconnects.

What do you guys think? Am I on to something?

Thank you.