Blynk.connected() in mainloop

I have been having issues with the connection of my Arduino to Blynk. Can I run the function Blynk.connected() in the mainloop or would this result in a high amount of requests to the Blynk server and subsequently the ban of my account? I couldn’t find how the connection status is determined by the function. Thank you!


void loop()
{
  if((millis() - reset_timer) >= 60000){
    Serial.println("Connection Timeout Reset");
    delay(4000);
    digitalWrite(3, LOW);
    Serial.println("This never happens!");
  }
  
  if(Blynk.connected() == true){
    Blynk.run();
    reset_timer = millis();
    Serial.println("Blynk Connected!");
  }
  else if(Blynk.connected() == false){
    Serial.println("Blynk Not Connected!");
    Blynk.connect();
  }
}

This is a bad idea, why don’t you just use a timer to check for connection?

Actually running this in the void loop() is fine, it responds with a basic boolean result and is a common method for initiating reconnection routines as needed. But there is more to it then simply checking for connection. Checking for connection and running reconnection repeatedly can get your device stuck in a loop while waiting for router reboot or something… and that is not good if you also want to run other control stuff, even when NOT connected. That is where Blynk.config() comes into play.

And running a delay() command is ALWAYS a BAD thing with a Blynk sketch… and that is where BlynkTimer comes into play.

Search this forum for reconnection routines to see how others do it.

I have a simplistic one here…

Your account doesn’t get banned… worst case it would have stopped accepting connections for a short time (a few - 24 hours??). However, Blynk has since made some changes last year that should basically prevent any such flooding anymore anyhow, at expense of void loop() cycles… kind of like a governor that slows things down when it gets too racy.

Hi Gunner,

thank you very much for your reply.

Please note that pin 3 is connected to the reset pin and used to reset my Arduino. So if this part of the code is executed, the delay statement doesn’t interfere with Blynk.

Based on your suggestions and searching the forum, I came up with the following code. Is there anything I could do better?

Is there any way to track the on- and offline time of the Arduino to get an idea how reliable it is?
I have considered tracking it on a microSD card with the Blynk.connected() method, but my Arduino Nano doesn’t have enough memory to do this at the same time with running Blynk and many of the pins are already in use for my ethernet module and a 433 MHz transmitter.

Muchas gracias!

//#define BLYNK_PRINT Serial

#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <RCSwitch.h>
#include <TimeLib.h>
#include <WidgetRTC.h>

char auth[] = "XXXXXXXXXXXXXXXXX";

BlynkTimer timer;

int connection_loss_count = 0;

#define W5100_CS  10

void connection_check(){
  if(Blynk.connected() == true){
    Serial.println("Blynk Connected!");
    connection_loss_count = 0;
  }
  else if(Blynk.connected() == false){
    connection_loss_count = connection_loss_count + 1;
    Serial.print("Blynk Not Connected ");
    Serial.println(connection_loss_count);
    Blynk.connect();
  
  if(connection_loss_count >= 6 && Blynk.connected() == false){
    Serial.println("Connection Timeout Reset");
    delay(4000);
    digitalWrite(3, LOW);
    Serial.println("This never happens!");
  }
}

void setup()
{
  digitalWrite(3, HIGH);
  pinMode(3, OUTPUT);
  Serial.begin(9600);
  Ethernet.begin(NULL);
  Blynk.config(auth);
  Blynk.connect();
  timer.setInterval(10000L, connection_check);
}

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

If you make a small change to the loop you can remove complexity.

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

You can also look at the connection time on the app to see when the last connection was made. If you have connection problems you can use the timer to count the uptime and send it to the serial.

I think this will probably lock the void loop() into another if not connected, reconnect loop that can prevent other background code from running, or at least efficiently, while awaiting said reconnection.

you are correct it needs Blynk.connect(1000l)

Thank you! I think I’m pretty satisfied with the complexity right now. And I’m worried setting Blynk.connect to 1000l couldn’t be sufficient to establish a connection.

I wish the app would provide more precise data on this. I already check the app for the last connection, but unfortunately, this doesn’t provide me with any info on how long the connection wasn’t working before it was finally able to reconnect. Tracking this over serial is a good idea, but I assume I would have to keep a computer or phone running for a week or so to track enough data.I’ll look into this, but I would also appreciate alternative suggestions. :slight_smile:

My example actually tracked connection attempt counts. You could easily do a little math and pass that info onto , SD card, EEPROM, array, etc. even Widget once connection was established.

Your post inspired me to look into EEPROM, which I had already forgotten about. I track the offline time now and write it to EEPROM every 12 hours or when my Arduino resets. In addition, the offline time is shown on a LCD in my Blynk app now. I’m also looking for ways on how to optimize my code to save memory (I’m using 75% rn). Thank you for your help!

//#define BLYNK_PRINT Serial

#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <RCSwitch.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <EEPROM.h>

char auth[] = "XXXXXXXXXXXXXXXXXX";

BlynkTimer timer;

int blynkRead1; // Computer 1 Bedroom
int blynkRead2; // Computer 1 Bedroom PW
int led_status = LOW;
int connection_loss_count = 0;

boolean disconnect_status = false;
unsigned long disconnect_timer_1 = 0;
unsigned long disconnect_timer_2 = 0;
unsigned long disconnect_time_diff = 0;
long total_downtime = 0;

boolean rcswitchstatus1 = false; // Computer 1 Bedroom
boolean rcswitchstatus2 = false; // Computer 1 Bedroom PW

#define W5100_CS  10

WidgetRTC rtc;
WidgetTerminal terminal(V0);
WidgetLCD lcd(V2);
RCSwitch mySwitch = RCSwitch();

BLYNK_WRITE(V4){
  blynkRead1 = param.asInt();
}
BLYNK_WRITE(V5){
  blynkRead2 = param.asInt();
}

BLYNK_CONNECTED() {
  rtc.begin();
}

void push_to_blynk(){
  // STATUS LED
  led_status = !led_status;
  if (led_status == 1){
    Blynk.virtualWrite(V1, 1023);
    }
  else{
    Blynk.virtualWrite(V1, 0);
    }

  // LCD
  lcd.print(0, 0, "Downtime:");
  lcd.print(0, 1, total_downtime); 
}

void switchit(char tristate[20]){
  mySwitch.sendTriState(tristate);
  mySwitch.sendTriState(tristate);
  mySwitch.sendTriState(tristate);
  mySwitch.sendTriState(tristate);
}

void starttime(char event[20]){
  terminal.print(year());
  terminal.print("/");
  if (month() < 10){
    terminal.print("0");
  }
  terminal.print(month());
  terminal.print("/");
  if (day() < 10){
    terminal.print("0");
  }
  terminal.print(day());
  terminal.print(" ");
  if (hour() < 10){
    terminal.print("0");
  }
  terminal.print((hour()));
  terminal.print(":");
  if (minute() < 10){
    terminal.print("0");
  }
  terminal.print(minute());
  terminal.print(":");
  if (second() < 10){
    terminal.print("0");
  }
  terminal.print(second());
  terminal.println(event);
  terminal.flush();
}

void start_tracking(){
  if(disconnect_status == false){
      disconnect_timer_1 = millis();
      disconnect_status = true;
    }
}

void stop_tracking(){
  if(disconnect_status == true){
      disconnect_timer_2 = millis();
      disconnect_time_diff = disconnect_timer_2 - disconnect_timer_1;
      total_downtime = total_downtime + disconnect_time_diff;
      disconnect_status = false;
    }
}

void connection_check(){
  if(Blynk.connected() == true){
    stop_tracking();
    Serial.println("Blynk Connected!");
    connection_loss_count = 0;
  }
  
  else if(Blynk.connected() == false){
    start_tracking();
    connection_loss_count = connection_loss_count + 1;
    Serial.print("Blynk Not Connected ");
    //Serial.println(connection_loss_count);
    Blynk.connect();
  }
  
  if(connection_loss_count >= 6 && Blynk.connected() == false){
    stop_tracking();
    EEPROM.put(0, total_downtime);
    Serial.println("Timeout Reset");
    delay(4000);
    digitalWrite(3, LOW);
    Serial.println("Never happens");
  }
  
}

void write_eeprom(){
  EEPROM.put(0, total_downtime);
  Serial.println("EEPROM Write");
}

void setup()
{
  digitalWrite(3, HIGH);
  pinMode(3, OUTPUT);
  EEPROM.get(0, total_downtime);
  start_tracking();
  mySwitch.enableTransmit(6);
  mySwitch.setPulseLength(309);
  Serial.begin(9600);
  Ethernet.begin(NULL);
  Blynk.config(auth);
  Blynk.connect();
  timer.setInterval(10000L, connection_check);
  timer.setInterval(1000L, push_to_blynk);
  timer.setInterval(43200000L, write_eeprom);
}

void loop(){
  timer.run();
  if(Blynk.connected() == true){
    Blynk.run();
  }
  
  if (blynkRead1 == 1 && rcswitchstatus1 == false){
    starttime(" Computer 1 ON");
    switchit("0FF0FF0FFF0F");
    rcswitchstatus1 = true;
  }
  if (blynkRead1 == 0 && rcswitchstatus1 == true){
    starttime(" Computer 1 OFF");
    switchit("0FF0FF0FFFF0");
    rcswitchstatus1 = false;
  }
  if (blynkRead2 == 1 && rcswitchstatus2 == false){
    starttime(" Computer 1 PW ENTERED");
    mySwitch.send(XXXXXXXXXX, 24);
    mySwitch.send(XXXXXXXXXX, 24);
    rcswitchstatus2 = true;
  }
  if (blynkRead2 == 0 && rcswitchstatus2 == true){
    rcswitchstatus2 = false;
  }
}

it would not matter if it did not connect, The code would get a chance to run once every second and the connection would be reattempted each time that it was found to be not connected.

Enjoy debugging.