Arduino did not reconnect if run first time without internet

Board: Arduino Uno
Connection : Ethernet Shield 5100
Blynk: V 0.6.0 and Iphone

I need some help in my below sketch. It is simple sync physical button example which work with and without internet and can reconnect again if internet available.

If I run it first time without internet it did not connect again but if first time run with internet and than it can reconnect after disconnect.

Second I want to reduce the waiting time to run offline when there is no internet.

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
BlynkTimer timer;

#define W5100_CS  10
#define SDCARD_CS 4

int DeviceLED = 11;
int ReCnctFlag;  // Reconnection Flag
int ReCnctCount = 0;  // Reconnection counter

const int ledPin1 = 2;
const int btnPin1 = A1;

void checkPhysicalButton();
int ledState1 = LOW;
int btnState1 = HIGH;

// Every time we connect to the cloud...
BLYNK_CONNECTED() {
  //   Request the latest state from the server
  Blynk.syncVirtual(V1);


  // Alternatively, you could override server state using:
  // Blynk.virtualWrite(V1, ledState1);

}

// When App button is pushed - switch the state
BLYNK_WRITE(V1) {
  ledState1 = param.asInt();
  digitalWrite(ledPin1, ledState1);
}

void checkPhysicalButton()
{
  if (digitalRead(btnPin1) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btnState1 != LOW) {

      // Toggle LED state
      ledState1 = !ledState1;
      digitalWrite(ledPin1, ledState1);

      // Update Button Widget
      Blynk.virtualWrite(V1, ledState1);
    }
    btnState1 = LOW;
  } else {
    btnState1 = HIGH;
  }
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card

  Ethernet.begin(mac);
  Blynk.config(auth);

  pinMode(ledPin1, OUTPUT);
  pinMode(btnPin1, INPUT_PULLUP);
  digitalWrite(ledPin1, ledState1);

  // Setup a function to be called every 100 ms
  timer.setInterval(100L, checkPhysicalButton);

  Blynk.connect();
  timer.setInterval(1000L, UpTime);
}

void UpTime() {
  Blynk.virtualWrite(V0, millis() / 1000);  // Send UpTime seconds to App
  Serial.print("UpTime: ");
  Serial.println(millis() / 1000);  // Send UpTime seconds to Serial
  digitalWrite(DeviceLED, !digitalRead(DeviceLED));  // Blink onboard LED
}

void loop() {
  timer.run();

  if (Blynk.connected()) {  // If connected run as normal
    Blynk.run();
  } else if (ReCnctFlag == 0) {  // If NOT connected and not already trying to reconnect, set timer to try to reconnect in 30 seconds
    ReCnctFlag = 1;  // Set reconnection Flag
    Serial.println("Starting reconnection timer in 30 seconds...");
    timer.setTimeout(30000L, []() {  // Lambda Reconnection Timer Function
      ReCnctFlag = 0;  // Reset reconnection Flag
      ReCnctCount++;  // Increment reconnection Counter
      Serial.print("Attempting reconnection #");
      Serial.println(ReCnctCount);
      Blynk.connect();  // Try to reconnect to the server
    });  // END Timer Function
  }
}

Serial output when run offline

[60731] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on Arduino Uno

[60824] Connecting to blynk-cloud.com:80
[65824] Connecting to blynk-cloud.com:80
Starting reconnection timer in 30 seconds...
UpTime: 70
UpTime: 71
UpTime: 72
UpTime: 73
UpTime: 74
                            //  Cut , but actually continue

UpTime: 99
Attempting reconnection #1
[99823] Connecting to blynk-cloud.com:80
[104824] Connecting to blynk-cloud.com:80
Starting reconnection timer in 30 seconds...
UpTime: 108
UpTime: 109
                             //  Cut , but actually continue

Attempting reconnection #2
[138823] Connecting to blynk-cloud.com:80
[143824] Connecting to blynk-cloud.com:80
Starting reconnection timer in 30 seconds...
UpTime: 147

Try to use blynk server IP instead of blynk-cloud.com

I didn’t get it. I am using ethernet shield and mac address in my sketch. Could you explain it.

Open a cmd window and execute ping blynk-cloud.com
You will get the IP adress
this is from France so not the same IP for you

Should I use like this
Blynk.begin(auth, server_ip, 8080, arduino_ip, dns_ip, gateway_ip, subnet_mask, arduino_mac);
in this way Blynk.begin() will stop off line working.
My old sketch is working fine when connect to internet

[4685] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on Arduino Uno

[4777] Connecting to blynk-cloud.com:80
[5130] Ready (ping: 124ms).
UpTime: 6

Do you mean it works perfectly with that?
weird !

very slow

If I disconnect lan cable / without internet my physical button should work which working now but once internet available it should connect again.

but it is not connecting when I run without internet.

But if first time I run the sketch with lan cable connected it will work without any problem and during this time many time I disconnect and reconnect with internet and working fine.

My target is:
Physical push button work with internet and without internet.
Also get connect / reconnect with server automatically when internet available.

I think I have some trouble here

 Ethernet.begin(mac);
  Blynk.config(auth);

I can’t help you , I only use WiFi with nodeMCU and ESP32, sorry

Thanks for your time.
I use same think with esp8266 it work perfect but with ethernet shield I change these
Ethernet.begin(mac);
Blynk.config(auth);
to work but I not succeed as esp.

1 Like

maybe @Gunner or @PeteKnight could help you :thinking:

No idea if you already do this… but…

It is not enough to use just Blynk.config(), you also need to isolate Blynk.run() so that it only executes when there is a Server connection, else it will stall out each loop cycle while trying to reconnect.

void loop() {
  if (Blynk.connected()) {  // If connected run as normal
    Blynk.run();
  }
// do all other loop stuff
}

I got the solution with improve in loop logic now its working and serial output is below. Only 10 seconds delay in offline sketch due to Blynk.config(auth);. Second thing. once I connect the lan cable it pass the logic but did not connect (ref. first attempt) But connect in second attempt.

If someone can help to reduce the timeout time in first bootup so my offline sketch start to work without 60 second delay and alternate of Blynk.config(auth); if possible.

Failed to configure Ethernet using DHCP
connecting using Ethernet...
[60738] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on Arduino Uno

[60898] Connecting to blynk-cloud.com:80
[65898] Connecting to blynk-cloud.com:80
Starting reconnection timer in 30 seconds...
UpTime: 70
UpTime: 71
UpTime: 72
UpTime: 73
UpTime: 74
UpTime: 75
UpTime: 76
UpTime: 77
UpTime: 78
UpTime: 79
UpTime: 80
UpTime: 81
UpTime: 82
UpTime: 83
UpTime: 84
UpTime: 85
UpTime: 86
UpTime: 87
UpTime: 88
UpTime: 89
UpTime: 90
UpTime: 91
UpTime: 92
UpTime: 93
UpTime: 94
UpTime: 95
UpTime: 96
UpTime: 97
UpTime: 98
UpTime: 99
Attempting reconnection #1
[99897] Connecting to blynk-cloud.com:80
[104898] Connecting to blynk-cloud.com:80
[109010] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on Arduino Uno

Successfull reconnect
Starting reconnection timer in 30 seconds...
UpTime: 109
UpTime: 109
UpTime: 110
UpTime: 111
UpTime: 112
UpTime: 113
UpTime: 114
UpTime: 115
UpTime: 116
UpTime: 117
UpTime: 118
UpTime: 119
UpTime: 120
UpTime: 121
UpTime: 122
UpTime: 123
UpTime: 124
UpTime: 125
UpTime: 126
UpTime: 127
UpTime: 128
UpTime: 129
UpTime: 130
UpTime: 131
UpTime: 132
UpTime: 133
UpTime: 134
UpTime: 135
UpTime: 136
UpTime: 137
UpTime: 138
Attempting reconnection #2
[139176] Connecting to blynk-cloud.com:80
[139509] Ready (ping: 142ms).
[139759] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on Arduino Uno

Successfull reconnect
UpTime: 139
UpTime: 140
UpTime: 141
UpTime: 142
UpTime: 143
UpTime: 144
UpTime: 145
UpTime: 146
UpTime: 147
UpTime: 148
UpTime: 149
UpTime: 150
UpTime: 151
UpTime: 152
UpTime: 153
UpTime: 154
UpTime: 155
UpTime: 156
UpTime: 157
UpTime: 158
UpTime: 159
UpTime: 160
UpTime: 161
UpTime: 162
UpTime: 163
UpTime: 164
UpTime: 165
UpTime: 166
UpTime: 167
UpTime: 168
UpTime: 169
UpTime: 170
UpTime: 171
UpTime: 172
UpTime: 173
UpTime: 174
UpTime: 175
UpTime: 176
UpTime: 177
UpTime: 178
UpTime: 179
UpTime: 180
UpTime: 181
UpTime: 182
UpTime: 183
UpTime: 184
UpTime: 185
UpTime: 186
UpTime: 187
UpTime: 188
UpTime: 189
UpTime: 190
UpTime: 191
UpTime: 192
UpTime: 193
UpTime: 194
UpTime: 195

I change in my loop

void loop() {
  timer.run();
  if (Blynk.connected()) {  // If connected run as normal
    Blynk.run();
  } else if (!Blynk.connected() && (ReCnctFlag == 0)) {  // If NOT connected and not already trying to reconnect, set timer to try to reconnect in 30 seconds
    ReCnctFlag = 1;  // Set reconnection Flag
    Serial.println("Starting reconnection timer in 30 seconds...");
    timer.setTimeout(30000L, []() {  // Lambda Reconnection Timer Function
      ReCnctFlag = 0;  // Reset reconnection Flag
      ReCnctCount++;  // Increment reconnection Counter
      Serial.print("Attempting reconnection #");
      Serial.println(ReCnctCount);
      Blynk.connect();  // Try to reconnect to the server
      if (Ethernet.begin(mac) == 1) {
       Blynk.config(auth);
        Serial.println("Successfull reconnect");
      }
    });
  }
}

And other side when Lan cable is connected than disconnect and reconnect logic also working fine as below

connecting using Ethernet...
[4685] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on Arduino Uno

[4809] Connecting to blynk-cloud.com:80
[5189] Ready (ping: 146ms).
UpTime: 6
UpTime: 7
UpTime: 8
UpTime: 9
UpTime: 10
UpTime: 11
UpTime: 12
UpTime: 13
UpTime: 14
UpTime: 15
UpTime: 16
UpTime: 17
UpTime: 18
UpTime: 19
UpTime: 20
UpTime: 21
UpTime: 22
UpTime: 23
UpTime: 24
[24463] Heartbeat timeout
UpTime: 25
Starting reconnection timer in 30 seconds...
UpTime: 26
UpTime: 27
UpTime: 28
UpTime: 29
UpTime: 30
UpTime: 31
UpTime: 32
UpTime: 33
UpTime: 34
UpTime: 35
UpTime: 36
UpTime: 37
UpTime: 38
UpTime: 39
UpTime: 40
UpTime: 41
UpTime: 42
UpTime: 43
UpTime: 44
UpTime: 45
UpTime: 46
UpTime: 47
UpTime: 48
UpTime: 49
UpTime: 50
UpTime: 51
UpTime: 52
UpTime: 53
UpTime: 54
UpTime: 55
Attempting reconnection #1
[55464] Connecting to blynk-cloud.com:80
[55803] Ready (ping: 138ms).
[56052] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on Arduino Uno

Successfull reconnect
UpTime: 56
UpTime: 57
UpTime: 58
UpTime: 59
UpTime: 60
UpTime: 61
UpTime: 62
UpTime: 63
UpTime: 64
UpTime: 65
1 Like

You’re always going to get inconsistent results with these Ethernet shields, they are generally very poor quality and don’t behave the way you expect.

You’ll generally get faster connections if you define a static IP address for your device, as then you don’t have to wait for the DHCP server to assign an IP address and do all the handshaking stuff. It can knock a few seconds (sometimes more) off the process.
Without knowing what you’re currently doing in your void setup, it’s difficult to say what could be done to improve the connection process to Blynk, but I’m sure there are some dramatic improvements available if the logic is improved.

Personally, when I do this stuff I take a different approach.
On startup I attempt to connect, and if that fails I set a flag to show that we’re running in standalone mode.
If the flag indicates if we’re in standalone mode then a simplified void loop logic path is used, then I attempt to re-connect every x seconds. In some cases this is a variable timing system, where y number of failed attempts drops the re-connect timing to a lower frequency, and z number of attempts extends the reconnection attempt timing even further.

Pete.

1 Like

Thanks PeteKnight for your time. I already set the flag in my loop as you say. And definitely need to improve the logic.

I add Blynk.connect(1000); instead of Blynk.connect(); so my attempting time is reduced from 10 sec to 01 sec.

Now I want to edit the Ethernet.h that if internet is not available it should not wait for 60 seconds to time out. I see this topic few days before and trying to search.

Thanks,

No, you’re not using a flag to know whether Blynk is connected or not, you’re calling the Blynk.connected() function to discover that each time. You do use a flag called ReCnctFlag, but in my mind it’s use is somewhat dubious, as although Blynk.connect() is non blocking in the sense that it won’t totally prevent further code execution if it can’t connect, I don’t think that void loop execution will continue uninterrupted while the Blynk.connect() function attempts a connection (but I may be wrong about this).

Does your project actually require the use of an Arduino Uno and Ethernet shield, could a NodeMCU with Wi-Fi connection be used instead? You’d certainly get better reliability and wouldn’t have to mess around hacking Ethernet libraries.

Pete.

1 Like

Ethernet.begin(mac); is blocking it should run once than Blynk.config(auth); will work.
So when I run without internet it didn’t execute thats why attempting to connect is fail but if I run with internet Ethernet.begin(mac); executed in setup so reconnection attempt is succeeded with only Blynk.config(auth);. Can you explain about Ethernet.begin() .

I need more than 10 GPIOs and future expansion also need that’s why I select arduino. With Nodemcu I already get the results but maximum 05 buttons.

If you need more than 10 gpio , use an esp32

1 Like

Or multiple ESP8266s and Bridge.

Pete.

2 Likes