My relays dont work if wifi is bad and ESP32 is offline

Hello,

I am completely new to Blynk and not very well-versed in coding.
I wanted to have simple control of some pumps through relays on my ESP32 and then the pump’s on-and-off status onto my Blynk application. Everything works well when there is good network connectivity. However, the issue that arises is when my Wifi is off or when the internet is bad, my relays do not turn on when this happens which completely shuts down my system until I get good internet. How can I override this and let my relays work offline even if there is no Wi-Fi?
I tried reading quite a few articles but then got even more confused.
Could someone please guide me as to how to fix this issue?


#define BLYNK_PRINT Serial

/* Fill in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID "Template"
#define BLYNK_TEMPLATE_NAME "Pump Control"
#define BLYNK_AUTH_TOKEN "auth"

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "ssid";
char pass[] = "pass";

// Define the Relay pin and related variables
const int RelayPinN = 33; // Pump1
int switchStatus = 0;

unsigned long previousNTime = 0;

unsigned long waitN = 100;


unsigned long PumpNOn = 5000;
unsigned long PumpNOff = 15000;

char auth[] = BLYNK_AUTH_TOKEN;

// Create Blynk Timer object
BlynkTimer timer;

void setup() {
  // Set RelayPin as an output pin
  pinMode(RelayPinN, OUTPUT);
  digitalWrite(RelayPinN, HIGH);
  
  Serial.begin(115200);           // Start serial communication at 115200 baud rate for debugging
  delay(10);

  // Configure Blynk and connect to WiFi
  Blynk.config(auth);
  Blynk.connectWiFi(ssid, pass);

  // timer.setInterval(1000L, myTimerEvent);  // Setup a function to be called every second
}

void loop() {

  Blynk.run();  // Run Blynk
  timer.run();  // Run BlynkTimer
  PumpN();
}

void PumpN(){
   unsigned long currentNTime = millis();
   if(currentNTime - previousNTime == waitN){
   digitalWrite(RelayPinN, LOW); 
     Blynk.virtualWrite(V0, "Water Pump Started");
  Serial.print("Water Pump: ");
  Serial.print("LOW");
   }
  
   if (currentNTime - previousNTime >= PumpNOn){
    digitalWrite(RelayPinN, HIGH);
      Blynk.virtualWrite(V0, "Water Pump Stopped");
  Serial.print("Water Pump: ");
  Serial.print("HIGH");
   }
   if (currentNTime - previousNTime >= PumpNOff){
    digitalWrite(RelayPinN, LOW);
     Blynk.virtualWrite(V0, "Water Pump Started");
  Serial.print("Water Pump: ");
  Serial.print("LOW");
    //reset timer
    previousNTime = currentNTime;
  }
}

// This function is called every time the device is connected to the Blynk.Cloud
BLYNK_CONNECTED() {
  Blynk.syncVirtual(V0); // updates Virtual Pin V0 to the latest stored value on the server.
}

Thank you

Why are you doing this in your void loop, and doing a millis comparison in your PumpN function, instead of using BlynkTimer ?

You need to manual,y manage your WiFi connection rather than using

as this is a blocking function.

You’ll also need to only run Blynk.run() if you are connected to the Blynk server.

There are some posts about how to correctly use Blynk.config(), it would be worth you doing a search to find these and learn from them.

Pete.

Hello Pete,

Thanks for the prompt reply.

I am using millis comparison as I wanted to use it to turn on the pump for 5 secs and off for 15 seconds, mainly to avoid delays as I heard that they don’t work well on Blynk.

Shouldn’t PumpN(); be in the loop as it needs to be continuous?

For the rest of the Blynk sections, I copied and pasted from different areas. As I said I’m new to this and at the moment everything is just a bit confusing, so I’m not even sure how to manually manage wifi or how to use Blyn.run() and Blynk.config()

I did see some posts on checkBlynkstatus, but I’m not sure how to incorporate it.

Thank You.

You should be using BlynkTimer for this.

Have you searched for “Blynk.config” ?

Pete.

Hello Pete,

I read up on everything. I have found a way to make the relays work without wifi now. However, the issue now is that I’m not able to connect to the Wi-Fi itself and manually I am not able to view the device ad hence no data on the blynk app.
I am affixing the code below, could you please guide me as to where I am wrong now.
Also, any example as to how to use Blynk timer for the pumps/relays?

#define BLYNK_PRINT Serial

/* Fill in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID "Template"
#define BLYNK_TEMPLATE_NAME "Pump Control"
#define BLYNK_AUTH_TOKEN "auth"

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "ssid";
char pass[] = "pass";

// Define the Relay pin and related variables
const int RelayPinN = 33; // Pump1
int switchStatus = 0;

unsigned long previousNTime = 0;

unsigned long waitN = 100;


unsigned long PumpNOn = 5000;
unsigned long PumpNOff = 15000;

char auth[] = BLYNK_AUTH_TOKEN;

boolean connectInProgress;
int connectTimerId;

BlynkTimer blynkTimer;  // SimpleTimer replacement

void checkBlynkConnection()
{
  if (!Blynk.connected() && !connectInProgress)
  {
    connectInProgress = true;  

    if(!Blynk.connect())
    {
      #ifdef BLYNK_PRINT
      Serial.println("[_\\|/_] Blynk Connect failed");
      #endif
    }
    else
    {
      #ifdef BLYNK_PRINT
      Serial.println("[_\\|/_] Blynk Connected");  
      #endif
    }
  
    connectInProgress = false; // either it connected or timed out
  }
}

void setup() {
  // Set RelayPin as an output pin
  pinMode(RelayPinN, OUTPUT);
  digitalWrite(RelayPinN, HIGH);
   Serial.begin(115200);           // Start serial communication at 115200 baud rate for debugging
 // timer tasks
  connectTimerId = blynkTimer.setInterval(300000L, checkBlynkConnection);   // check Blynk connection every 5 minutes

  // non-blocking Blynk setup
  Blynk.config(auth, BLYNK_DEFAULT_DOMAIN, BLYNK_DEFAULT_PORT);
  Blynk.disconnect(); // skip connecting to the Blynk server until checkBlynkConnection timer fires

  // this allows setup() to continue/exit and the main loop() to begin without blocking
  blynkTimer.setTimeout(2000L, checkBlynkConnection); // in  2 seconds, do initial connection check
}

void loop() {
 blynkTimer.run();
  
  // only attempt Blynk-related functions when connected to Blynk
  if (Blynk.connected())
  {
    Blynk.run();
  }
  PumpN();
}

void PumpN(){
   unsigned long currentNTime = millis();
   if(currentNTime - previousNTime == waitN){
   digitalWrite(RelayPinN, LOW); 
     Blynk.virtualWrite(V0, "Water Pump Started");
  Serial.print("Water Pump: ");
  Serial.print("LOW");
   }
  
   if (currentNTime - previousNTime >= PumpNOn){
    digitalWrite(RelayPinN, HIGH);
      Blynk.virtualWrite(V0, "Water Pump Stopped");
  Serial.print("Water Pump: ");
  Serial.print("HIGH");
   }
   if (currentNTime - previousNTime >= PumpNOff){
    digitalWrite(RelayPinN, LOW);
     Blynk.virtualWrite(V0, "Water Pump Started");
  Serial.print("Water Pump: ");
  Serial.print("LOW");
    //reset timer
    previousNTime = currentNTime;
  }
}

// This function is called every time the device is connected to the Blynk.Cloud
BLYNK_CONNECTED() {
  Blynk.syncVirtual(V0); // updates Virtual Pin V0 to the latest stored value on the server.
}

That’s a very bold claim!
My stats show that I’ve visited this forum every day for the past 7 years and read over 135,000 posts, but I certainly wouldn’t claim that I’ve read everything.

This piece of code was written to demonstrate the process of connecting to WiFi then Blynk, then attempting to re-connect if either one of these connections fails…

Feel free to take the relevant parts that you need for your purposes, and to adjust the parameters to suit your requirements (bear in mind that this was written to debunk a particular claim regarding a bug in the library, not as a functional piece of code).

You’ll see that BlynkTimer is needed to periodically check the connection status. I’d suggest (once again) that you stop calling the PumpN() function from your void loop and instead call a modified version of that function (without the millis comparison code) with BlynkTimer.

BTW, pumps don’t generally like to be constantly started then stopped, although it does tend to depend on the type of motor they utilise. I have no idea what this pump is for, as you haven’t shared that information, but you may be better using some sort of liquid level sensor.

Pete.

1 Like