How to run the program when Blynk not connect to WIFI and resume connectivity when Blynk back online?

Hi everyone I’m new to Blynk and I want to track the Blynk WIFI connection when it is offline. I want it to still do something when it is offline but based on my code when “internet = 0” (in the void loop) it kinda freezes and I can’t run the offline code inside that condition. I’ve been researching for a while on how to do this but I didn’t seem to find any solution. I use Blynk Edgent by the way.

Here is my code:

//#define BLYNK_TEMPLATE_ID           "TMPxxxxxx"
//#define BLYNK_TEMPLATE_NAME         "Device"

#define BLYNK_FIRMWARE_VERSION "0.1.0"

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG

#define APP_DEBUG

#include "BlynkEdgent.h"

int internet = 0;

const int btn1 = 2;   
const int btn2 = 3;   

const int relay1_pin = 12;
const int relay2_pin = 13;

int relay1State = LOW;
int relay2State = LOW;

void checkConnection() {

  bool isConnected = Blynk.connected();
  if (isConnected == false) {
    internet = 0;
    Serial.println("Blynk Not Connected");
  }
  if (isConnected == true) {
    internet = 1;
    Serial.println("Blynk Connected");
  }
}

BLYNK_WRITE(V0) {
  relay1State = param.asInt();
  digitalWrite(relay1_pin, !relay1State);
}

BLYNK_WRITE(V1) {
  relay2State = param.asInt();
  digitalWrite(relay2_pin, !relay2State);
}

BLYNK_CONNECTED() {
  Blynk.syncVirtual(V0, V1);
}

void setup() {
  delay(1000);
  Serial.begin(115200);

  pinMode(relay1_pin, OUTPUT);
  pinMode(relay2_pin, OUTPUT);

  // Turn off relay
  digitalWrite(relay1_pin, HIGH);
  digitalWrite(relay2_pin, HIGH);

  BlynkEdgent.begin();
  timer.setInterval(2000L, checkConnection);
}

void loop() {
  BlynkEdgent.run();
  timer.run();

  // check if there is wifi connection
  if (internet != 1) {
    Serial.println("Connection failed!");
    // run offline operation
    // turn on/off relay with physicle button
  } else {
    Serial.println("Connection OK!");
  }
}

You’re going to have to re-work the Edgent example sketch quite a bit if you want to handle off-line functionality.
Is there any reason why you’re using the Edgent example rather than a regular static provisioning Blynk example?

Pete.

Thanks, Pete. I don’t think I know how to customize the Edgent sketch for this functionality. I think Edgent example let you config wifi network later on the app rather than include wifi name and password in the code and OTA too?

That’s my point - it’s a very complex sketch and isn’t a good starting point t for what you want to achieve.

Yes, the Edgent example allows dynamic provisioning of WiFi credentials.
There is a way to use Blynk.Air OTA without using Edgent though.

Pete.

Dynamic provisioning is a good feature to have but seems like I can’t achieve both of these functionalities, either have offline function or dynamic provisioning.

No, I’m not saying that, just that the Edgent sketch is very complex and you’ll need to to ensure that you understand it before you start making changes.

Pete.