Blynk blocking code if no connection to wifi or cloud

I am trying to run this code in case there is no wifi or cloud connection. If I remove Blynk.begin and Blynk.run the code is executed. Can someone assist me with this please.

// This #include statement was automatically added by the Particle IDE.
#include <blynk.h>

// This #include statement was automatically added by the Particle IDE.
#include <MFRC522.h>


SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED);

const uint32_t msRetryDelay = 2*60000; // retry every 2min
const uint32_t msRetryTime  =   30000; // stop trying after 30sec


bool   retryRunning = false;
bool state = HIGH;

Timer retryTimer(msRetryDelay, retryConnect);  // timer to retry connecting
Timer stopTimer(msRetryTime, stopConnect);     // timer to stop a long running try

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";  //Demo
char server[] = "xxxxxxxxxxxxxx";

#define SS_PIN A2
#define RST_PIN D2

MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.

WidgetLCD lcd(V7);             // Blynk LCD Widget pin V7 LCD
WidgetLCD lcd2(V8);            // Blynk LCD Widget pin V5 LCD

Servo myServo;  
#define A45 V1                 // 45 button V1
#define B90 V2                 // 90 button V2
#define C135 V6                // 135 button V6
#define readValue V5           // read Servo Angle Labeled Display V5
#define BLYNK_PRINT Serial

const int led7 = D7;
const int servoPin = D3;       // servo connected to pin D3 on Photon


void setup() {
    
    myServo.attach(servoPin);  // attaches the servo on pin D1 to the servo object
    myServo.write(10);         // Sets Sservo Position to 10 due to jittering at 0degrees
    
    pinMode(led7, OUTPUT);
    
    SPI.begin();
    SPI.setClockDivider(SPI_CLOCK_DIV8);
    
  mfrc522.setSPIConfig();
  mfrc522.PCD_Init(); // Init MFRC522 card

  Particle.connect();
    
    if (!waitFor(Particle.connected, msRetryTime))
    WiFi.off();                // no luck, no need for WiFi
   
    Blynk.begin(auth, server);
    
}



BLYNK_READ(readValue)
{
Blynk.virtualWrite(readValue, (myServo.read() - 10));  // Push Mode sends value instantly
}

BLYNK_WRITE(A45)                                   //Set angle at 45degrees
{
    int status1 = param.asInt();
    
    if (status1 == 1)
    {
         myServo.write(55);
    
    }
    else if (status1 == 0)
    {
         myServo.write(10);
    }
}

BLYNK_WRITE(B90)                                   //Set angle at 90degrees
{
    int status2 = param.asInt();
    
    if (status2 == 1)
    {
         myServo.write(100);
    
    }
    else if (status2 == 0)
    {
         myServo.write(10);
    }
}

BLYNK_WRITE(C135)                                  //Set angle at 135degrees
{
    int status3 = param.asInt();
    
    if (status3 == 1)
    {
         myServo.write(145);
    
    }
    else if (status3 == 0)
    {
         myServo.write(10);
    }
}



void retryConnect()
{
  if (!Particle.connected())   // if not connected to cloud
  {
    stopTimer.start();         // set of the timout time
    WiFi.on();
    Particle.connect();        // start a reconnectino attempt
    
  }
  else                         // if already connected
  {
    retryTimer.stop();         // no further attempts required
    retryRunning = false;
  }
}

void stopConnect()
{
    if (!Particle.connected()) // if after retryTime no connection
      WiFi.off();              // stop trying and swith off WiFi
    stopTimer.stop();
}


void loop() {
    digitalWrite(D7, !digitalRead(D7));
   
  
     if (!retryRunning && !Particle.connected())
  { // if we have not already scheduled a retry and are not connected
    
    stopTimer.start();         // set timeout for auto-retry by system
    retryRunning = true;
    retryTimer.start();        // schedula a retry
  }
  delay(500);
     
   Blynk.run();
   
  // Look for new cards
  if ( mfrc522.PICC_IsNewCardPresent()) {
    
    if ( mfrc522.PICC_ReadCardSerial()) {
     

      String UID = "";
      

      for (byte i = 0; i < mfrc522.uid.size; i++) {
        
        UID.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
        UID.concat(String(mfrc522.uid.uidByte[i], HEX));
      }
      
      mfrc522.PICC_HaltA();
     
      
UID.toUpperCase();

if (UID.substring(1) == "04 83 26 02 84 46 80") //change here the UID of the card/cards that you want to give access
  { 
   
      lcd.clear();
      lcd.print(0, 0, "UID:");
      lcd.print(0, 1, UID);
      delay(1000);
      lcd.clear();
      lcd.print(0, 0, "Door is");
      lcd.print(0, 1, "----UNLOCKED----");
      lcd2.clear();
      lcd2.print(0, 0, "Welcome Nick...");
      lcd2.print(0, 1, " Access Granted ");
      Blynk.virtualWrite(readValue, 90);
      myServo.write(100);
      delay(3000);
      lcd.clear();
      lcd2.clear();
      lcd.print(0, 0, "Door is");
      lcd.print(0, 1, "-----LOCKED-----");
      lcd2.print(0, 0, "Please present");
      lcd2.print(0, 1, "security pass");
      myServo.write(10);

  }

  
  
  else if (UID.substring(1) == "85 08 06 AB") //change here the UID of the card/cards that you want to give access
  { 
 
      lcd.clear();
      lcd.print(0, 0, "UID:");
      lcd.print(0, 1, UID);
      delay(1000);
      lcd.clear();
      lcd.print(0, 0, "Door is");
      lcd.print(0, 1, "----UNLOCKED----");
      lcd2.clear();
      lcd2.print(0, 0, "Welcome Elena...");
      lcd2.print(0, 1, " Access Granted ");
      Blynk.virtualWrite(readValue, 45);
      myServo.write(55);
      delay(3000);
      lcd.clear();
      lcd.print(0, 0, "Door is");
      lcd.print(0, 1, "-----LOCKED-----");
      lcd2.clear();
      lcd2.print(0, 0, "Please present");
      lcd2.print(0, 1, "security pass");
      myServo.write(10);

  }

  else   {
     
      myServo.write(10);
      lcd.clear();
      lcd.print(0, 0, "UID:");
      lcd.print(0, 1, UID);
      delay(1000);
      lcd2.clear();
      lcd2.print(0, 0, "Unauthorised....");
      lcd2.print(0, 1, " ACCESS DENIED ");
      delay(2000);
      lcd.clear();
      lcd2.clear();
      lcd2.print(0, 0, "Please present");
      lcd2.print(0, 1, "security pass");

      } 
     
    }
   }
  }

Forgot to mention after blinking green it goes to breathing white if there is no connection.

@eLumaLite, I suggest you take your credentials out of your posted code!.

Just like Particle functions not working without a cloud connection, you need to “surround” your Blynk calls with connectivity checks. A solid white LED means no WiFi connection. Blynk has connection management functions much like Particle’s:

http://docs.blynk.cc/#blynk-firmware-connection-management

You need to decide if your code will run at all with no Cloud or Blynk connection and surround your cloud/Blynk calls with connectivity checks (eg. Particle.connected() or WiFi.ready() or Blynk.connected()).

Thanks @peekay123.
I tried Blynk.run only if Particle is connected.
Unfortunately had long delays and unresponsive at times even when connected.
Replaced Blynk.begin in setup with Blynk.config
And instead of Blynk.run at the beginning of loop
I put Blynk.run only if Particle.connect and Wifi.on
Wrong approach?
Need code to run whether there is a network connection or not.
If there is a wifi connection and connected to cloud, then to be able to use Blynk.

@eLumaLite, can you repost your new code?

For Blynk.run() you (normally) only need WiFi.ready() not the Particle cloud as such.

Actually you need internet access, but there is no straight forward API to check, so you’d need to ping or request a public address to know wheter or not your WiFi is also connected to the outside world.

1 Like

@peekay123 will be posting here the code when I get back home tomorrow .

Hi @ScruffR. This is only a test project. My main concern is for code in loop to run if there is no wifi connection(not even an access point). So after a set time of trying to connect, let code run in loop.
After attempting a reconnect lets say every 5mins, if successfully connected to internet then can use Blynk.

Below are the changes I made. I noticed that after attempting a reconnect if no wifi, I got red flashing code for around 30secs, code in loop froze during the red flashing, then green flashing and a successful connection after I turned router back on. Connected to Cloud and Blynk. Still think I have not done this correctly.

#include <blynk.h>

// This #include statement was automatically added by the Particle IDE.
#include <MFRC522.h>


SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED);

const uint32_t msRetryDelay = 2*60000; // retry every 2min
const uint32_t msRetryTime  =   30000; // stop trying after 30sec


bool retryRunning = false;
bool state = HIGH;

Timer retryTimer(msRetryDelay, retryConnect);  // timer to retry connecting
Timer stopTimer(msRetryTime, stopConnect);     // timer to stop a long running try

char auth[] = "dbf5a7cd2dbb41b1802xxxxxxxxx";  //Demo
char server[] = "xxxxxxxxxx:8443";

#define SS_PIN A2
#define RST_PIN D2

MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.

WidgetLCD lcd(V7);             // Blynk LCD Widget pin V7 LCD
WidgetLCD lcd2(V8);            // Blynk LCD Widget pin V5 LCD

Servo myServo;  
#define A45 V1                 // 45 button V1
#define B90 V2                 // 90 button V2
#define C135 V6                // 135 button V6
#define readValue V5           // read Servo Angle Labeled Display V5
#define BLYNK_PRINT Serial

const int led7 = D7;
const int servoPin = D3;       // servo connected to pin D3 on Photon


void setup() {
    
    myServo.attach(servoPin);  // attaches the servo on pin D1 to the servo object
    myServo.write(10);         // Sets Sservo Position to 10 due to jittering at 0degrees
    
    pinMode(led7, OUTPUT);
    
    SPI.begin();
    SPI.setClockDivider(SPI_CLOCK_DIV8);
    
  mfrc522.setSPIConfig();
  mfrc522.PCD_Init(); // Init MFRC522 card

  Particle.connect();
  Blynk.config(auth, server);  
  
    if (!waitFor(Particle.connected, msRetryTime))
    WiFi.off();                // no luck, no need for WiFi
    
}



BLYNK_READ(readValue)
{
Blynk.virtualWrite(readValue, (myServo.read() - 10));  // Push Mode sends value instantly
}

BLYNK_WRITE(A45)                                   //Set angle at 45degrees
{
    int status1 = param.asInt();
    
    if (status1 == 1)
    {
         myServo.write(55);
    
    }
    else if (status1 == 0)
    {
         myServo.write(10);
    }
}

BLYNK_WRITE(B90)                                   //Set angle at 90degrees
{
    int status2 = param.asInt();
    
    if (status2 == 1)
    {
         myServo.write(100);
    
    }
    else if (status2 == 0)
    {
         myServo.write(10);
    }
}

BLYNK_WRITE(C135)                                  //Set angle at 135degrees
{
    int status3 = param.asInt();
    
    if (status3 == 1)
    {
         myServo.write(145);
    
    }
    else if (status3 == 0)
    {
         myServo.write(10);
    }
}



void retryConnect()
{
  if (!Particle.connected())   // if not connected to cloud
  {
    stopTimer.start();         // set of the timout time
    WiFi.on();
    Particle.connect();        // start a reconnection attempt
    Blynk.connect();
  }
  else                         // if already connected
  {
    
    retryTimer.stop();         // no further attempts required
    retryRunning = false;
  }
}

void stopConnect()
{
    if (!Particle.connected()) // if after retryTime no connection
    WiFi.off();                // stop trying and switch off WiFi
    stopTimer.stop();
}


void loop() {
    
    Blynk.run();
    
    digitalWrite(D7, !digitalRead(D7));
  
     if (!retryRunning && !Particle.connected())
  { // if we have not already scheduled a retry and are not connected
    
    stopTimer.start();         // set timeout for auto-retry by system
    retryRunning = true;
    retryTimer.start();        // schedula a retry
  }
  delay(500);
     
   
   
  // Look for new cards
  if ( mfrc522.PICC_IsNewCardPresent()) {
    
    if ( mfrc522.PICC_ReadCardSerial()) {
     

      String UID = "";
      

      for (byte i = 0; i < mfrc522.uid.size; i++) {
        
        UID.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
        UID.concat(String(mfrc522.uid.uidByte[i], HEX));
      }
      
      mfrc522.PICC_HaltA();
     
      
UID.toUpperCase();

if (UID.substring(1) == "04 83 26 02 84 46 80") //change here the UID of the card/cards that you want to give access
  { 
   
      lcd.clear();
      lcd.print(0, 0, "UID:");
      lcd.print(0, 1, UID);
      delay(1000);
      lcd.clear();
      lcd.print(0, 0, "Door is");
      lcd.print(0, 1, "----UNLOCKED----");
      lcd2.clear();
      lcd2.print(0, 0, "Welcome Nick...");
      lcd2.print(0, 1, " Access Granted ");
      Blynk.virtualWrite(readValue, 90);
      myServo.write(100);
      delay(3000);
      lcd.clear();
      lcd2.clear();
      lcd.print(0, 0, "Door is");
      lcd.print(0, 1, "-----LOCKED-----");
      lcd2.print(0, 0, "Please present");
      lcd2.print(0, 1, "security pass");
      myServo.write(10);

  }

  
  
  else if (UID.substring(1) == "85 08 06 AB") //change here the UID of the card/cards that you want to give access
  { 
 
      lcd.clear();
      lcd.print(0, 0, "UID:");
      lcd.print(0, 1, UID);
      delay(1000);
      lcd.clear();
      lcd.print(0, 0, "Door is");
      lcd.print(0, 1, "----UNLOCKED----");
      lcd2.clear();
      lcd2.print(0, 0, "Welcome Elena...");
      lcd2.print(0, 1, " Access Granted ");
      Blynk.virtualWrite(readValue, 45);
      myServo.write(55);
      delay(3000);
      lcd.clear();
      lcd.print(0, 0, "Door is");
      lcd.print(0, 1, "-----LOCKED-----");
      lcd2.clear();
      lcd2.print(0, 0, "Please present");
      lcd2.print(0, 1, "security pass");
      myServo.write(10);

  }

  else   {
     
      myServo.write(10);
      lcd.clear();
      lcd.print(0, 0, "UID:");
      lcd.print(0, 1, UID);
      delay(1000);
      lcd2.clear();
      lcd2.print(0, 0, "Unauthorised....");
      lcd2.print(0, 1, " ACCESS DENIED ");
      delay(2000);
      lcd.clear();
      lcd2.clear();
      lcd2.print(0, 0, "Please present");
      lcd2.print(0, 1, "security pass");

      } 
     
    }
   }
  }

Here is the sequence @peekay123 @ScruffR of onboard led.
If there is a connection to wifi and cloud, all works fine.
When I disconnect the internet…
green flashing for 30secs
cyan flashing for 90secs
red flashing for 30secs(code in loop stops working during this time)
green flashing for 30secs(code in loops starts working again)
white breathing for 5 mins
red flashing for 30secs(code stops working again)
green flashing for 30secs(code stops working)
white breathing (code now works)
red flashing, green flashing(code stops working)
I reconnect the internet and after a successful connection, all good again.