Improving Ping

Hello all,

The Blynk startup Serial Monitor is reporting that I have a Ping of around 300ms.

[7068] Connecting to  ...
[8591] Connected to GPRS
[8592] Connecting to blynk.cloud:80
[9882] Ready (ping: 298ms).

I am guessing that this is due to my location (New Zealand),

Is it possible to improve this? I have read in other posts that anything above 200ms can cause connection stability issues.

Best,

M

Which regional server is your account on?

Pete.

Hello Pete,

Region: ny3

Best,

M

Okay, so your account lives on a server in New York, a distance of around 15,000km

Your nearest geographical server is in Singapore, a distance of around 8,500km

The issue of transferring an account to a more local server is discussed in this topic, where Bill (who is in Australia) also uses the NY3 server…

Pete.

Hello again, @PeteKnight et al…

I am having some continued connection issues. Could you assist please?

I captured this from the serial monitor,

L01var = 157
V01var = 508
V02var = 0
C19var = 2
C19var = 3
T01var = 26
T02var = 44
L01var = 166
V01var = 508
V02var = 0
C19var = 2
T01var = 26
T02var = 42
L01var = 178
V01var = 508
V02var = 0
[13601241] Heartbeat timeout
[13611267] Connecting to blynk.cloud:80

As you can see, it was working well for a long time, nearly 4 hours before it disconnected then did not attempt to reconnect again.

what could be causing this please?

Here is it’s full code…


#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL2GriGYWFd"
#define BLYNK_TEMPLATE_NAME "Spira Fairy Lights Only"
#define BLYNK_AUTH_TOKEN "xnPhxFufNr8z5yRC7ebn7RGB7Rvz5uvr"

#include <MKRNB.h>
#include <BlynkSimpleMKRNB.h>
#include <Arduino.h>
#include <Wire.h>

#define SLAVE_ADDR 9
#define ANSWERSIZE 6

String receivedResponse = "";

int C01var = 1;  // Colour Variables
int C02var = 1;
int C03var = 1;
int C04var = 1;
int C05var = 1;
int C06var = 1;
int C07var = 1;
int C08var = 1;
int C09var = 1;
int C10var = 1;
int C11var = 1;
int C12var = 1;
int C13var = 1;
int C14var = 1;
int C15var = 1;
int C16var = 1;
int C17var = 1;
int C18var = 1;
int C19var = 1;
int C20var = 1;
int C21var = 1;
int C22var = 1;
int C23var = 1;
int C24var = 1;
int C25var = 1;
int C26var = 1;
int C27var = 1;
int C28var = 1;
int C29var = 1;
int C30var = 1;
int C31var = 1;
int C32var = 1;
int C33var = 1;
int C34var = 1;
int C35var = 1;
int C36var = 1;
int C37var = 1;
int C38var = 1;
int C39var = 1;
int C40var = 1;
int C41var = 1;
int C42var = 1;
int C43var = 1;
int C44var = 1;
int C45var = 1;
int C46var = 1;
int C47var = 1;
int C48var = 1;
int C49var = 1;
int C50var = 1;

int P01var = 1;  //power variable

int E01var = 1;     //select effect
int E02var = 1000;  //effect delay

int T01var = 0;  //Temperature
int T02var = 0;  //Humidity

int A01var = 0;  //CT1
int A02var = 0;  //CT2
int A03var = 0;  //CT3
int A04var = 0;  //CT4

int L01var = 0;  //Light Level LUX / 10

int V01var = 0;  //PSU Voltage
int V02var = 0;  //Mains detection

int R01var = 1;  //Relay control

const int status4pin = 0;
const int status5pin = 1;
const int status6pin = 2;
const int hbPin = 3;

bool status4state = false;
bool status5state = false;
bool status6state = false;
bool hbState = false;

NBClient client;  //init Blynk objects
GPRS gprs;
NB nbAccess;
BlynkTimer timer;

char pin[] = "";

void sensorTimer() {

  status6state = !status6state;
  digitalWrite(status6pin, status6state);

  Blynk.virtualWrite(V54, T01var);
  Serial.print("T01var = ");
  Serial.println(T01var);

  Blynk.virtualWrite(V55, T02var);
  Serial.print("T02var = ");
  Serial.println(T02var);

  Blynk.virtualWrite(V60, L01var);
  Serial.print("L01var = ");
  Serial.println(L01var);

  Blynk.virtualWrite(V61, V01var);
  Serial.print("V01var = ");
  Serial.println(V01var);

  Blynk.virtualWrite(V62, V02var);
  Serial.print("V02var = ");
  Serial.println(V02var);
}

void heartbeatTimer() {

  if (Blynk.connected()) {

    status5state = !status5state;
    hbState = !hbState;

    digitalWrite(status5pin, status5state);
    digitalWrite(hbPin, hbState);
  }
}

void packetTimer() {

  status4state = !status4state;
  digitalWrite(status4pin, status4state);

  static int packetIndex = 0;

  String master;

  switch (packetIndex) {
    case 0:
      master = "P01" + padWithZeros(P01var, 3);
      break;
    case 1:
      master = "R01" + padWithZeros(R01var, 3);
      break;
    case 2:
      master = "C19" + padWithZeros(C19var, 3);
      break;
    case 3:
      master = "C21" + padWithZeros(C21var, 3);
      break;
    case 4:
      master = "C23" + padWithZeros(C23var, 3);
      break;
    case 5:
      master = "C25" + padWithZeros(C25var, 3);
      break;
    case 6:
      master = "C27" + padWithZeros(C27var, 3);
      break;
    case 7:
      master = "C29" + padWithZeros(C29var, 3);
      break;
    case 8:
      master = "C31" + padWithZeros(C31var, 3);
      break;
    case 9:
      master = "C33" + padWithZeros(C33var, 3);
      break;
    case 10:
      master = "C35" + padWithZeros(C35var, 3);
      break;
    case 11:
      master = "C37" + padWithZeros(C37var, 3);
      break;
    case 12:
      master = "C39" + padWithZeros(C39var, 3);
      break;
    case 13:
      master = "C41" + padWithZeros(C41var, 3);
      break;
    case 14:
      master = "C43" + padWithZeros(C43var, 3);
      break;
    case 15:
      master = "C49" + padWithZeros(C49var, 3);
      break;
    case 16:
      master = "E01" + padWithZeros(E01var, 3);
      break;
    default:
      break;
  }

  // Increment the index and wrap around if needed
  packetIndex = (packetIndex + 1) % 17;  // Adjust to match the number of cases

  Wire.beginTransmission(SLAVE_ADDR);
  Wire.write(master.c_str(), master.length());
  Wire.endTransmission();

  // Receive and print the response from the slave
  Wire.requestFrom(SLAVE_ADDR, ANSWERSIZE);
  receivedResponse = "";
  while (Wire.available()) {
    receivedResponse += (char)Wire.read();
  }

  if (receivedResponse.startsWith("T01") && receivedResponse.length() >= 6) {
    T01var = receivedResponse.substring(3, 6).toInt();
    //Serial.print("  T01:");
    //Serial.print(T01var);
  }

  if (receivedResponse.startsWith("T02") && receivedResponse.length() >= 6) {
    T02var = receivedResponse.substring(3, 6).toInt();
    //Serial.print("  T02:");
    //Serial.print(T02var);
  }

  if (receivedResponse.startsWith("L01") && receivedResponse.length() >= 6) {
    L01var = receivedResponse.substring(3, 6).toInt();
    //Serial.print("  L01:");
    //Serial.print(L01var);
  }

  if (receivedResponse.startsWith("V01") && receivedResponse.length() >= 6) {
    V01var = receivedResponse.substring(3, 6).toInt();
    //Serial.print("  L01:");
    //Serial.print(L01var);
  }

  if (receivedResponse.startsWith("V02") && receivedResponse.length() >= 6) {
    V02var = receivedResponse.substring(3, 6).toInt();
    //Serial.print("  L01:");
    //Serial.print(L01var);
  }
}

BLYNK_WRITE(V50) {  //P01var
  P01var = param.asInt();
  Serial.print("P01var = ");
  Serial.println(P01var);
}

BLYNK_WRITE(V53) {  //R01var
  R01var = param.asInt();
  Serial.print("R01var = ");
  Serial.println(R01var);
}

BLYNK_WRITE(V18) {  //C19var
  C19var = param.asInt();
  Serial.print("C19var = ");
  Serial.println(C19var);
}

BLYNK_WRITE(V20) {  //C21var
  C21var = param.asInt();
  Serial.print("C21var = ");
  Serial.println(C21var);
}

BLYNK_WRITE(V22) {  //C23var
  C23var = param.asInt();
  Serial.print("C23var = ");
  Serial.println(C23var);
}

BLYNK_WRITE(V24) {  //C25var
  C25var = param.asInt();
  Serial.print("C25var = ");
  Serial.println(C25var);
}

BLYNK_WRITE(V26) {  //C27var
  C27var = param.asInt();
  Serial.print("C27var = ");
  Serial.println(C27var);
}

BLYNK_WRITE(V28) {  //C29var
  C29var = param.asInt();
  Serial.print("C29var = ");
  Serial.println(C29var);
}

BLYNK_WRITE(V30) {  //C31var
  C31var = param.asInt();
  Serial.print("C31var = ");
  Serial.println(C31var);
}

BLYNK_WRITE(V32) {  //C33var
  C33var = param.asInt();
  Serial.print("C33var = ");
  Serial.println(C33var);
}

BLYNK_WRITE(V34) {  //C35var
  C35var = param.asInt();
  Serial.print("C35var = ");
  Serial.println(C35var);
}

BLYNK_WRITE(V36) {  //C37var
  C37var = param.asInt();
  Serial.print("C37var = ");
  Serial.println(C37var);
}

BLYNK_WRITE(V38) {  //C39var
  C39var = param.asInt();
  Serial.print("C39var = ");
  Serial.println(C39var);
}

BLYNK_WRITE(V40) {  //C41var
  C41var = param.asInt();
  Serial.print("C41var = ");
  Serial.println(C41var);
}

BLYNK_WRITE(V42) {  //C43var
  C43var = param.asInt();
  Serial.print("C43var = ");
  Serial.println(C43var);
}

BLYNK_WRITE(V48) {  //C48var
  C49var = param.asInt();
  Serial.print("C49var = ");
  Serial.println(C49var);
}

BLYNK_WRITE(V51) {  //C43var
  E01var = param.asInt();
  Serial.print("E01var = ");
  Serial.println(E01var);
}


void setup() {
  // Debug console
  Serial.begin(9600);
  Blynk.begin(BLYNK_AUTH_TOKEN, nbAccess, gprs, client, pin);
  timer.setInterval(60 * 1000L, sensorTimer);
  timer.setInterval(10L, packetTimer);
  timer.setInterval(1000L, heartbeatTimer);

  Wire.begin();
  pinMode(status4pin, OUTPUT);
  pinMode(status5pin, OUTPUT);
  pinMode(status6pin, OUTPUT);
  pinMode(hbPin, OUTPUT);
}

String padWithZeros(int value, int width) {
  String str = String(value);
  while (str.length() < width) {
    str = "0" + str;
  }
  return str;
}

void loop() {

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


Thank you for your assistance.

Best,

M

The fact that you’re using GPRS is probably the largest contributing factor to the long ping times.
Have you tried increasing the heartbeat timeout?

Have you done any testis to see how long void sensorTimer() takes to execute?

Pete.

At the moment we don’t have an API that allows us to move the user user from one region to another. So the only option is to remove account and create a new one in the required region.

Could you pleaes do the next:

ping sgp1.blynk.cloud
ping ny3.blynk.cloud

and post the results here?

Hello @Dmitriy,

This is what I get from my desktop…

C:\Users\noodi>ping sgp1.blynk.cloud

Pinging sgp1.blynk.cloud [128.199.144.129] with 32 bytes of data:
Reply from 128.199.144.129: bytes=32 time=125ms TTL=48
Reply from 128.199.144.129: bytes=32 time=125ms TTL=48
Reply from 128.199.144.129: bytes=32 time=124ms TTL=48
Reply from 128.199.144.129: bytes=32 time=124ms TTL=48

Ping statistics for 128.199.144.129:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 124ms, Maximum = 125ms, Average = 124ms

C:\Users\noodi>ping ny3.blynk.cloud

Pinging ny3.blynk.cloud [64.225.16.22] with 32 bytes of data:
Reply from 64.225.16.22: bytes=32 time=197ms TTL=46
Reply from 64.225.16.22: bytes=32 time=192ms TTL=46
Reply from 64.225.16.22: bytes=32 time=193ms TTL=46
Reply from 64.225.16.22: bytes=32 time=194ms TTL=46

Ping statistics for 64.225.16.22:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 192ms, Maximum = 197ms, Average = 194ms

I am unsure how to ping from the device itself, are there guides on this, I attempted with the Sara Module using AT+UPING, but it appears that it has issues.

It does appear that singapore is ‘closer’ for us anyhow.

Best,

M

Hi @PeteKnight,

I am sure, but I need to use mobile, I don’t have any choice in that.

The ping from my desktop to ny3 is 197ms and my device gets 298ms, so I suppose it would be logical to assume that using GPRS is adding 100ms.

The ping from my desktop to sgp1 is 125ms so I would assume my device would achieve something akin to 225ms if assigned to sgp1.

I have run tests on the run time of sensorTimer() and here are the results, about 1000ms. As you can see I had one disconnect…

[7572] Connecting to  ...
[9095] Connected to GPRS
[9096] Connecting to blynk.cloud:80
[10187] Ready (ping: 298ms).
976
1665
1247
1228
1192
[367869] Connecting to blynk.cloud:80
190
[372019] Ready (ping: 1375ms).
993
995
993
995
992
994
992
993

My thoughts are to put each of the Blynk.virtualWrite’s in their own timer and run them each 60s but staggered so that the code can return to void loop() between transmissions, would you agree with that approach, I am unsure of how to do the staggering, but I will look into that now.

Best,

M

Pete.

Thank you @PeteKnight,

I haven’t done that yet, and to be honest I am a bit lost.

Reading this,

It appears that the total time from loss of connection would be 45seconds + 45seconds + 103 seconds before resetting, this is a huge amount of time, and I don’t understand how this could be affected by a ping of 300ms…?

This said, am happy to try anything, what do you suggest I improve it to and how would I go about doing that?

With this?

BLYNK_HEARTBEAT

I can’t see any examples of this so would appreciate help?

Best,

M

I have now replaced…

void sensorTimer() {

  pointOne = millis();

  status6state = !status6state;
  digitalWrite(status6pin, status6state);

  Blynk.virtualWrite(V54, T01var);
  //Serial.print("T01var = ");
  //Serial.println(T01var);

  Blynk.virtualWrite(V55, T02var);
  //Serial.print("T02var = ");
  //Serial.println(T02var);

  Blynk.virtualWrite(V60, L01var);
  //Serial.print("L01var = ");
  //Serial.println(L01var);

  Blynk.virtualWrite(V61, V01var);
  //Serial.print("V01var = ");
  //Serial.println(V01var);

  Blynk.virtualWrite(V62, V02var);
  //Serial.print("V02var = ");
  //Serial.println(V02var);

  Blynk.virtualWrite(V56, A01var);
  //Serial.print("A01var = ");
  //Serial.println(A01var);

  Blynk.virtualWrite(V59, A04var);
  //Serial.print("A04var = ");
  //Serial.println(A04var);

  pointTwo = millis();

  pointThree = pointTwo - pointOne;
  Serial.println(pointThree);
}

with…

void sensorTimer() {
  pointOne = millis();
  status6state = !status6state;
  digitalWrite(status6pin, status6state);

  switch (currentVirtualWriteIndex) {
    case 0:
      Blynk.virtualWrite(V54, T01var);
      break;
    case 1:
      Blynk.virtualWrite(V55, T02var);
      break;
    case 2:
      Blynk.virtualWrite(V60, L01var);
      break;
    case 3:
      Blynk.virtualWrite(V61, V01var);
      break;
    case 4:
      Blynk.virtualWrite(V62, V02var);
      break;
    case 5:
      Blynk.virtualWrite(V56, A01var);
      break;
    case 6:
      Blynk.virtualWrite(V59, A04var);
      break;
  }

  currentVirtualWriteIndex = (currentVirtualWriteIndex + 1) % 7;

  pointTwo = millis();
  pointThree = pointTwo - pointOne;
  Serial.println(pointThree);
}

sensorTimer() now runs more frequently but only is taking 140ms to execute, is this acceptable?

[7572] Connecting to  ...
[9095] Connected to GPRS
[9096] Connecting to blynk.cloud:80
[13422] Ready (ping: 1533ms).
141
141
140
140
141
141
141
140
141
140
141
139
140
139
140

Best,

M

If you go to Web Dashboard > Device > Device Info and scroll down to the bottom, what value is shown below HEARTBEAT INTERVAL ?

The heartbeat interval can be changed by adding…

#define BLYNK_HEARTBEAT 60

Near the top of your sketch. The value (60 in the example above) represents the heartbeat interval in seconds.

Pete.

Thank you @PeteKnight,

I will change this to 90? bit of a shot in the dark?

Best,

M

I’ve no idea, because you haven’t answered my question.

Pete.

Which one Pete, sorry if I missed it?

Oh, I see now sorry,

Mine was 60.

Best,

M

@Dmitriy @PeteKnight

Hi Dmitriy, I posted the ping results in an earlier post as requested,

Could we please pick up where we left off with this?

Best,

Matt

First of all, the heartbeat has nothing to do with the ping. It’s just a setting to handle online/offline status on the UI mostly.

It’s strange that ny ping is smaller than sgp ping for you. And looks like your account is on the SGP server. I wish we had an Australian server, but we don’t. And we don’t have plans for it in the near future. That would definitely improve the ping for you.

So for now the only possible fix for you would be to manually go to ny3.blynk.cc and create an account there (if it’s really 100ms quicker). Also, before doing that - it would be good to remove the existing account to avoid possible issues.

Could you please ping 170.64.160.170? It’s Sydney server I just started. Interesting to see what ping it will show.

Here you go

Pinging 170.64.160.170 with 32 bytes of data:
Reply from 170.64.160.170: bytes=32 time=34ms TTL=246
Reply from 170.64.160.170: bytes=32 time=33ms TTL=246
Reply from 170.64.160.170: bytes=32 time=34ms TTL=246
Reply from 170.64.160.170: bytes=32 time=33ms TTL=246

Ping statistics for 170.64.160.170:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 33ms, Maximum = 34ms, Average = 33ms