Arduino MKR1010 frequent disconnects

Dear Blynk-Community,

I recently tried to utilize Blynk for my Arduino projects. My current setup:

  • Arduino MKR1010 Wifi (updated to latest WiFiNINA Firmware 1.4.8)
  • powered by 5V USB wall wart
  • Blynk v1.0.1.

In my project sketch I noticed some frequent disconnects (Blynk Heartbeat timeouts) on an irregular basis. First, I thought my connection management was maybe badly implemented or I had other issues, I often read about in this community (non clean main loop, too much coinciding timers, vPin flooding, you name it). But after trying to isolate the issue, I ended up with this minimal example sketch, with which I can reproduce it. As you can see, I simply measure the offline time and put in proportion to the overall run time of my arduino. Roughly 4 to 5 % of all time, the device is NOT connected to blynk servers, what looks like a bit too much for me.

Is this a known/ common issue with the Arduino MKR1010? I already read some posts about Arduino connection issues, but I didn’t found an satifying answer yet.

Any potential mesaures, I can take to reduce the average downtime? For my application round about 1% offtime would be acceptable, the lower the better.

Thank you very much for your support in advance!

#include "1_secret.h"

#include <SPI.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>

#define BLYNK_DEVICE_NAME "Offline Test"

char auth[] = BLYNK_AUTH_TOKEN;

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

BlynkTimer timer;
unsigned long ontime= 0;
unsigned long offtime= 0;


void setup()
{
  Blynk.begin(auth, ssid, pass);

  Blynk.virtualWrite(V0, 0);
  Blynk.virtualWrite(V1, 0);
  Blynk.virtualWrite(V2, 0);

  timer.setInterval(1000L, noticeUptime);
}

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

void noticeUptime() {
  if (Blynk.connected()) {
    ontime = millis();
    Blynk.virtualWrite(V0, ontime / 1000);
    Blynk.virtualWrite(V2, (float(offtime) / float(ontime)) * 100.);
  }
}

BLYNK_CONNECTED() {
  if (ontime) {
    unsigned long diff = millis() - ontime;
    offtime = offtime + diff;

    Blynk.virtualWrite(V1, offtime / 1000UL);
  }
}

Unbenannt

Hey there,

your sketch must contain #define BLYNK_TEMPLATE_ID “YourTemplateID”
When using blynk iot and it must be in the beginning of your sketch along with #define BLYNK_DEVICE_NAME “Offline Test”.

Thank you, but this is not related to my actual issue. Template ID together with my Wifi credentials are writtenin the included header file (secret.h).

Template ID should always be the first line of the main .ino file if you want the Blynk library to work properly.

Pete.

I didn’t say it’s the cause of your problem, but it’s a mistake and I have to tell you about it.

Well, I adjusted the code according to your comments. But apparently this didn’t cause the disconnects. Does any one have an explanation or advice for improvement to reduce disconnected times? Is this an usual behaviour when working with blynk (and arduino)? Because 5% offline quota seems a bit too much for me. It means a device is more than 1¼h offline every single day.

#define BLYNK_TEMPLATE_ID "xxxxxxx"
#define BLYNK_DEVICE_NAME "Offline Test"
#define BLYNK_AUTH_TOKEN "xxxxxxx";

#define WIFI_SSID "xxxxxxx"
#define WIFI_PASS "xxxxxxx"

#include <SPI.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>


char auth[] = BLYNK_AUTH_TOKEN;

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

BlynkTimer timer;
unsigned long ontime= 0;
unsigned long offtime= 0;


void setup()
{
  Blynk.begin(auth, ssid, pass);

  Blynk.virtualWrite(V0, 0);
  Blynk.virtualWrite(V1, 0);
  Blynk.virtualWrite(V2, 0);

  timer.setInterval(1000L, noticeUptime);
}

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

void noticeUptime() {
  if (Blynk.connected()) {
    ontime = millis();
    Blynk.virtualWrite(V0, ontime / 1000);
    Blynk.virtualWrite(V2, (float(offtime) / float(ontime)) * 100.);
  }
}

BLYNK_CONNECTED() {
  if (ontime) {
    unsigned long diff = millis() - ontime;
    offtime = offtime + diff;

    Blynk.virtualWrite(V1, offtime / 1000UL);
  }
}

Unbenannt

I think the logic you are using to calculate the connected and disconnected time periods is flawed in a number of ways.

Pete.

I agree with you. As I looked a bit deeper into it, I noticed that this sketch is not recognizing ALL offline time, just “some”. I tried to understand a bit more about the mechanism how the Blynk Heartbeat and Blynk.connected() logic works.

After reading some threads here about it, my understanding is, that Blynk.connected() is not immediately returning a FALSE value, after a disconnect happens. It takes some further seconds (possibly related to the second config Variable BLYNK_TIMEOUT_MS?), until Blynk recognizes its timeout. Hence, my sktech is (and was always intended to be) just a rough estimation of occurred disconnection time. However, it’s an underestemating algorithm, so actual offline time ratio should be higher than shown in the Web-Dashboard. I could verify this, by manually forcing my router to reconnect to Internet and simply measuring the real offline time vs. displayed offline time (wifi still on, just obtained new IP from my ISP).

Please don’t hesitate to tell me, if I’m missing sth. or I’m completely wrong here. I’m pretty new to Blynk. Even if you have any suggestions for improvement, I’d really appreciate your feedback.

To my original issue:
I had meanwhile the chance to test this sketch with same device in a different location (different ISP and Router/ Modem). Here It seems that no disconnects occur (except the daily ISP reconnect in the night). I’m currently rebuilding my original project sketch step by step to find out, which commit will cause the originally recognized disconnects in my project sketch. But with this minimal example it seems fine, at least here on the other site. I keep you posted.

My point is that, as far as I can see, your logic of using millis() is totally flawed. as is this logical test:

Personally, I’d use a timer that does a Blynk.connected test every second.
If this evaluates as true then incitement a “connected time” variable by 1.
If it evaluates a false then incitement a “disconnected time” variable by 1.

This will give you an approximate number of seconds that the device has been connected and disconnected.

As you’ve pointed-out, these numbers aren’t 100% accurate, but will give a much better indication of the situation than your current methodology.

Pete.

Thanks, Pete, for your input.

Actually, your cited line if (ontime) { always evaluates TRUE except for the very first server connection after boot up. This was necessary so that the time from millis()= 0 to very first server connect won’t get considered as disconnected time. But indeed, your timer solution is a nice and simple alternative :+1:

For anybody who may read this in future and have a similar issue with frequent disconnects. I think I identified the router as the source of my problem. I meanwhile switched back with my device to first place, where the disconnects always occurred. There I used an old DIR-615 router for my wifi. I replaced it with my fritzbox from second place and et voila, it works now without any disconnects (at least not recognized by my minimal sketch). Anyhow the old DIR-615 was not able to handle all my network traffic concurrently, so I’m going to replace it.

Thanks for your help, both!

1 Like