How can start a code with no internet?

How can I make a code using Blynk so that if there is an internet connection to the Blynk server, and if there is no internet, it completes the code without trying to run Blynk because when it tries to run the server it makes the code not work.

this is my code the problem when i have internet it’s work good but when dissconnect the internet it’s show to me in serial

( connected to blynk.cloud : 80 )
again and again

#define BLYNK_TEMPLATE_ID "XXX"
    #define BLYNK_DEVICE_NAME "XXX"
    #define BLYNK_AUTH_TOKEN "XXX"
    #define BLYNK_PRINT Serial
    #include <ESP8266WiFi.h>
    #include <BlynkSimpleEsp8266.h>
     #include <SPI.h>
    #include <MFRC522.h>
    #include <Servo.h>
    const char* NAME;
    const char* WHO;
    const char* ID;

    char auth[] = BLYNK_AUTH_TOKEN;

    String Event_Name = "XXX";

    String Key = "cCkHZ..................";

    // Replace with your unique IFTTT URL resource
    String resource = "/trigger/" + Event_Name + "/with/key/" + Key;

    // Maker Webhooks IFTTT
    const char* servers = "maker.ifttt.com";

    char ssid[] = "XXX";  // type your wifi name
    char pass[] = "XXXX";  // type your wifi password
    #define SS_PIN 4
    #define RST_PIN 5
    #define LED_G 16 //define green LED pin
    #define LED_R 9 //define red LED
    #define BUZZER 15 //buzzer pin
    #define Button 0
    #define SERVO_PIN 2
    Servo myservo;
    #define ACCESS_DELAY 2000
    #define DENIED_DELAY 1000

    MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
    int angle =0;
    int angle2=0;
    int counter = 0;
    int fflag = 0; 
    int eflag = 0; 
    int jflag = 0;

    BlynkTimer mytimerobject;



    void setup(){
       Serial.begin(115200);
      Blynk.begin(auth, ssid, pass);
      SPI.begin(); 
      mfrc522.PCD_Init();
       pinMode(LED_G, OUTPUT);
      pinMode(LED_R, OUTPUT);
      digitalWrite(LED_R, LOW);
      digitalWrite(LED_G, LOW);
      pinMode(0, INPUT);
      pinMode(BUZZER, OUTPUT);
      noTone(BUZZER);
    }


    void makeIFTTTRequest() {
      Serial.print("Connecting to ");
      Serial.print(servers);

      WiFiClient client;
      int retries = 5;
      while (!!!client.connect(servers, 80) && (retries-- > 0)) {
        Serial.print(".");
      }
      Serial.println();
      if (!!!client.connected()) {
        //Serial.println("Failed to connect...");
      }

      //Serial.print("Request resource: ");
     // Serial.println(resource);

      // Temperature in Celsius
      String jsonObject = String("{\"value1\":\"") + NAME + "\",\"value2\":\"" + WHO + "\",\"value3\":\"" + ID + "\"}";  // if you want add NEW VALUE add |( + "\",\"value3\":\"" + NEW value + )| 

      client.println(String("POST ") + resource + " HTTP/1.1");
      client.println(String("Host: ") + servers);
      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();
    }



        void exitbutton(){
        
      int buttonState;  
      buttonState = digitalRead(Button);
    if (buttonState == LOW) // light the LED
      {
       myservo.attach (2);
        myservo.write(angle);
        for (angle =0 ; angle <180; angle +=10)
        {
       myservo.write(angle); //servo start position
       delay(150); //speed time to open
       
        }

       delay(10000);
       for(angle = 180; angle>=0; angle -=10)
        {
          myservo.write(angle);
        delay(150); // speed time to close
        }
        myservo.detach();
      }
      }
    void Open()
    {
       //digitalWrite(LED_R, LOW);
       digitalWrite(LED_G, HIGH);
         tone(BUZZER, 500);
        delay(500);
        noTone(BUZZER);
           myservo.attach(SERVO_PIN);
              myservo.write(0);
         
    for (angle =0 ; angle <180; angle +=10)
        {
       myservo.write(angle); //servo start position
       delay(150); //speed time to open
       
        }

       delay(10000);
       for(angle = 180; angle>=0; angle -=10)
        {
          myservo.write(angle);
        delay(150); // speed time to close
        }
        digitalWrite(LED_G, LOW);
        myservo.detach();
    }
    void NotAuthorized()
    {
        Serial.println(" Not Authorized to Enter ");
       digitalWrite(LED_R, HIGH);
       tone(BUZZER, 300);
        delay(1500);
        digitalWrite(LED_R, LOW);
        noTone(BUZZER);
        delay(DENIED_DELAY);
    }
  
    void loop(){
                                                                                      //_________if there is NO internet________
       // attempt to connect to Wifi network:
      
      while ( WiFi.status() != WL_CONNECTED) {  
      exitbutton();
        // Look for new cards
        if (  (! mfrc522.PICC_IsNewCardPresent()) )
      {
        return;
      }
      // Select one of the cards
      if (  (! mfrc522.PICC_ReadCardSerial()))
      {
        return;
      }
      //Show UID on serial monitor
      Serial.print("UID tag :");
      String content= "";
      byte letter;
      for (byte i = 0; i < mfrc522.uid.size; i++)
      {
         Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
         Serial.print(mfrc522.uid.uidByte[i], HEX);
         content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
         content.concat(String(mfrc522.uid.uidByte[i], HEX));
      }
      Serial.println();
      Serial.print("Message : ");
      content.toUpperCase(); 
      

    //change here the ( student's )UID of the card_________________
      if (  (content.substring(1) == "04 CE 6F 15 19 6C 80")  ||   //the first card for Abdulrahman_201712803
            (content.substring(1) == "04 F2 12 14 19 6C 80")  ||   // the second card for Ibrahim_201716556
            (content.substring(1) == "04 BF 1E 10 7A 74 80")  ||   // the third card for Mohammed_201716783
             (content.substring(1) == "04 23 ED 15 19 6C 80") ||   // the fourth card for Khalid_201716958
             (content.substring(1) == "04 DB 1E FC 6A 74 80") ||   // the fifth card for Fahad_201716884
             (content.substring(1) == "04 8B FB 14 19 6C 80")    // the six card for Ahmad_201716229
             
             ) 
      {
        
        //the first card
        if ( (content.substring(1) == "04 CE 6F 15 19 6C 80") ){
        Serial.println("The student Enter:");
        Serial.println("Abdulrahman_201712803");

        }
        // the second card
        else if ( (content.substring(1) == "04 F2 12 14 19 6C 80") ){
        Serial.println("The student Enter:");
        Serial.println("Ibrahim_201716556");

        }
        // the third card
        else if ( (content.substring(1) == "04 BF 1E 10 7A 74 80") ){
        Serial.println("The student Enter:");
        Serial.println("Mohammed_201716783");

        }
        // the fourth card
        else if ( (content.substring(1) == "04 23 ED 15 19 6C 80") ){
        Serial.println("The student Enter:");
        Serial.println("Khalid_201716958");

        }
        // the fifth card
        else if ( (content.substring(1) == "04 DB 1E FC 6A 74 80") ){
        Serial.println("The student Enter:");
        Serial.println("Fahad_201716884");
        }
        // the six card
        else if ( (content.substring(1) == "04 8B FB 14 19 6C 80") ){
        Serial.println("The student Enter:");
        Serial.println("Ahmad_201716229");

        }
        Serial.println();
        
      Open();

     
      }
      //change here the ( Doctors's )UID of the card_________________
      else if (  (content.substring(1) == "C9 F4 CA B1")  ||  // the first card for Dr.Abdullah Albaker
                 (content.substring(1) == "C9 F4 CA B2")    // the second card for Dr.Abdulaziz almalaq       
             ){
              //the first card
              if (content.substring(1) == "C9 F4 CA B1"){
              Serial.println("The Doctor Enter:");
              Serial.println("Dr.Abdullah Albaker");
        }
             //the second card
             else if (content.substring(1) == "C9 F4 CA B2"){
                Serial.println("The Doctor Enter:");
                Serial.println("Dr.Abdulaziz almalaq");
        }
               
                Serial.println();
                     Open();
              
              }
    //change here the ( Head of Department )UID of the card_________________
       else if (  (content.substring(1) == "E9 CB BC B2") ) //change here the UID of the card
      {
        Serial.println("Head of Department Enter");
        Serial.println("Dr.Ahmad saud");

        
        Serial.println();
        Open();

      }  
     
     else   {
     NotAuthorized();
      } 


        // wait 10 seconds for connection:
        delay(1000);
      }
      
                                                                                       //_________if there is internet________
       while ( WiFi.status() == WL_CONNECTED) {
      mytimerobject.run();
       Blynk.run();
      // check the network status connection once every 1 seconds:
      delay(1000);
      Bexitbutton();
        // Look for new cards
        if (  (! mfrc522.PICC_IsNewCardPresent()) )
      {
        return;
      }
      // Select one of the cards
      if (  (! mfrc522.PICC_ReadCardSerial()))
      {
        return;
      }
      //Show UID on serial monitor
      Serial.print("UID tag :");
      String content= "";
      byte letter;
      for (byte i = 0; i < mfrc522.uid.size; i++)
      {
         Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
         Serial.print(mfrc522.uid.uidByte[i], HEX);
         content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
         content.concat(String(mfrc522.uid.uidByte[i], HEX));
      }
      Serial.println();
      Serial.print("Message : ");
      content.toUpperCase(); 

    //change here the ( Student's )UID of the card_________________
      if ( ( (content.substring(1) == "04 CE 6F 15 19 6C 80") ||     //the first card for Abdulrahman_201712803 
             (content.substring(1) == "04 F2 12 14 19 6C 80") ||     //the second card for Ibrahim_201716556
             (content.substring(1) == "04 BF 1E 10 7A 74 80") ||     //the third card for Mohammed_201716783
             (content.substring(1) == "04 23 ED 15 19 6C 80") ||     //the fourth  card for Khalid_201716958
             (content.substring(1) == "04 DB 1E FC 6A 74 80") ||     //the fifth card for Fahad_201716884
             (content.substring(1) == "04 8B FB 14 19 6C 80")      //the six card for Ahmad_201716229
             ) && (fflag== 1)) 
      {
        //the first card
        if ( (content.substring(1) == "04 CE 6F 15 19 6C 80") && (fflag== 1)){
        Serial.println("The student Enter:");
        Serial.println("Abdulrahman_201712803");
        Blynk.virtualWrite(V2, "The student Enter:" );
        Blynk.virtualWrite(V2, "Abdulrahman_201712803" );
        NAME = "Abdulrahman Khalif ALshammary";
        WHO = "Student";
        ID = "201712803";
        Open();
        makeIFTTTRequest();
        }
        // the second card
        else if ( (content.substring(1) == "04 F2 12 14 19 6C 80") && (fflag== 1)){
        Serial.println("The student Enter:");
        Serial.println("Ibrahim_201716556");
        Blynk.virtualWrite(V2, "The student Enter:" );
        Blynk.virtualWrite(V2, "Ibrahim_201716556" );
        NAME = "Ibrahim Muflih Alruwaythi";
        WHO = "Student";
        ID = "201716556";
        Open();
        makeIFTTTRequest();
        }
    // the third card
        else if ( (content.substring(1) == "04 BF 1E 10 7A 74 80") && (fflag== 1)){
        Serial.println("The student Enter:");
        Serial.println("Mohammed_201716783");
        Blynk.virtualWrite(V2, "The student Enter:" );
        Blynk.virtualWrite(V2, "Mohammed_201716783" );
        NAME = "Mohammed";
        WHO = "Student";
        ID = "201716783";
        Open();
        makeIFTTTRequest();
        }
        // the fourth card
        else if ( (content.substring(1) == "04 23 ED 15 19 6C 80") && (fflag== 1)){
        Serial.println("The student Enter:");
        Serial.println("Khalid_201716958");
        Blynk.virtualWrite(V2, "The student Enter:" );
        Blynk.virtualWrite(V2, "Khalid_201716958" );
        NAME = "Khalid";
        WHO = "Student";
        ID = "201716958";
        Open();
        makeIFTTTRequest();
        }
        // the fifth card
        else if ( (content.substring(1) == "04 DB 1E FC 6A 74 80") && (fflag== 1)){
        Serial.println("The student Enter:");
        Serial.println("Fahad_201716884");
        Blynk.virtualWrite(V2, "The student Enter:" );
        Blynk.virtualWrite(V2, "Fahad_201716884");
        NAME = "Fahad";
        WHO = "Student";
        ID = "201716884";
        Open();
        makeIFTTTRequest();
        }
        // the six card
        else if ( (content.substring(1) == "04 8B FB 14 19 6C 80") && (fflag== 1)){
        Serial.println("The student Enter:");
        Serial.println("Ahmad_201716229");
        Blynk.virtualWrite(V2, "The student Enter:" );
        Blynk.virtualWrite(V2, "Ahmad_201716229" );
        NAME = "Ahmad";
        WHO = "Student";
        ID = "201716229";
        Open();
        makeIFTTTRequest();
        
        }
        
        Serial.println();
          

      }
    //change here the ( Doctors's )UID of the card_________________
      else if (  ((content.substring(1) == "C9 F4 CA B2")||        //the first card
                  (content.substring(1) == "C9 F4 CA B1")         //the second card
                  )&& (eflag== 1)) 
      {
        //the first card
        if ( (content.substring(1) == "C9 F4 CA B1") && (eflag== 1)){
        Serial.println("The Doctor Enter:");
        Serial.println("Dr.Abdullah Albaker");
        Blynk.virtualWrite(V2, "The Doctor Enter:" );
        Blynk.virtualWrite(V2, "Dr.Abdullah Albaker" );
        NAME = "Dr.Abdullah Albaker";
        WHO = "Faculty member";
        ID = "-";
        Open();
        makeIFTTTRequest();
        }
        //the second card
        else if ( (content.substring(1) == "C9 F4 CA B2") && (eflag== 1)){
        Serial.println("The Doctor Enter:");
        Serial.println("Dr.Abdulaziz almalaq");
        Blynk.virtualWrite(V2, "The Doctor Enter:" );
        Blynk.virtualWrite(V2, "Dr.Abdulaziz almalaq" );
        NAME = "Dr.Abdulaziz almalaq";
        WHO = "Faculty member";
        ID = "-";
        Open();
        makeIFTTTRequest();
        
        }
        
        Serial.println();    
       

      }
      //change here the ( Head of Department )UID of the card_________________
       else if (  (content.substring(1) == "E9 CB BC B2") ) //change here the UID of the card
      {
        Serial.println("Head of Department Enter");
        Serial.println("Dr.Ahmad saud");
        Blynk.virtualWrite(V2, "Head of Department Enter" );
        Blynk.virtualWrite(V2, "Dr.Ahmad saud" );
        Serial.println();
        NAME = "Dr.Ahmad saud";
        WHO = "Head of department";
        ID = "-";
        makeIFTTTRequest();
        Open();
      }
     else   {
      Blynk.virtualWrite(V2, "Some one (Not authorized) try to Enter" );
       NotAuthorized();

      }
      delay(1000);
      
      }
    }
    BLYNK_CONNECTED(){
      Blynk.syncVirtual(V3);
      Blynk.syncVirtual(V4);
      Blynk.syncVirtual(V2);
    }
    BLYNK_WRITE(V3)
    {
      
       fflag = param.asInt(); // assigning incoming value from pin V3 to a variable
      
       
     
    }
    BLYNK_WRITE(V4)
    {
       eflag = param.asInt(); // assigning incoming value from pin V4 to a variable
     
    }

First of all, you should read this
https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

Second thing, blynk.begin and blynk.run are both blocking functions, so the internet connection is required.

Are you trying to make a stand-alone door lock system that doesn’t require internet? That’s not very “Blynk-ish”… And your code doesn’t make much sense to me. :thinking:

https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

Keep your void loop clean and use timers instead of delays. It will never work otherwise!

Use the search function to find information about how others have implemented RFID in their projects.

1 Like

Blynk.begin is a blocking function. If it cant connect to WiFi or the Blynk server the code execution will stop at that line of the void setup.

To solve this, you need to manage your own WiFi connection, then if a connection is successful you then use Blynk.config and Blynk.connect.

Each time the Blynk.run command is encountered, the code will attempt to connect to Blynk if it isn’t already connected. For this reason, you should check if you have a WiFi connection before executing Blynk.run otherwise you will encounter delays while the Blynk.connect process attempts to re-connect, then times-out.

Pete.

thanks all of you ,
i try to take the loop clean as

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

and i put inside the void setup:

timer.setInterval(4000L, internet);
and
timer.setInterval(1000L, nointernet);
inside void internet:

if ( WiFi.status() == WL_CONNECTED && !Blynk.connected() ){
     Blynk.begin(auth, ssid, pass); 
   }

now it’s work good but, if it has a ssid name network and there is no internet but the name is avaliable will stuck in
connecting to Blynk-cloud : 80

and can I use the (Blynk.air) advantage which is in Blynk2.0
to upload the code from any were ?

The ideal blynk void loop should look like this

void loop() 
{
  Blynk.run();
  timer.run();
}

If you would like to use OTA ( Over-The-Air updates ), you should use blynk edgent, which allow you to update the firmware over the air, and use wifi provisioning.

There is no point in posting snippets of code.

If you’re still using Blynk.begin then you will continue to experience this issue, as…

If you’ve done this…

then we need to see your entire sketch to see how you are managing your WiFi connection, and whether you’ve listened to this piece of advice…

Both of these timed functions will execute, regardless of whether you have an internet connection, so this appears to be a bad choice of solution, but it’s impossible to say without seeing your entire sketch.

My approach is to use a Boolean variable as a flag to indicate whether we are in “standalone_mode” or not, and only execute Blynk.run if standalone_mode == false.
Then, have a timer to call a function which attempts to re-try the connection if standalone_mode == true, or sets standalone_mode = true if Blynk.connected() == false.

Pete.