BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

If the server falls, the "BlynkTimer" crashes too

#1

I’m running this example but when “My Local Server” loses the network connection the sketch stop on the “UpTime:” and it dosn’t run any more: “timer.setInterval” gets stuck like this:

UpTime: 57
UpTime: 58
UpTime: 59
UpTime: 60
UpTime:

I don’t know, what’s wrong? I need some help.

The example that I’m using is the “#9 Basic “Keeps running without WiFi or Blynk Server connection, but reconnects as soon as they are available” sketch” that are on this link:

0 Likes

#2

What do all those Serial debug statements I put in there tell you?

0 Likes

#3

Hello @Gunner , in the most of time when I disconnect the WiFi adpter of the server simulating a connection fall, the Sketch stop.

ESP8266 12E
Core 2.4.1

Serial Exit:

tail 8
chksum 0x2d
csum 0x2d
v614f7c32
~ld
scandone
[158083] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.5.3 on NodeMCU
> Blockquote 
  Give Blynk a Github star! => https://github.com/blynkkk/blynk-library
> Blockquote 
scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 1
cnt 
> Blockquote 
connected with WiFi, channel 9
dhcp client start...
ip:192.168.xx.xx,mask:255.255.255.0,gw:192.168.xx.xx
[158594] NTP time: Tue Sep 18 09:20:50 2018
[158594] Connecting to myhostname.ddns.net:8441
[159146] Fingerprint OK
[159161] Ready (ping: 13ms).
Cconnected
UpTime: 160
UpTime: 161
UpTime: 162
UpTime: 163
UpTime: 164
UpTime: 165
UpTime: 166
UpTime: 167
pm open,type:2 0
UpTime: 168
UpTime: 169
UpTime: 170
UpTime: 171
UpTime: 172
UpTime: 173
UpTime: 174
UpTime: 175
[175244] Heartbeat timeout
Starting reconnection timer in 30 seconds...
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
UpTime: 196
UpTime: 197
UpTime: 198
UpTime: 199
UpTime: 200
UpTime: 201
UpTime: 202
UpTime: 203
UpTime: 204
UpTime: 205
Attempting reconnection #1
[205245] NTP time: Tue Sep 18 09:21:36 2018
[205245] Connecting to myhostname.ddns.net:8441
[210246] NTP time: Tue Sep 18 09:21:41 2018
[210246] Connecting to myhostname.ddns.net:8441
Starting reconnection timer in 30 seconds...
UpTime: 212
UpTime: 213
UpTime: 214
UpTime: 215
UpTime: 216
UpTime: 217
UpTime: 218
UpTime: 219
UpTime: 220
UpTime: 221
UpTime: 222
UpTime: 223
UpTime: 224
UpTime: 225
UpTime: 226
UpTime: 227
UpTime: 228
UpTime: 229
UpTime: 230
UpTime: 231
UpTime: 232
UpTime: 233
UpTime: 234
UpTime: 235
UpTime: 236
UpTime: 237
UpTime: 238
UpTime: 239
UpTime: 240
UpTime: 241
UpTime: 242
Attempting reconnection #2
[242249] NTP time: Tue Sep 18 09:22:13 2018
[242249] Connecting to myhostname.ddns.net:8441
[242884] Fingerprint OK
[242903] Ready (ping: 16ms).
Cconnected
UpTime: 243
UpTime: 244
UpTime: 245
UpTime: 246
UpTime: 247
UpTime: 248
UpTime: 249
UpTime: 250
UpTime: 251
UpTime: 252
UpTime: 253
UpTime: 254
UpTime: 255
UpTime: 256
UpTime: 257
UpTime: 258
[258985] Heartbeat timeout
Starting reconnection timer in 30 seconds...
UpTime: 259
UpTime: 260
UpTime: 261
UpTime: 262
UpTime: 263
UpTime: 264
UpTime: 265
UpTime: 266
UpTime: 267
UpTime: 268
UpTime: 269
UpTime: 270
UpTime: 271
UpTime: 272
UpTime: 273
UpTime: 274
UpTime: 275
UpTime: 276
UpTime: 277
UpTime: 278
UpTime: 279
UpTime: 280
UpTime: 281
UpTime: 282
UpTime: 283
UpTime: 284
UpTime: 285
UpTime: 286
UpTime: 287
UpTime: 288
Attempting reconnection #1
[288986] NTP time: Tue Sep 18 09:23:00 2018
[288986] Connecting to myhostname.ddns.net:8441
[289550] Fingerprint OK
[289566] Ready (ping: 14ms).
Cconnected
UpTime: 289
UpTime: 290
UpTime: 291
UpTime: 292
UpTime: 293
UpTime: 294
UpTime: 295
UpTime: 296
UpTime: 297
UpTime: 298
UpTime: 299
UpTime: 300
UpTime: 301
UpTime: 302
UpTime: 303
UpTime: 304
UpTime: 305
UpTime: 306
UpTime: 307
UpTime: 308
UpTime: 309
UpTime: 310
UpTime: 311
UpTime: 312
UpTime: 313
UpTime: 314
UpTime: 315
UpTime: 316
UpTime: 317
UpTime: 318
UpTime: 319
UpTime: 320
UpTime: 321
UpTime: 322
UpTime: 323
UpTime: 324
UpTime: 325
UpTime: 326
UpTime: 327
UpTime: 328
UpTime: 329
UpTime: 330
UpTime: 331
UpTime: 332
UpTime: 333
UpTime: 334
UpTime: 335
UpTime: 336
UpTime: 337
UpTime: 338
UpTime: 339
UpTime: 340
UpTime: 341
[341882] Heartbeat timeout
Starting reconnection timer in 30 seconds...
UpTime: 342
UpTime: 343
UpTime: 344
UpTime: 345
UpTime: 346
UpTime: 347
UpTime: 348
UpTime: 349
UpTime: 350
UpTime: 351
UpTime: 352
UpTime: 353
UpTime: 354
UpTime: 355
UpTime: 356
UpTime: 357
UpTime: 358
UpTime: 359
UpTime: 360
UpTime: 361
UpTime: 362
UpTime: 363
UpTime: 364
UpTime: 365
UpTime: 366
UpTime: 367
UpTime: 368
UpTime: 369
UpTime: 370
UpTime: 371
Attempting reconnection #1
[371883] NTP time: Tue Sep 18 09:24:23 2018
[371883] Connecting to myhostname.ddns.net:8441
[372530] Fingerprint OK
[372544] Ready (ping: 12ms).
Cconnected
UpTime: 372
UpTime: 373
UpTime: 374
UpTime: 375
UpTime: 376
UpTime: 377
UpTime: 378
UpTime: 379
UpTime: 380
UpTime: 381
UpTime: 382
UpTime: 383
UpTime: 384
UpTime: 385
UpTime: 386
UpTime: 387
UpTime: 388
UpTime: 389
UpTime: 390
UpTime: 391
UpTime: 392
UpTime: 393
UpTime: 394
UpTime: 395
UpTime: 396
UpTime: 397
UpTime: 398
UpTime: 399
UpTime: 400
UpTime: 401
UpTime: 402
UpTime: 403
UpTime: 404
UpTime: 405
UpTime: 406
UpTime: 407
UpTime: 408
UpTime: 409
UpTime: 410
UpTime: 411
UpTime: 412
UpTime: 413
UpTime: 414
UpTime: 415
UpTime: 416
UpTime: 417
UpTime: 418
UpTime: 419
UpTime: 420
UpTime: 421
UpTime: 422
UpTime: 423
UpTime: 424
UpTime: 425
UpTime: 426
UpTime: 427
UpTime: 428
UpTime: 429
UpTime: 430
UpTime: 431
UpTime: 432
UpTime: 433
UpTime: 434
UpTime: 435
UpTime: 436
UpTime: 437
UpTime: 438
UpTime: 439
UpTime: 440
UpTime: 441
UpTime: 442
[442868] Disconnected
Connected, updating
HTTP_UPDATE_FAILD Error (11): Update error: ERROR[11]: Invalid bootstrapping state, reset ESP8266 before updatingStarting reconnection timer in 30 seconds...
sl
[64] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.5.3 on NodeMCU
> Blockquote 
  Give Blynk a Github star! => https://github.com/blynkkk/blynk-library
> Blockquote 
scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 1
cnt 
> Blockquote 
connected with WiFi, channel 9
dhcp client start...
ip:192.168.xx.xx,mask:255.255.255.0,gw:192.168.xx.xx
[575] NTP time: Tue Sep 18 09:25:41 2018
[575] Connecting to myhostname.ddns.net:8441
[5076] NTP time: Tue Sep 18 09:25:45 2018
[5076] Connecting to myhostname.ddns.net:8441
[5598] Fingerprint OK
[5613] Ready (ping: 12ms).
Cconnected
UpTime: 6
UpTime: 7
UpTime: 8
UpTime: 9
pm open,type:2 0
UpTime: 10
UpTime: 11
UpTime: 12
UpTime: 13
UpTime: 14
UpTime: 15
UpTime: 16
UpTime: 17
UpTime: 18
UpTime: 19
UpTime: 20
UpTime: 21
[21698] Heartbeat timeout
Starting reconnection timer in 30 seconds...
UpTime: 22
UpTime: 23
UpTime: 24
UpTime: 25
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
Attempting reconnection #1
[51699] NTP time: Tue Sep 18 09:26:31 2018
[51699] Connecting to myhostname.ddns.net:8441
[52203] Fingerprint OK
[52215] Ready (ping: 9ms).
Cconnected
UpTime: 52
UpTime: 53
UpTime: 54
UpTime: 55
UpTime: 56
UpTime: 57
UpTime: 58
UpTime: 59
UpTime: 60
UpTime: 61
UpTime: 62
UpTime: 63
UpTime: 64
UpTime: 65
UpTime: 66
UpTime: 67
UpTime: 68
UpTime: 69
[70358] Disconnected
Connected, updating
sleep disable
state: 5 -> 0 (0)
rm 0
pm close 7
del if0
usl
> Blockquote 
 ets Jan  8 2013,rst cause:2, boot mode:(3,7)
> Blockquote 
load 0x4010f000, len 1384, room 16 
tail 8
chksum 0x2d
csum 0x2d
v614f7c32
@cp:0
ld
scandone
[3829] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.5.3 on NodeMCU
> Blockquote 
  Give Blynk a Github star! => https://github.com/blynkkk/blynk-library
> Blockquote 
scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 1
cnt 
> Blockquote 
connected with WiFi, channel 9
dhcp client start...
ip:192.168.xx.xx,mask:255.255.255.0,gw:192.168.xx.xx
[6839] NTP time: Tue Sep 18 09:27:13 2018
[6840] Connecting to myhostname.ddns.net:8441
[8867] NTP time: Tue Sep 18 09:27:15 2018
[8867] Connecting to myhostname.net:8441
Starting reconnection timer in 30 seconds...
UpTime: 11
UpTime: 12
UpTime: 13
UpTime: 14
UpTime: 15
pm open,type:2 0
UpTime: 16
UpTime: 17
UpTime: 18
UpTime: 19
UpTime: 20
UpTime: 21
UpTime: 22
UpTime: 23
UpTime: 24
UpTime: 25
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
Attempting reconnection #1
[40869] NTP time: Tue Sep 18 09:27:46 2018
[40869] Connecting to myhostname.ddns.net:8441
[41461] Fingerprint OK
[41477] Ready (ping: 14ms).
Cconnected
UpTime: 41
UpTime: 42
...
UpTime: 56
[57561] Heartbeat timeout
Starting reconnection timer in 30 seconds...
UpTime: 57
UpTime: 58
...
UpTime: 85
UpTime: 86
Attempting reconnection #1    //  _**Ok**_
[87562] NTP time: Tue Sep 18 09:28:33 2018
[87562] Connecting to myhostname.ddns.net:8441
[92563] NTP time: Tue Sep 18 09:28:38 2018
[92563] Connecting to myhostname.ddns.net:8441
Starting reconnection timer in 30 seconds...
UpTime: 94
UpTime: 94
UpTime: 95
UpTime: 96
...
UpTime: 122
UpTime: 123   
Attempting reconnection #2     //  **_Ok_**
[124566] NTP time: Tue Sep 18 09:29:10 2018
[124566] Connecting to myhostname.ddns.net:8441
[125119] Fingerprint OK
[125138] Ready (ping: 16ms).
Cconnected
UpTime: 125
UpTime: 125
UpTime: 126
UpTime: 127
...
UpTime: 156
UpTime: 157
UpTime: 158   //   _**failed...**_

This is the complete Sketch that I’m using:

#define BLYNK_PRINT Serial // This prints to Serial Monitor
//#define BLYNK_DEBUG
#define USE_SERIAL Serial

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h>
#include <BlynkSimpleEsp8266_SSL.h>

//#define BLYNK_MSG_LIMIT 0
//#define BLYNK_HEARTBEAT 60

//#include <ESP8266mDNS.h>  // For OTA
//#include <WiFiUdp.h>  // For OTA
//#include <ArduinoOTA.h>  // For OTA

char auth[] = "193349xxxxxxxxx3b54deda2846";
char ssid[] = "xxxxx";
char pass[] = "xxxx";
//char server[] = "xxx.xxx.xxx.xxx";  // IP for Local Cloud Server
char server[] = "xxxxx.ddns.net";  // URL for Blynk Cloud Server
int port = 8441;

#define BLYNK_DEFAULT_FINGERPRINT "6e xx xx xxx xx xx 67"  

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

byte fechad_portz = 12;
byte luz_1 = 13;
int TogreUpTime;
int contConec = 0;

BlynkTimer timer;

void setup() {
  
  Serial.begin(115200);
  
  USE_SERIAL.setDebugOutput(true);
  pinMode(2, OUTPUT);

  WiFi.begin(ssid, pass);  // Non-blocking if no WiFi available
  //Blynk.config(auth, server, port);
  Blynk.config(auth, server, port, BLYNK_DEFAULT_FINGERPRINT);
  Blynk.connect();

  timer.setInterval(1000L, UpTime);
  timer.setInterval(10000L, myTimerEvent);

  //ArduinoOTA.setHostname("Loss of connection test");  // For OTA
  //ArduinoOTA.begin();  // For OTA
}

BLYNK_CONNECTED() {
  Serial.println("Cconnected");
  ReCnctCount = 0;
}

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 myTimerEvent() {  // Protecao para fechad_portaz, nao travar em aberto

if(digitalRead(fechad_portz) == LOW){
   digitalWrite(fechad_portz, HIGH);  
   Blynk.notify("Desprecione butao portaozinho!");
 }
 if(digitalRead(luz_1) == HIGH){  // status app luz_1
   Blynk.virtualWrite(V81, 255);
 }else{Blynk.virtualWrite(V81, 0);}

}

BLYNK_WRITE(V20) {  //Portaozinho  
  if (param.asInt() == 1) {
      digitalWrite(fechad_portz, LOW);
      delay(500);
      digitalWrite(fechad_portz, HIGH);
  }
}

BLYNK_WRITE(V21) {  //Luz_1  
  if (param.asInt() == 1) {
      digitalWrite(luz_1, LOW);
  }else{digitalWrite(luz_1, HIGH);}
  }
  
BLYNK_WRITE(V10){  // Update   
  if (param.asInt() == 1) {
      Blynk.disconnect();
      update_config();
  }
}

void  update_config(){

  //timer.restartTimer(pausa_receb_dados);  
 // timer.toggle(TogreUpTime);

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
  }
  Serial.println("Connected, updating");
  delay(1000);

    t_httpUpdate_return ret = ESPhttpUpdate.update("http://xxxxx.ddns.net/Garagem_1/garagem_1.bin");  //,"","6E xx xx 9D 4F");
          
    switch (ret) {
      case HTTP_UPDATE_FAILED:
        USE_SERIAL.printf("HTTP_UPDATE_FAILD Error (%d): %s", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
        timer.toggle(TogreUpTime);
        break;

      case HTTP_UPDATE_NO_UPDATES:
        USE_SERIAL.println("HTTP_UPDATE_NO_UPDATES");
        timer.toggle(TogreUpTime);
        break;

      case HTTP_UPDATE_OK:
        USE_SERIAL.println("HTTP_UPDATE_OK");
        break;
    }
     
}
//***********************************************************************  
/*  Serial.println("set float");
  float timeout = (millis() / 1000);

  Serial.println("entering while");
  while (Blynk.connect() == false) {
    if (((millis() / 1000) - timeout) > 10) {
      break;
    }
    */
void loop() {
  timer.run();
  //ArduinoOTA.handle();  // For OTA

  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
  }
}
0 Likes

#4

There was an issue with SimpleTimer and wifi on esp8266 - https://github.com/esp8266/Arduino/issues/2295

I have recently encountered this same issue with latest ESP core. I can suggest you to implement your own timer in the loop function and check if it solves the issue or try to apply hints from the post mentioned above.

0 Likes

#5

@bilbobg, Thank you.

0 Likes