Blynk.begin enters endless loop

Hello everyone

I did a door security project to use rfid and esp8266 Nodemcu . My code is good working but i have a problem.

My problem:

If there is no internet connection and esp has just started, the code goes into an infinite loop within the Blynk.begin function. But if there is no connection, I want it to continue offline and only try to connect to the internet every 20 seconds.

My code:

void setup() {
  Serial.begin(9600);  // Seri haberle┼čmeyi ba┼člat─▒yoruz.                                         
  SPI.begin();         // Spi haberle┼čme ba┼člat─▒ld─▒.                                       
  mfrc522.PCD_Init();  // Rfid mod├╝l ba┼člat─▒ld─▒.
  // r├Âle,ledler,ve buton i├žin giri┼č ├ž─▒k─▒┼č atand─▒.

  pinMode(relay,OUTPUT);
  pinMode(green,OUTPUT);
  pinMode(red,OUTPUT);
  pinMode(yellow,OUTPUT);
  pinMode(button,INPUT);
  Blynk.begin(auth,ssid,pass);  //Blynk ba┼člat─▒ld─▒.
}

i am so sorry for my bad english

@Musakucuk please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Pete.

#include <SPI.h>

#include <MFRC522.h>

#include <WiFiClient.h>

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>

#include <BlynkSimpleEsp8266.h>

//***** KABLOSUZ AYARLARI ******* 

char auth[] = "auth";

const char* ssid = "wifiname";

const char* pass = "pass";

//*********IFTTT****************

const char* server = "maker.ifttt.com";

// Ki┼čiye ├Âzel IFTTT Url'i

const char* resource = "/trigger/logger/with/key/auth";

//*********** RFID **************

#define RST_PIN         D8           

#define SS_PIN          D4          

#define relay           D3

#define button          D8

MFRC522 mfrc522(SS_PIN, RST_PIN);   

String readCardId = ""; // okunan kart idsi

String tempid = "";     // de─či┼čken kart idsi

//******** Led Pins and Button Pin *************

#define green           D1

#define red             D0

#define yellow          D2

unsigned long lastTime = 0;

unsigned long lastTime1 = 0;

unsigned long lastTime2 = 0;

unsigned long delayTime = 10000; 

unsigned long delayTime1 = 2000;  

unsigned long delayTime2 = 10000;

bool find = false;  

WiFiClient client;

//***** ANAHTAR L─░STES─░ ***********

String Key_list[11][2] =          // Anahtar Listesi

  {

  {"name","cardid"},

  {"name","cardid"},

  {"name", "cardid"},

  {"name","cardid"},

  {"name","cardid"},

  {"name","cardid"},

  {"name","cardid"},

  {"name","cardid"},

  {"name","cardid"},

  {"name","cardid"},

  {"name","cardid"}

  };

// Google tablolar ile ba─člant─▒ sa─člayan fonksiyon 

void connectGoogleSheets()

{

  int retries = 8;

  while(!!!client.connect(server, 80) && (retries-- > 0)) {

    Serial.print(".");

  }

} 

// IFTTT web servisine bir http iste─či g├Ânderir.

void makeIFTTTRequest(String name_surname,String kart_id,String durum) {

  digitalWrite(red,HIGH);

  Serial.print("Connecting to "); 

  Serial.print(server);

  

  int retries = 5;

  while(!!!client.connect(server, 80) && (retries-- > 0)) {

    Serial.print(".");

  }

  Serial.println();

  if(!!!client.connected()) {

    Serial.println("Failed to connect...");

  }

  

  Serial.print("Request resource: "); 

  Serial.println(resource);

  

  String jsonObject = String("{\"value1\":\"") + name_surname + "\",\"value2\":\"" + kart_id

                      + "\",\"value3\":\"" + durum + "\"}";

                                  

  client.println(String("POST ") + resource + " HTTP/1.1");

  client.println(String("Host: ") + server); 

  client.println("Connection: close\r\nContent-Type: application/json");

  client.print("Content-Length: ");

  client.println(jsonObject.length());

  client.println();

  client.println(jsonObject);

        

  int timeout = 5 * 10; // 5 seconds             

  while(!!!client.available() && (timeout-- > 0)){

    delay(100);

  }

  if(!!!client.available()) {

    Serial.println("No response...");

  }

  while(client.available()){

    Serial.write(client.read());

  }

  

  Serial.println("\nclosing connection");

  client.stop(); 

  digitalWrite(red,LOW);

}

String getID()  

{

  digitalWrite(green,HIGH);

  delay(200);

  readCardId = "";

  if(!mfrc522.PICC_IsNewCardPresent())

    {

      return "";

    }

    if(!mfrc522.PICC_ReadCardSerial())

    {

      return "";

    }

    for(int i = 0; i < mfrc522.uid.size;i++)

    {

      readCardId += String(mfrc522.uid.uidByte[i],HEX);

    }

    Serial.println(readCardId);

    digitalWrite(green,LOW);

    return readCardId;

}

void openDoor()   // Kap─▒ a├ž metodu kap─▒ otomati─čini 200 milisaniye a├ž─▒yor ve kapat─▒yor.

{

  digitalWrite(relay,HIGH);

  delay(200);

  digitalWrite(relay,LOW);

}

void decision()   //Karar fonksiyonu ile gelen kart id'nin karar─▒ verilir.

{

  

  for(int count=0;count<11;count++)

    {

      if(Key_list[count][1] == tempid)

      {

        digitalWrite(red,HIGH);

        digitalWrite(green,LOW);

        

        Serial.print(Key_list[count][0]);

       // Serial.println(" Adli kisi giris yapti.");

        openDoor();

        if(Blynk.connected()) makeIFTTTRequest(Key_list[count][0],Key_list[count][1],"A├ž─▒ld─▒.");

        

        digitalWrite(red,LOW);

        digitalWrite(green,HIGH);

        tempid = "";

        break;

      }

    }

     if(tempid != "")

      {

        if(Blynk.connected()) makeIFTTTRequest("Bilinmeyen Kullanici",tempid,"A├ž─▒lmad─▒.");

        digitalWrite(red,HIGH);

        delay(250);

        digitalWrite(red,LOW);

      }

}

BLYNK_WRITE(V2) 

{

  digitalWrite(green,LOW);

  int pinData = param.asInt(); 

  if(pinData == 1){

    openDoor();

    makeIFTTTRequest("Google Asistan","Kart─▒z","A├ž─▒ld─▒.");

  }

  digitalWrite(green,HIGH);

}

void setup() {

  Serial.begin(9600);  // Seri haberle┼čmeyi ba┼člat─▒yoruz.                                         

  SPI.begin();         // Spi haberle┼čme ba┼člat─▒ld─▒.                                       

  mfrc522.PCD_Init();  // Rfid mod├╝l ba┼člat─▒ld─▒.

  

  // r├Âle,ledler,ve buton i├žin giri┼č ├ž─▒k─▒┼č atand─▒.

  pinMode(relay,OUTPUT);

  pinMode(green,OUTPUT);

  pinMode(red,OUTPUT);

  pinMode(yellow,OUTPUT);

  pinMode(button,INPUT);

  Blynk.begin(auth,ssid,pass);  //Blynk ba┼člat─▒ld─▒.

      

}

void loop() {

  unsigned long nowTime = millis(); 

  unsigned long nowTime1 = millis();

  unsigned long nowTime2 = millis();

  

  if (nowTime - lastTime >= delayTime) 

  {

    lastTime = nowTime;    

    if(WiFi.status() != WL_CONNECTED) digitalWrite(yellow,LOW);

    else digitalWrite(yellow,HIGH);

  }

  if(nowTime1 - lastTime1 >= delayTime1)

  {

    lastTime1 = nowTime1;

    if(digitalRead(button) == true){

      digitalWrite(red,HIGH);

      Blynk.notify("Bina i├žersindeki kap─▒dan bir ki┼či kap─▒ a├žma iste─či g├Ânderdi."); 

    }

    else digitalWrite(red,LOW);

  }

  if(nowTime2 - lastTime2 >= delayTime2)  // 30 dakikada bir google tablolar ile ba─člant─▒ sa─član─▒yor.

  {

    lastTime2 = nowTime2;

    connectGoogleSheets();

  }

  

  tempid = getID(); //kart ├╝zerinde okunan kart id tempid de─či┼čkenine at─▒l─▒r.

  decision();  // karar metodu ile kap─▒n─▒n a├ž─▒l─▒p a├ž─▒lmayaca─č─▒ karar─▒ verilir.

  

  Blynk.run();
}

My full codes

Blynk.begin is a blocking function.
If it canÔÇÖt establish a WiFi connection or a connection to the Blynk server then it will block all code execution beyond that point.

The solution is to change your sketch so that you manage your own WiFi connection, then use Blynk.config and Blynk.connect to try to establish the Blynk connection. if either of these connections cannot be established then you continue in ÔÇťstandaloneÔÇŁ mode and probably re-try the WiFi, and if successful the Blynk connections, after a a suitable amount of time has passed.

If you search the forum for Blynk.config you will see this issue discussed in detail.

Also, IÔÇÖd recommend that you try to clean your void loop up as much as possible. I realise that this is difficult when using RFID readers, but you should at least be using a timer rather than doing all of the millis comparisons.

Pete.

Thank you very much for your response, Pete.
But the Blynk.config topics in the forum are very complex. I wonder if there is a quick source where I can reach results

Hello in the void setup replace Blynk.begin with

Blynk.config (wifi, auth);
Blynk.connectWiFi (ssid, pass);

in this way it does not block