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!");
}
}