Make sketch`s work more faster when no internet connection with Blynk

Hi everybody.

I noticed that when no Internet connection, blink always makes connection requests and because of this, all the code begins to execute with huge delays.

Specifically, my problem is that in addition to the blink buttons my smart home is controlled by 433Mhz remotes for greater convenience, who also begin to work as broken without connection.

How can I make execution of the rest of the code faster when there is no internet connection?

Maybe I can use "while (Blynk.connect() == false) { } " or something else?

Thanks in advance.

You can use this: http://docs.blynk.cc/#blynk-firmware-connection-management-blynkconnected

for example to check every 5 seconds if Blynk is still connected and act accordingly. I also though it was possible to set the timeout for the heartbeats, but it’s been a while since I’ve done anything with Blynk to be honest (lack of time and will …).

2 Likes

Hi @Pacer

Depending on you need for speed… maybe you could use
Blynk.connect(timeout)

  • with a suitable timeout (in milliseconds) ?

Then you don’t have that long a timeout (I saw mentioned somwhere that the default connect-timeout is 30 seconds).

You can also use full manual connection management as shown here: https://github.com/blynkkk/blynk-library/tree/master/examples/More/ArduinoClient

I tried Blynk.connect(timeout).
Nothing good for speed. The system began to hang more than without it.

Tried to put bool 123= Blynk.connect(60000); on start of a code- then my D1 begins
to restart every 1second.

When exactly I can put this function in code?

With that you’ll have 60 seconds delay - instead of 30 seconds.
I was thinking more like 3000 milliseconds…

Just in you normal while(Blynk.connect(3000)==false ){
…the code you want it to do while not connected - will the be executed every 3rd second.
}

This is provided that you can connect with in 3 seconds under normal circumstances.

3 secs is long .
ok. And what’ll be with executed code if I print Blynk.disconnect ();?
Are the code will be executed without any delays?

I was tried other variation:

{bool 123=Blynk.connect();
if (!Blynk.connect)
{Blynk.disconnect ();}
timer.setInterval (60000, nointernet);```

But this not worked too...

As in too long?
Or long enough?

So as I understand, you want to have a function that every 60 seconds test if network/Blynk is back, and in the mean time executes some code ?

Is it the same code it’s supposed to execute both while connected and disconnected ?

1 Like

Too long.
Exactly, I want to execute faster the same code while connected and disconnected.

Could you please provide the code, in an orderly fashion? :slight_smile:

#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>

#define DHTPIN 4
#define DHTTYPE DHT11 



long unsigned int lowIn; 
long unsigned int pause = 30000;
bool lockLow = true;
bool takeLowTime;

bool isFirstConnect = true;


long unsigned int lowIn2; 
long unsigned int pause2 = 30000;
bool lockLow2 = true;
bool takeLowTime2;

bool needlightstate= false;







const int doorprihoj = 14; 
const int pirprihoj = 12;    
const int pir2prihoj = 16;
const int releprihoj = 3;   

const int pir1kuh = 2;
const int pir2kuh = 0;
const int lightkuh = 5;
const int relekuh = 1;


WidgetBridge bridge1(V8);

int timer1;
int timer2;
int timer3;
int timer4;
int nointernettimer;
int timer5;
int splittimer1;
int splittimer2;
int splittimer3;

int autoprihoj =0;
int autokuh=0;
int ohrana=0;


char auth[] = "AAAAAAAAAAAAAAAAA";

char ssid[] = "AAAAAA";
char pass[] = "AAAAA";


SimpleTimer timer;
DHT dht(DHTPIN, DHTTYPE);



 
  

void sendUptime()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  t = ((int) (t * 10) / 10.0);
  h = ((int) (h * 10) / 10.0);
  
  Blynk.virtualWrite(8, t); // virtual pin
  Blynk.virtualWrite(9, h); // virtual pin
  bridge1.virtualWrite (V26, t);

  

}


void autolightprihoj()
{

if (autoprihoj==0)
  { 




   
 int door=digitalRead(doorprihoj);
  
  
  if(digitalRead(pirprihoj)==HIGH||door==0||digitalRead(pir2prihoj)==HIGH)
  {
    
    if(lockLow)
    {
      lockLow = false;     
      
      digitalWrite(3, LOW);
      Blynk.virtualWrite (V0, LOW);
      
    }        
    takeLowTime = true;
  }

  
  if(digitalRead(pirprihoj)==LOW&&door==1&&digitalRead(pir2prihoj)==LOW)
  {      
    
    if(takeLowTime)
    {
      lowIn = millis();          
      takeLowTime = false;       
    }
   
    if(!lockLow && millis() - lowIn > pause)
    { 
     
      lockLow = true;               
      
      digitalWrite (3, HIGH);
      Blynk.virtualWrite (V0, HIGH);
      
    }

  }
    
       
  }}

void autolightkuh ()
{
 if (autokuh==0)
  { bool needlight= (digitalRead(lightkuh)==HIGH&&digitalRead(pir1kuh)==HIGH)||(digitalRead(lightkuh)==HIGH&&digitalRead(pir2kuh)==HIGH);
    if (needlight)
     {digitalWrite(1, LOW);
      needlightstate = true;

      Blynk.virtualWrite (V3, LOW);
     }
  

      if (needlightstate==true){
    
    if (digitalRead(pir1kuh)==HIGH||digitalRead(pir2kuh)==HIGH)
  {
    
    if(lockLow2)
    {
      lockLow2 = false;     
      
      digitalWrite(1, LOW);
      Blynk.virtualWrite (V3, LOW);
    }        
    takeLowTime2 = true;
  }

  
  if(digitalRead(pir1kuh)==LOW&&digitalRead(pir2kuh)==LOW)
  {      
    
    if(takeLowTime2)
    {
      lowIn2 = millis();         
      takeLowTime2 = false;       
    }
    
    if(!lockLow2 &&( millis() - lowIn2 > pause2))
    { 
      
      lockLow2 = true;               
      
     
      digitalWrite (1, HIGH);
      Blynk.virtualWrite (V3, HIGH);
      
      needlightstate= false;
    
     }
   
    }}
   
    
    }
  
}

 
 




BLYNK_CONNECTED() {
  
 bridge1.setAuthToken("AAAAAAAA");}
 
  if (isFirstConnect) {
    // Request Blynk server to re-send latest values for all pins
    Blynk.syncAll();

    // You can also update individual virtual pins like this:
    //Blynk.syncVirtual(V0, V1, V4);

    isFirstConnect = false;
  }


 


void sendnotify ()
{ 
  if (ohrana==0){
  Blynk.virtualWrite (V11, 0);
   if(digitalRead(pir1kuh)==HIGH||digitalRead(pir2kuh)==HIGH)
  {
  Blynk.virtualWrite (V10, 255);}

 if(digitalRead(pir1kuh)==LOW&&digitalRead(pir2kuh)==LOW)
  {Blynk.virtualWrite (V10, 0);}
  
  
  if (digitalRead(doorprihoj)==0)
 {
   Blynk.virtualWrite (V4, 255);}

if (digitalRead(doorprihoj)==1)
 { 
   Blynk.virtualWrite (V4, 0);}
  
   if(digitalRead(pirprihoj)==HIGH||digitalRead(pir2prihoj)==HIGH)
  { 
   
  Blynk.virtualWrite (V7, 255);}
  
  if(digitalRead(pirprihoj)==LOW&&digitalRead(pir2prihoj)==LOW)
  {Blynk.virtualWrite (V7, 0); 
      }}     
      
   if (ohrana==1){
  Blynk.virtualWrite (V11, 255);
   if(digitalRead(pir1kuh)==HIGH||digitalRead(pir2kuh)==HIGH)
  {Blynk.notify ("Движение на кухне");
  Blynk.virtualWrite (V10, 255);}

 if(digitalRead(pir1kuh)==LOW&&digitalRead(pir2kuh)==LOW)
  {Blynk.virtualWrite (V10, 0);}
  
  
  if (digitalRead(doorprihoj)==0)
 {Blynk.notify ("Входная Дверь открыта"); 
   Blynk.virtualWrite (V4, 255);}

if (digitalRead(doorprihoj)==1)
 {
   Blynk.virtualWrite (V4, 0);}
  
   if(digitalRead(pirprihoj)==HIGH||digitalRead(pir2prihoj)==HIGH)
  { 
  Blynk.notify ("Движение в прихожей"); 
  Blynk.virtualWrite (V7, 255);}
  
  if(digitalRead(pirprihoj)==LOW&&digitalRead(pir2prihoj)==LOW)
  {Blynk.virtualWrite (V7, 0); 
      }}   
      
      }

  
void setup()
{
  
  
  
  timer4= timer.setInterval (1000, autolightkuh);
  
  timer3= timer.setInterval (1000, autolightprihoj);
  
  timer2= timer.setInterval(5000, sendnotify);
  
  timer1= timer.setInterval(10000, sendUptime);
  
  
  Serial.begin(9600); 
  pinMode (pirprihoj,INPUT);    
  pinMode (pir2prihoj,INPUT); 
  pinMode (releprihoj,OUTPUT);   
  pinMode (doorprihoj,INPUT);
  pinMode (pir1kuh,INPUT);
  pinMode (pir2kuh,INPUT);
  pinMode (lightkuh,INPUT);
  pinMode (relekuh,OUTPUT);
  dht.begin();
  digitalWrite (releprihoj, HIGH);
  digitalWrite (relekuh, HIGH);
  Blynk.begin(auth, ssid, pass);
}


 BLYNK_WRITE(V0) 
  {if(param.asInt()==0)
{ if (autoprihoj==1) {digitalWrite(releprihoj, LOW);}
if (autoprihoj==0) {
 if (digitalRead (releprihoj)==LOW)
 {Blynk.virtualWrite (V0, LOW);}
 if (digitalRead (releprihoj)==HIGH)
 {Blynk.virtualWrite (V0, HIGH);}}}
  
else { if (autoprihoj==1) {digitalWrite(releprihoj, HIGH);}
if (autoprihoj==0) {
 if (digitalRead (releprihoj)==LOW)
 {Blynk.virtualWrite (V0, LOW);}
 if (digitalRead (releprihoj)==HIGH)
 {Blynk.virtualWrite (V0, HIGH);}}}}

 
 
 





 BLYNK_WRITE(V3) 
  {if(param.asInt()==0)
{ if (autokuh==1) {digitalWrite(relekuh, LOW);}
if (autokuh==0) {
 if (digitalRead (relekuh)==LOW)
 {Blynk.virtualWrite (V3, LOW);}
 if (digitalRead (relekuh)==HIGH)
 {Blynk.virtualWrite (V3, HIGH);}}}
  
else { if (autokuh==1) {digitalWrite(relekuh, HIGH);}
if (autokuh==0) {
 if (digitalRead (relekuh)==LOW)
 {Blynk.virtualWrite (V3, LOW);}
 if (digitalRead (relekuh)==HIGH)
 {Blynk.virtualWrite (V3, HIGH);}}}}

  BLYNK_WRITE(V1) 
 {if(param.asInt()==0)
{ autoprihoj=0;
  }
else {autoprihoj=1;}}

 BLYNK_WRITE(V2) 
 {if(param.asInt()==0)
{ autokuh=0;
  }
else {autokuh=1;}}


BLYNK_WRITE(V17)
 {if(param.asInt()==0)
 {ohrana=1;

 Blynk.notify ("ОХРАНА АКТИВИРОВАНА"); 
 Blynk.virtualWrite (V11, 255);}

 
 else {ohrana=0;

 Blynk.notify ("ОХРАНА ОТКЛЮЧЕНА");
 Blynk.virtualWrite (V11, 0);}}


void loop()
{

   Blynk.run();
  timer.run();
}