Nodemcu is disconnecting after a while?

i have a code to control 4 relays with a node mcu which can also be controlled by an RF remote . when the module is powered on it is working jus fine but after sometime it just disconnects (idk if it only disconnects from blynk or it loses connection to the wifi also) and seemingly never connects again it is a constant problem it occurs everytime. Can anyone solve this problem form me? IDK what is wrong with the code or with my Nodemcu Module itself

The Code:

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <RCSwitch.h>
BlynkTimer clocktime;


#define relayfan 4
#define relaylight 5
#define relaylightb 14
#define relayrandom 12

#define VPIN_BUTTON_1    V12
#define VPIN_BUTTON_2    V13
#define VPIN_BUTTON_3    V14
#define VPIN_BUTTON_4    V15

RCSwitch mySwitch = RCSwitch();

int relayfanstate = HIGH;
int relaylightstate = HIGH;
int relaylightbstate = HIGH;
int relayrandomstate = HIGH;
int mainstate = HIGH;
long code;


char auth[] = "7k6FzFfP541FKDV-9jwppRFNvR-hDr0n";

char ssid[] = "Nag";
char pass[] = "87654321";
BLYNK_CONNECTED()
{
  Serial.println("blynk connected first function");
  Blynk.syncVirtual(VPIN_BUTTON_1);
  Blynk.syncVirtual(VPIN_BUTTON_2);
  Blynk.syncVirtual(VPIN_BUTTON_3);
  Blynk.syncVirtual(VPIN_BUTTON_4);
}

BLYNK_WRITE(VPIN_BUTTON_1)
{
  Serial.println("blynk write");
  relayfanstate = param.asInt();
  digitalWrite(relayfan, relayfanstate);
}

BLYNK_WRITE(VPIN_BUTTON_2)
{
  Serial.println("blynk write");
  relaylightstate = param.asInt();
  digitalWrite(relaylight, relaylightstate);
}

BLYNK_WRITE(VPIN_BUTTON_3)
{
  Serial.println("blynk write");
  relaylightbstate = param.asInt();
  digitalWrite(relaylightb, relaylightbstate);
}

BLYNK_WRITE(VPIN_BUTTON_4)
{
  Serial.println("blynk write");
  relayrandomstate = param.asInt();
  digitalWrite(relayrandom, relayrandomstate);
}


void setup()
{
  Serial.begin(9600);
  mySwitch.enableReceive(0);
  pinMode(relayfan, OUTPUT);
  pinMode(relaylight, OUTPUT);
  pinMode(relaylightb, OUTPUT);
  pinMode(relayrandom, OUTPUT);
  digitalWrite(relayfan, HIGH);
  digitalWrite(relaylight, HIGH);
  digitalWrite(relaylightb, HIGH);
  digitalWrite(relayrandom, HIGH);
  Blynk.begin(auth, ssid, pass);
  Serial.println("Blynk config done");
}


void loop()
{

  if (Blynk.connected() == false)
  {
    Serial.println("Not Connected");
    Serial.println("Without internet rf code recieving started");
    delay(500);
    if (mySwitch.available())
    {
      Serial.println("Switch available");
      code = mySwitch.getReceivedValue();
      if (mySwitch.getReceivedValue() == 8594433)
      {
        Serial.println("code matched without internet");
        mainstate = !mainstate;
        digitalWrite(relayfan, mainstate);
        digitalWrite(relaylight, mainstate);
        digitalWrite(relaylightb, mainstate);
        digitalWrite(relayrandom, mainstate);
        delay(1000);
        mySwitch.resetAvailable();
      }
      else if (mySwitch.getReceivedValue() == 8594447)
      {
        digitalWrite(relayfan, LOW);
        delay(1000);
        mySwitch.resetAvailable();
      }
      else if (mySwitch.getReceivedValue() == 8594445)
      {
        digitalWrite(relayfan, HIGH);
        delay(1000);
        mySwitch.resetAvailable();
      }
      else if (mySwitch.getReceivedValue() == 8594436)
      {
        relaylightstate = !relaylightstate;
        digitalWrite(relaylight, relaylightstate);
        delay(1000);
        mySwitch.resetAvailable();
      }
      else if (mySwitch.getReceivedValue() == 8594437)
      {
        relaylightbstate = !relaylightbstate;
        digitalWrite(relaylightb, relaylightbstate);
        delay(1000);
        mySwitch.resetAvailable();
      }
      else if (mySwitch.getReceivedValue() == 8594439)
      {
        relayrandomstate = !relayrandomstate;
        digitalWrite(relayrandom, relayrandomstate);
        delay(1000);
        mySwitch.resetAvailable();
      }
    }
  }
  else
  {
    Serial.println(" Connected");
    Blynk.run();
    with_internet();
  }
  clocktime.run();
}

void with_internet()
{
  Serial.println("With internet rf code recieving started");
  delay(500);
  if (mySwitch.available())
  {
    Serial.println("Switch available");
    code = mySwitch.getReceivedValue();
    if (mySwitch.getReceivedValue() == 8594433)
    {
      Serial.println("code matched without internet");
      mainstate = !mainstate;
      digitalWrite(relayfan, mainstate);
      digitalWrite(relaylight, mainstate);
      digitalWrite(relaylightb, mainstate);
      digitalWrite(relayrandom, mainstate);
      Blynk.virtualWrite(VPIN_BUTTON_1, mainstate);
      Blynk.virtualWrite(VPIN_BUTTON_2, mainstate);
      Blynk.virtualWrite(VPIN_BUTTON_3, mainstate);
      Blynk.virtualWrite(VPIN_BUTTON_4, mainstate);
      delay(1000);
      mySwitch.resetAvailable();
    }
    else if (mySwitch.getReceivedValue() == 8594447)
    {
      digitalWrite(relayfan, LOW);
      Blynk.virtualWrite(VPIN_BUTTON_1, LOW);
      delay(1000);
      mySwitch.resetAvailable();
    }
    else if (mySwitch.getReceivedValue() == 8594445)
    {
      digitalWrite(relayfan, HIGH);
      Blynk.virtualWrite(VPIN_BUTTON_1, HIGH);
      delay(1000);
      mySwitch.resetAvailable();
    }
    else if (mySwitch.getReceivedValue() == 8594436)
    {
      relaylightstate = !relaylightstate;
      digitalWrite(relaylight, relaylightstate);
      Blynk.virtualWrite(VPIN_BUTTON_2, relaylightstate);
      delay(1000);
      mySwitch.resetAvailable();
    }
    else if (mySwitch.getReceivedValue() == 8594437)
    {
      relaylightbstate = !relaylightbstate;
      digitalWrite(relaylightb, relaylightbstate);
      Blynk.virtualWrite(VPIN_BUTTON_3, relaylightbstate);
      delay(1000);
      mySwitch.resetAvailable();
    }
    else if (mySwitch.getReceivedValue() == 8594439)
    {
      relayrandomstate = !relayrandomstate;
      digitalWrite(relayrandom, relayrandomstate);
      Blynk.virtualWrite(VPIN_BUTTON_4, relayrandomstate);
      delay(1000);
      mySwitch.resetAvailable();
    }
  }
}

Suggestion use
Blynk. begin(auth, ssid, pass)
In setup

And
Blynk. run() in loop

The disconnections are caused by your cluttered void loop and the delays you are using.
You should read this:

When using an IR sensor, you need some code in your void loop to “listen” for new IR commands, but you don’t need blocking delays at all in your code, and all of the code used to take the correct action based on the mySwitch.getReceivedValue() should be handled in a single function, probably using a switch/case statement.

Pete.

I changed my code to this

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <RCSwitch.h>
BlynkTimer clocktime;


#define relayfan 4
#define relaylight 5
#define relaylightb 14
#define relayrandom 12

#define VPIN_BUTTON_1    V12
#define VPIN_BUTTON_2    V13
#define VPIN_BUTTON_3    V14
#define VPIN_BUTTON_4    V15

RCSwitch mySwitch = RCSwitch();

int relayfanstate = HIGH;
int relaylightstate = HIGH;
int relaylightbstate = HIGH;
int relayrandomstate = HIGH;
int mainstate = HIGH;
long code;


char auth[] = "7k6FzFfP541FKDV-9jwppRFNvR-hDr0n";

char ssid[] = "Nag";
char pass[] = "87654321";
BLYNK_CONNECTED()
{
  Serial.println("blynk connected first function");
  Blynk.syncVirtual(VPIN_BUTTON_1);
  Blynk.syncVirtual(VPIN_BUTTON_2);
  Blynk.syncVirtual(VPIN_BUTTON_3);
  Blynk.syncVirtual(VPIN_BUTTON_4);
}

BLYNK_WRITE(VPIN_BUTTON_1)
{
  Serial.println("blynk write");
  relayfanstate = param.asInt();
  digitalWrite(relayfan, relayfanstate);
}

BLYNK_WRITE(VPIN_BUTTON_2)
{
  Serial.println("blynk write");
  relaylightstate = param.asInt();
  digitalWrite(relaylight, relaylightstate);
}

BLYNK_WRITE(VPIN_BUTTON_3)
{
  Serial.println("blynk write");
  relaylightbstate = param.asInt();
  digitalWrite(relaylightb, relaylightbstate);
}

BLYNK_WRITE(VPIN_BUTTON_4)
{
  Serial.println("blynk write");
  relayrandomstate = param.asInt();
  digitalWrite(relayrandom, relayrandomstate);
}


void setup()
{
  Serial.begin(9600);
  mySwitch.enableReceive(0);
  pinMode(relayfan, OUTPUT);
  pinMode(relaylight, OUTPUT);
  pinMode(relaylightb, OUTPUT);
  pinMode(relayrandom, OUTPUT);
  digitalWrite(relayfan, HIGH);
  digitalWrite(relaylight, HIGH);
  digitalWrite(relaylightb, HIGH);
  digitalWrite(relayrandom, HIGH);
  Blynk.begin(auth, ssid, pass);
  Serial.println("Blynk config done");
}


void loop()
{

  if (Blynk.connected() == false)
  {
    Serial.println("Not Connected");
    without_internet();
  }
  else
  {
    Serial.println(" Connected");
    Blynk.run();
    with_internet();
  }
  clocktime.run();
}


void without_internet()
{
  if (mySwitch.available())
  {
    Serial.println("Switch available");
    code = mySwitch.getReceivedValue();
    if (mySwitch.getReceivedValue() == 8594433)
    {
      Serial.println("code matched without internet");
      mainstate = !mainstate;
      digitalWrite(relayfan, mainstate);
      digitalWrite(relaylight, mainstate);
      digitalWrite(relaylightb, mainstate);
      digitalWrite(relayrandom, mainstate);
      mySwitch.resetAvailable();
    }
    else if (mySwitch.getReceivedValue() == 8594447)
    {
      digitalWrite(relayfan, LOW);
      mySwitch.resetAvailable();
    }
    else if (mySwitch.getReceivedValue() == 8594445)
    {
      digitalWrite(relayfan, HIGH);
      mySwitch.resetAvailable();
    }
    else if (mySwitch.getReceivedValue() == 8594436)
    {
      relaylightstate = !relaylightstate;
      digitalWrite(relaylight, relaylightstate);
      mySwitch.resetAvailable();
    }
    else if (mySwitch.getReceivedValue() == 8594437)
    {
      relaylightbstate = !relaylightbstate;
      digitalWrite(relaylightb, relaylightbstate);
      mySwitch.resetAvailable();
    }
    else if (mySwitch.getReceivedValue() == 8594439)
    {
      relayrandomstate = !relayrandomstate;
      digitalWrite(relayrandom, relayrandomstate);
      mySwitch.resetAvailable();
    }
  }
}



void with_internet()
{
  Serial.println("With internet rf code recieving started");
  if (mySwitch.available())
  {
    Serial.println("Switch available");
    code = mySwitch.getReceivedValue();
    if (mySwitch.getReceivedValue() == 8594433)
    {
      Serial.println("code matched without internet");
      mainstate = !mainstate;
      digitalWrite(relayfan, mainstate);
      digitalWrite(relaylight, mainstate);
      digitalWrite(relaylightb, mainstate);
      digitalWrite(relayrandom, mainstate);
      Blynk.virtualWrite(VPIN_BUTTON_1, mainstate);
      Blynk.virtualWrite(VPIN_BUTTON_2, mainstate);
      Blynk.virtualWrite(VPIN_BUTTON_3, mainstate);
      Blynk.virtualWrite(VPIN_BUTTON_4, mainstate);
      mySwitch.resetAvailable();
    }
    else if (mySwitch.getReceivedValue() == 8594447)
    {
      digitalWrite(relayfan, LOW);
      Blynk.virtualWrite(VPIN_BUTTON_1, LOW);
      mySwitch.resetAvailable();
    }
    else if (mySwitch.getReceivedValue() == 8594445)
    {
      digitalWrite(relayfan, HIGH);
      Blynk.virtualWrite(VPIN_BUTTON_1, HIGH);
      mySwitch.resetAvailable();
    }
    else if (mySwitch.getReceivedValue() == 8594436)
    {
      relaylightstate = !relaylightstate;
      digitalWrite(relaylight, relaylightstate);
      Blynk.virtualWrite(VPIN_BUTTON_2, relaylightstate);
      mySwitch.resetAvailable();
    }
    else if (mySwitch.getReceivedValue() == 8594437)
    {
      relaylightbstate = !relaylightbstate;
      digitalWrite(relaylightb, relaylightbstate);
      Blynk.virtualWrite(VPIN_BUTTON_3, relaylightbstate);
      mySwitch.resetAvailable();
    }
    else if (mySwitch.getReceivedValue() == 8594439)
    {
      relayrandomstate = !relayrandomstate;
      digitalWrite(relayrandom, relayrandomstate);
      Blynk.virtualWrite(VPIN_BUTTON_4, relayrandomstate);
      mySwitch.resetAvailable();
    }
  }
}

node mcu is still disconnecting but is taking slightly longer like 40-50 after turning on it is getting disconnected

So, you’ve removed the delays and you’ve added a timer (called rather bizarrely clocktime rather than the standard timer) but it is currently serving no purpose.

You’ve moved some code out of the void loop into other functions, but you are calling those function each time the void loop executes, which is no different to having the code in your void loop in the first place.

The code in your void loop should be:

Blynk.run
timer.run
IR listening code

If (and only if) an IR signal is received then you should be calling a function, containing a switch/case statement, which decodes this and takes the appropriate actions.

Forget about the online/offline code for now, just keep it simple.

Pete.

Is this better?

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <RCSwitch.h>

#define relayfan 4
#define relaylight 5
#define relaylightb 14
#define relayrandom 12

#define VPIN_BUTTON_1    V12
#define VPIN_BUTTON_2    V13
#define VPIN_BUTTON_3    V14
#define VPIN_BUTTON_4    V15

RCSwitch mySwitch = RCSwitch();

int relayfanstate = HIGH;
int relaylightstate = HIGH;
int relaylightbstate = HIGH;
int relayrandomstate = HIGH;
int mainstate = HIGH;
long code;


char auth[] = "7k6FzFfP541FKDV-9jwppRFNvR-hDr0n";

char ssid[] = "Nag";
char pass[] = "87654321";
BLYNK_CONNECTED()
{
  Serial.println("blynk connected first function");
  Blynk.syncVirtual(VPIN_BUTTON_1);
  Blynk.syncVirtual(VPIN_BUTTON_2);
  Blynk.syncVirtual(VPIN_BUTTON_3);
  Blynk.syncVirtual(VPIN_BUTTON_4);
}

BLYNK_WRITE(VPIN_BUTTON_1)
{
  Serial.println("blynk write");
  relayfanstate = param.asInt();
  digitalWrite(relayfan, relayfanstate);
}

BLYNK_WRITE(VPIN_BUTTON_2)
{
  Serial.println("blynk write");
  relaylightstate = param.asInt();
  digitalWrite(relaylight, relaylightstate);
}

BLYNK_WRITE(VPIN_BUTTON_3)
{
  Serial.println("blynk write");
  relaylightbstate = param.asInt();
  digitalWrite(relaylightb, relaylightbstate);
}

BLYNK_WRITE(VPIN_BUTTON_4)
{
  Serial.println("blynk write");
  relayrandomstate = param.asInt();
  digitalWrite(relayrandom, relayrandomstate);
}


void setup()
{
  Serial.begin(9600);
  mySwitch.enableReceive(0);
  pinMode(relayfan, OUTPUT);
  pinMode(relaylight, OUTPUT);
  pinMode(relaylightb, OUTPUT);
  pinMode(relayrandom, OUTPUT);
  digitalWrite(relayfan, HIGH);
  digitalWrite(relaylight, HIGH);
  digitalWrite(relaylightb, HIGH);
  digitalWrite(relayrandom, HIGH);
  Blynk.begin(auth, ssid, pass);
  Serial.println("Blynk config done");
}


void loop()
{
  fin:
  if (mySwitch.available())
  {
    with_internet
  }
  Blynk.run();
}


void with_internet()
{
  if (Blynk.connected == true)
  {
    Serial.println("Switch available");
    code = mySwitch.getReceivedValue();
    if (mySwitch.getReceivedValue() == 8594433)
    {
      Serial.println("code matched without internet");
      mainstate = !mainstate;
      digitalWrite(relayfan, mainstate);
      digitalWrite(relaylight, mainstate);
      digitalWrite(relaylightb, mainstate);
      digitalWrite(relayrandom, mainstate);
      Blynk.virtualWrite(VPIN_BUTTON_1, mainstate);
      Blynk.virtualWrite(VPIN_BUTTON_2, mainstate);
      Blynk.virtualWrite(VPIN_BUTTON_3, mainstate);
      Blynk.virtualWrite(VPIN_BUTTON_4, mainstate);
      mySwitch.resetAvailable();
    }
    else if (mySwitch.getReceivedValue() == 8594447)
    {
      digitalWrite(relayfan, LOW);
      Blynk.virtualWrite(VPIN_BUTTON_1, LOW);
      mySwitch.resetAvailable();
    }
    else if (mySwitch.getReceivedValue() == 8594445)
    {
      digitalWrite(relayfan, HIGH);
      Blynk.virtualWrite(VPIN_BUTTON_1, HIGH);
      mySwitch.resetAvailable();
    }
    else if (mySwitch.getReceivedValue() == 8594436)
    {
      relaylightstate = !relaylightstate;
      digitalWrite(relaylight, relaylightstate);
      Blynk.virtualWrite(VPIN_BUTTON_2, relaylightstate);
      mySwitch.resetAvailable();
    }
    else if (mySwitch.getReceivedValue() == 8594437)
    {
      relaylightbstate = !relaylightbstate;
      digitalWrite(relaylightb, relaylightbstate);
      Blynk.virtualWrite(VPIN_BUTTON_3, relaylightbstate);
      mySwitch.resetAvailable();
    }
    else if (mySwitch.getReceivedValue() == 8594439)
    {
      relayrandomstate = !relayrandomstate;
      digitalWrite(relayrandom, relayrandomstate);
      Blynk.virtualWrite(VPIN_BUTTON_4, relayrandomstate);
      mySwitch.resetAvailable();
    }
  }
  else
  {
    code = mySwitch.getReceivedValue();
    if (mySwitch.getReceivedValue() == 8594433)
    {
      Serial.println("code matched without internet");
      mainstate = !mainstate;
      digitalWrite(relayfan, mainstate);
      digitalWrite(relaylight, mainstate);
      digitalWrite(relaylightb, mainstate);
      digitalWrite(relayrandom, mainstate);
      mySwitch.resetAvailable();
    }
    else if (mySwitch.getReceivedValue() == 8594447)
    {
      digitalWrite(relayfan, LOW);
      mySwitch.resetAvailable();
    }
    else if (mySwitch.getReceivedValue() == 8594445)
    {
      digitalWrite(relayfan, HIGH);
      mySwitch.resetAvailable();
    }
    else if (mySwitch.getReceivedValue() == 8594436)
    {
      relaylightstate = !relaylightstate;
      digitalWrite(relaylight, relaylightstate);
      mySwitch.resetAvailable();
    }
    else if (mySwitch.getReceivedValue() == 8594437)
    {
      relaylightbstate = !relaylightbstate;
      digitalWrite(relaylightb, relaylightbstate);
      mySwitch.resetAvailable();
    }
    else if (mySwitch.getReceivedValue() == 8594439)
    {
      relayrandomstate = !relayrandomstate;
      digitalWrite(relayrandom, relayrandomstate);
      mySwitch.resetAvailable();
    }
    goto fin;
  }
}

please don’t get frustrated at me or anything just desperately trying to get it working at all costs

the name timer is clashing with another in the rc switch library used for decoding the rf signal anyway i forgot to remove it after the first code iteration.

You don’t need this line of code. When the code in the function completes it will automatically return to the point in the code where the function was called. And my Number 1 tip for C++ programming is never, ever, ever use goto

My advice would be to keep this line of code in the void loop:

and , as I’ve said before, use Switch/case on the ‘code’ value instead of all these horrible if/else statements.

You should also get rid of the Blynk.connected test.

Pete.

Mate sorry but just this one last

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <RCSwitch.h>

#define relayfan 4
#define relaylight 5
#define relaylightb 14
#define relayrandom 12

#define VPIN_BUTTON_1    V12
#define VPIN_BUTTON_2    V13
#define VPIN_BUTTON_3    V14
#define VPIN_BUTTON_4    V15

RCSwitch mySwitch = RCSwitch();

int relayfanstate = HIGH;
int relaylightstate = HIGH;
int relaylightbstate = HIGH;
int relayrandomstate = HIGH;
int mainstate = HIGH;
long code;


char auth[] = "7k6FzFfP541FKDV-9jwppRFNvR-hDr0n";

char ssid[] = "Nag";
char pass[] = "87654321";
BLYNK_CONNECTED()
{
  Serial.println("blynk connected first function");
  Blynk.syncVirtual(VPIN_BUTTON_1);
  Blynk.syncVirtual(VPIN_BUTTON_2);
  Blynk.syncVirtual(VPIN_BUTTON_3);
  Blynk.syncVirtual(VPIN_BUTTON_4);
}

BLYNK_WRITE(VPIN_BUTTON_1)
{
  Serial.println("blynk write");
  relayfanstate = param.asInt();
  digitalWrite(relayfan, relayfanstate);
}

BLYNK_WRITE(VPIN_BUTTON_2)
{
  Serial.println("blynk write");
  relaylightstate = param.asInt();
  digitalWrite(relaylight, relaylightstate);
}

BLYNK_WRITE(VPIN_BUTTON_3)
{
  Serial.println("blynk write");
  relaylightbstate = param.asInt();
  digitalWrite(relaylightb, relaylightbstate);
}

BLYNK_WRITE(VPIN_BUTTON_4)
{
  Serial.println("blynk write");
  relayrandomstate = param.asInt();
  digitalWrite(relayrandom, relayrandomstate);
}


void setup()
{
  Serial.begin(9600);
  mySwitch.enableReceive(0);
  pinMode(relayfan, OUTPUT);
  pinMode(relaylight, OUTPUT);
  pinMode(relaylightb, OUTPUT);
  pinMode(relayrandom, OUTPUT);
  digitalWrite(relayfan, HIGH);
  digitalWrite(relaylight, HIGH);
  digitalWrite(relaylightb, HIGH);
  digitalWrite(relayrandom, HIGH);
  Blynk.begin(auth, ssid, pass);
  Serial.println("Blynk config done");
}


void loop()
{
  bool connectionstatus = Blynk.connected();
  if (connectionstatus == true)
  {
    Blynk.run;
  }
  if (mySwitch.available())
  {
    code = mySwitch.getReceivedValue();
    read_code(code);
  }
}

void read_code(long cd)
{
  switch (cd)
  {
    case 8594433:
      Serial.println("code matched without internet");
      mainstate = !mainstate;
      digitalWrite(relayfan, mainstate);
      digitalWrite(relaylight, mainstate);
      digitalWrite(relaylightb, mainstate);
      digitalWrite(relayrandom, mainstate);
      Blynk.virtualWrite(VPIN_BUTTON_1, mainstate);
      Blynk.virtualWrite(VPIN_BUTTON_2, mainstate);
      Blynk.virtualWrite(VPIN_BUTTON_3, mainstate);
      Blynk.virtualWrite(VPIN_BUTTON_4, mainstate);
      mySwitch.resetAvailable();
      break;

    case 8594447:
      digitalWrite(relayfan, LOW);
      Blynk.virtualWrite(VPIN_BUTTON_1, LOW);
      mySwitch.resetAvailable();
      break;

    case 8594445:

      digitalWrite(relayfan, HIGH);
      Blynk.virtualWrite(VPIN_BUTTON_1, HIGH);
      mySwitch.resetAvailable();
      break;

    case 8594436:
      relaylightstate = !relaylightstate;
      digitalWrite(relaylight, relaylightstate);
      Blynk.virtualWrite(VPIN_BUTTON_2, relaylightstate);
      mySwitch.resetAvailable();
      break;

    case 8594437:
      relaylightbstate = !relaylightbstate;
      digitalWrite(relaylightb, relaylightbstate);
      Blynk.virtualWrite(VPIN_BUTTON_3, relaylightbstate);
      mySwitch.resetAvailable();
      break;

    case 8594439:
      relayrandomstate = !relayrandomstate;
      digitalWrite(relayrandom, relayrandomstate);
      Blynk.virtualWrite(VPIN_BUTTON_4, relayrandomstate);
      mySwitch.resetAvailable();
      break;
  }
}

is this good enough that even without internet my rf remote should be able to control the relays and the node mcu does not disconnect from blynk frequently?

You don’t need to pass a parameter value to the read_code function. You’ve already saved the
IR value to the code variable, which is global so is available within the read_code function.

So, just do this:

` read_code();`
void read_code()
{
  switch (code)

This comment is not necessarily correct…

as the function is called regardless of whether the connection to the Blynk server is established or not.

By taking this approach, you disconnections will be far fewer.

No, because your code uses the blocking Blynk.begin() command. If there is no connection to the Blynk server (because either the internet or the server is down) then the code won’t progress beyond this point.

If you want to fix that then you need to manually manage your WiFi connection, then use the Blynk.config() command to pass your auth code and Blynk.connect to make the connection to the Blynk server.

Pete.

i did not understand can you put it in code form so that i can understand

i also do not understand this im just checking if blynk is connected regardless of the status it should check for rf code so why wont it progress beyond that and what is blocking Blynk.begin?

There are lots of explanations on the forum, with code examples. Just search for Blynk.config and you’ll find what you need.

Pete.

Mate I’m confused as hell with all the questions and the answers after searching for Blynk.config.

Just one , anywhere in the forum is a code there wherein a task is executed regardless of the wifi connection (reading rf codes in my case). And if wifi is connected just update the state on the app(Blynk.virtualwrite)? And not lose connection frequently obviously.

You need to think of the WiFi connection and Blynk connection as separate things, which they are when you use Blynk.config.

In this situation, you are responsible for establishing the WiFi connection. If this is successful then you can use Blynk.config and Blynk.connect, to define and attempt to initialise the Blynk connection.
There is obviously no point in attempting to connect to Blynk if there is no WiFi connection available.

Pete.