Connection to Blynk always drops out after about an hour

Hi guys,

Firstly, apologies if I’m being a fool, but this is all VERY new to me. I’ve got an Arduino mega with an ethernet2 shield, which I’m using to switch 2 relays over Blynk, each on for 7 seconds in the event of a button press. Everything works exactly as I’d expect, but after about an hour or so of being up and running it stops working and tells me the ‘Arduino Mega is not in network’. If I turn it on and off again it works straight away, but is guaranteed to drop out again. Any thoughts? Here’s my code in case it’s something I’m doing wrong:

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <Ethernet2.h>
#include <BlynkSimpleEthernet2.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "Iwontleavethisontheforum";

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth);
  pinMode(2, OUTPUT);
  digitalWrite(2, HIGH);  // 
  pinMode(3, OUTPUT);
  digitalWrite(3, HIGH);  // 
  // You can also specify server.
  // For more options, see BoardsAndShields/Arduino_Ethernet_Manual example
  //Blynk.begin(auth, "your_server.com", 8442);
  //Blynk.begin(auth, IPAddress(192,168,1,100), 8888);
}

BLYNK_WRITE(V1)
{
  if (param.asInt() == 0) 
  {
    digitalWrite(2, HIGH);
  } 
else
  {
    digitalWrite(2, LOW);
    delay(7000);
  }
}
BLYNK_WRITE(V2)
{
  if (param.asInt() == 0) 
  {
    digitalWrite(3, HIGH);
  } 
  
else
  {
    digitalWrite(3, LOW);
    delay(7000);
  }
}

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

Thanks for your time guys, much appreciated.

Well, there are some issues with your code. The 7s delay is bound to cause errors along the way. There is a more elegant approach to this using the SimpleTimer library. It can run a timeout function, so you better use that. There are a couple of example of how to use this library and it’s very well documented on the Arduino.cc site.

Rule of thumb, avoids using delay. The reason behind this is that it stops everything your MCU is doing. It won’t respond to anything, hence, you will get a timeout connecting to the Blynk servers. I’m thinking this is the reason for your disconnect.

Thanks a lot for getting back to me, I’m aware it must become tedious answering such basic questions but it makes all the difference if someone can nudge you onto the right path. I’ll go away and see if I can research enough to replace the ‘delays’ in the code using the ‘Simple Timer Library’. I’ll report back soon,

Thanks again.

HammerandTongs

Your very welcome. We all gotta start somewhere and a basic question, if asked right, deserves an answer :wink:

Well, the issue hasn’t gone away. I’ve even uploaded the sketch I originally posted but without the delays and I still keep losing the connection. It did work at one point for about 30 hours on the weekend but since then I’ve been lucky to maintain connection to the network for more than 10 or 15 minutes. Anyone out there got any more suggestions? My internet connection is solid and I’ve also tried powering the arduino from the jack rather than usb but with no improvement. It’s really disappointing, if it wasn’t for this issue I’d be as pleased as punch!

Since you use ethernet, I’ve got a silly remark, did you switch network cables and/or the network shield?

Ow one more, which network shield are you using? There could be issues with that :slight_smile:

Thanks for being so quick with these replies. Haven’t switched any cables and it’s a branded Arduino Ethernet 2 shield. Am i really the only one who’s had this trouble??

Just to update, and hopefully someone may spot something, I uploaded just the simple ethernet 2 sketch to the arduino and used a button widget to do nothing more than light the LED integrated with pin 13. It works perfectly without any connection drop out so far, so the problem must be my code? The problem is, after removing the delays (thanks to Lichtsignaal) I can’t see ANY reason why there should be a problem. Maybe someone has had this before and will spot the problem?

Please post your latest sketch.

Hi,

Here’s the sketch I’ve been trying to use:

#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <SPI.h>
#include <Ethernet2.h>
#include <BlynkSimpleEthernet2.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = “yesIdidthis”;

void setup()
{
Serial.begin(9600);
Blynk.begin(auth);
pinMode(2, OUTPUT);
digitalWrite(2, HIGH); // Make this what ever you want you default value to be.
pinMode(3, OUTPUT);
digitalWrite(3, HIGH); // Make this what ever you want you default value to be.
// You can also specify server.
// For more options, see BoardsAndShields/Arduino_Ethernet_Manual example
//Blynk.begin(auth, “your_server.com”, 8442);
//Blynk.begin(auth, IPAddress(192,168,1,100), 8888);
}

BLYNK_WRITE(V1)
{
if (param.asInt() == 0)
{
digitalWrite(2, HIGH);
}
else
{
digitalWrite(2, LOW);

}
}
BLYNK_WRITE(V2)
{
if (param.asInt() == 0)
{
digitalWrite(3, HIGH);
}

else
{
digitalWrite(3, LOW);

}
}

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

It’s just switching 2 relays, but I’ve obviously done something wrong. Thanks in advance, this is driving me nuts now.

It could be wiring or power supply in that case. Code seems fine. Also make sure those pins not used for Ethernet shield.

I really think everything else is OK, but I’ll go and double check. Good to know the code looks alright though. Thanks

Hi all, quick update here in case anyone ever encounters the same problem. I was losing the will to live with it to be honest and I practically put the whole project back together again in the hope that I’d missed something - yet the connection to the servers kept dropping out every hour. Anyway, I’d put a blank micro SD card in the Mega for the future but decided to take it out and hey presto the connection has been perfect ever since. I don’t know what it was, certainly not about which pins the card might affect because I’d tried different configurations when troubleshooting. Any way, it works flawlessly now and a big thank you for the people who took the time to help me out.

1 Like