[local server] Blynk.connect behavior

Hello,

I note that if I disconnect ethernet cable for only a couple of second, the enc28j60 reconnect without problem.
But if i let it disconnected for a couple of minute, the sketch go on to try connecting whithout success, so I’ve to reboot the arduino to retrieve the connection.

Perhaps is there anywhere on the local server a parameter that can explain that?

@Alx-I presumably during the few seconds it doesn’t miss the Blynk hearbeat.
Study this sketch as you don’t need to reboot an Arduino when you lose connection to a Blynk server.

// BlynkNoBlock.ino for ENC28J60
#define BLYNK_PRINT Serial /* Comment this out to disable prints and save space */
#include <UIPEthernet.h>
#include <BlynkSimpleUIPEthernet.h>
BlynkTimer timer;
char auth[] = "xxxxxxxxxxxxxx";
unsigned int myEthernetTimeout =   5000;  // Ethernet Connection Timeout (ECT)
unsigned int blynkInterval     =  25000;  // Check Server Frequency      (CSF)

byte arduino_mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };    //DE:ED:BA:FE:FE:ED
char server[]=    "blynk-cloud.com"; // can use "blynk-cloud.com" or an IP address
IPAddress arduino_ip ( 192,   168,   10,  120);
IPAddress dns_ip     (  192,   168,   10,   90);
IPAddress gateway_ip ( 192,   168,   10,   90);
IPAddress subnet_mask(255, 255, 255,   0);

void setup()
{
  Serial.begin(115200);      // Debug console, Mega is OK at 115200
  Serial.println();
  timer.setInterval(myEthernetTimeout, myfunction);
  timer.setInterval(blynkInterval, checkBlynk);   // check connection to server per blynkInterval    
  Ethernet.begin(arduino_mac, arduino_ip, dns_ip, gateway_ip, subnet_mask );
  Blynk.config(auth, server);  // this is OK
  Blynk.connect();             // this is OK
}


void myfunction(){
  Serial.println("\tLook, no Blynk block.");
  if(Blynk.connected()){
    Serial.println("\tEthernet still connected.");
    Blynk.virtualWrite(V11, millis() / 1000);
  }
}

void checkBlynk() {
  unsigned long startConnecting = millis();    
  while(!Blynk.connected()){
    Blynk.connect();  
    if(millis() > startConnecting + myEthernetTimeout){
      Serial.println("\tUnable to connect to server. ");
      break;
    }
  }
  Serial.print("\tChecking again in ");
  Serial.print(blynkInterval / 1000.0);
  Serial.println("s.");
}

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

No, the heartbeats are well there because I can see them in the serial…
But after a too long time, it fails to reconnect.
My sketch is similar to yours…

//V1.1: to test stability connection & debug (23648/1357)
 
//<<LIBRARIES>>
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save flash space
#define BLYNK_DEBUG           // Optional, this enables lots of prints
#define BLYNK_NO_BUILTIN        // Disable built-in analog & digital pin operations to spare flash space
#define BLYNK_NO_FLOAT          // Disable float operations to spare flash space
#define BLYNK_NO_INFO           // Skip device info to spare flash space
#include <BlynkSimpleUIPEthernet.h>   // ENC28J60 ethernet BLYNK library



char auth[] = ".....................

  char Flag_Init_Boot='0';


// Mac address should be different for each device in your LAN
//byte arduino_mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };   //Original mac address
byte mac[] = { 0xDE, 0x................... };             

IPAddress localip ( 192, 1.........       // your arduino i.p address, through ENC28J60
IPAddress mydns ( 192, ...........
IPAddress gateway ( 192,.........
IPAddress subnet  (255..........
IPAddress serverip (109..........           // your local server i.p address  109.89.253.243
    
void setup() {
  Serial.begin(9600);                           // start serial for output  
  Blynk.begin(auth, serverip, 8442, localip, mydns, gateway, subnet, mac); 
} 


void loop(){  
while (Blynk.connected()==false) {Blynk.disconnect(); Blynk.connect();}  
Blynk.run(); 

  if (Flag_Init_Boot=='0'){                                         
  Blynk.email(F("R8"),F("WD"));
  Flag_Init_Boot='1';   
  }
}

How long is “too long time”?

Similar but different.
You should consider removing the Blynk.begin() blocking routine.

Some tests->
If I disconnect UTP, the first heartbeat is printed after ~15sec.
if I wait 60sec, the sketch reconnect →

Blockquote
[0] Using static IP
[1052] IP:192.168.0.57
[1053] ___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ / '/
/
//_, /////_
/
__/ v0.4.8 on Arduino Nano
[1165] Free RAM: 512
[1187] Connecting to IP
[2717] Connecting to IP
[3267] Connecting to IP
[3770] <[02|00|01|00]
[3771] <1125e09c0beauth
[3827] >[00|00|01|00|C8]
[3828] Ready (ping: 56ms).
[3828] <[0D|00|02|00|05]
[3856] <R8[00]WD
[5394] >[00|00|02|00|C8]
[13886] <[06|00|03|00|00]
[15887] <[06|00|04|00|00]
[17888] <[06|00|05|00|00]
[19889] <[06|00|06|00|00]
[21396] Heartbeat timeout: 21396, 5395, 19889
[21397] Disconnected
[21401] Connecting to IP
[46451] Connecting to IP
[71453] Disconnected
[71453] Connecting to IP
[71972] <[02|00|01|00]
[71973] <1125eauth
[72022] >[00|00|01|00|C8]
[72022] Ready (ping: 46ms).
[81977] <[06|00|02|00|00]

But if I reconnect the UTP after 120sec, it’s dead…

What happens after 120s with the sketch I provided?

I don’t success to connect with yours sketch->

Blockquote
[53] ___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ / '/
/
//_, /////_
/
__/ v0.4.8 on Arduino Nano
[551] Connecting to IP
[2124] Ready (ping: 44ms).
[417538626] Heartbeat timeout
[417538626] Connecting to IP
Look, no Blynk block.
[417539715] Connecting to IP
[417™ =>plantage

Je vois pas. J’ai juste adapté Blynk.config(auth, server, port); pour indiquer le port qui n’est pas par défaut…

I take it that’s not actual data from Serial Monitor as that would be 2 hours duration?

Yes it is just now after uploaded the sketch… strange!

And again ->
[53]
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ / '/
/
//_, /////_
/
__/ v0.4.8 on Arduino Nano

[551] Connecting to 109.89.253.243:8442
[2080] Connecting to 109.89.253.243:8442
[2628] Ready (ping: 46ms).
[1074464942] Heartbeat timeout
[1074464943] Connecting to 109.89.253.243:8442

I actually use the ENC on a Mega. You are probably out of memory on the Nano and you would need to do the same memory saving adjustments that you have in your original sketch.

Fine it’s working now !
Your sketch was just too fat for my little NANO :slight_smile:
After 120sec -> still ok to reconnect!

Nice. Is it simply because blynk.connect() doesn’t work properly after blynk.begin()?

1 Like

Blynk.begin() is only for beginners and is best avoided.

1 Like

Ok thank you, I’ll go further with these configurations…