Nodemcu stays in connect but it get divice offline within a day

My device working one day after go yo offline
I changed port 8080 same problem
After i changed 8442 same problem
not working i need help plz
Better solution i except

I’d start by providing information about which version of Blynk you are using, which library you are using, and posting your sketch (correctly formatted of course) along with any serial monitor output that may be helpful to diagnosing the issue.

Pete.

my blynk app version 2.27.30
my sketch
4 relay control and 1 motion deducted send notify

my library =https://github.com/blynkkk/blynk-library

#include <SoftwareSerial.h>
#define BLYNK_PRINT Serial

//#define BLYNK_MAX_SENDBYTES 128
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char ssid[] = "user name";
char pass[] = "password";

char auth[] = "token";

#define BLYNK_PRINT Serial


int val = 0; 
int pirState;
int x = 0;
WidgetLCD lcd(V1);
WidgetLCD lcd2(V5);
WidgetLCD lcd3(V7);
int ReCnctFlag;  // Reconnection Flag
int ReCnctCount = 0;  
BlynkTimer timer;

void resetMCU()
{
#if defined(ARDUINO_ARCH_MEGAAVR)
  wdt_enable(WDT_PERIOD_8CLK_gc);
#elif defined(__AVR__)
  wdt_enable(WDTO_15MS);
#elif defined(__arm__)
  NVIC_SystemReset();
#elif defined(ESP8266) || defined(ESP32)
  ESP.restart();
#else
  #error "MCU reset procedure not implemented"
#endif
  for (;;) {}
}



BLYNK_WRITE(V0)   
{

  if(param.asInt() == 0){
    pinMode(D0, LOW);
      lcd.print(0, 0, "SWITCH 01 OFF"); 
  }else{
     pinMode(D0, HIGH);
      lcd.print(0, 0, "SWITCH 01 ONN"); 
  }
   
}

BLYNK_WRITE(V2)   
{

  if(param.asInt() == 0){
    pinMode(D1, LOW);
     lcd.print(0, 1, "SWITCH 02 OFF"); 
  }else{
     pinMode(D1, HIGH);
      lcd.print(0, 1, "SWITCH 02 ONN"); 
  }
   
}

BLYNK_WRITE(V3)   
{

  if(param.asInt() == 0){
    pinMode(D5, LOW);
  lcd2.print(0, 0, "SWITCH 03 OFF"); 
  }else{
     pinMode(D5, HIGH);
 lcd2.print(0, 0, "SWITCH 03 ONN"); 
  }
   
}

#define pirPin D4               
int pirValue;   
char replybuffer[255];             
int pinValue;  
SoftwareSerial sim(10, 11);
int _timeout;
String _buffer;

BLYNK_WRITE(V4)   
{
  
  if(param.asInt() == 0){
    pinMode(D3, LOW);
     lcd2.print(0, 1, "SWITCH 04 OFF"); 
  }else{
     pinMode(D3, HIGH);
      lcd2.print(0, 1, "SWITCH 04 ONN"); 
  }
   
}

BLYNK_WRITE(V6)   
{
 pinValue = param.asInt();    
  if(param.asInt() == 0){
    lcd3.clear();
     lcd3.print(0, 1, "HUMAN OFF"); 
  }else{
      lcd3.print(0, 1, "HUMAN ONN"); 
  }
   
}

BLYNK_CONNECTED()
{

}

int calibrationTime = 30;
long unsigned int lowIn;
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
int PIRValue = 0;

void initWiFi() {
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, pass);
  Serial.print("Connecting to WiFi ..");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(1000);
  }
  Serial.println(WiFi.localIP());
  //The ESP8266 tries to reconnect automatically when the connection is lost
  WiFi.setAutoReconnect(true);
  WiFi.persistent(true);
}

void setup() { 
  
   Serial.begin(9600);
   
   _buffer.reserve(50);
  Serial.println("System Started...");


  pinMode(D0, OUTPUT);
  pinMode(D1, OUTPUT);
  pinMode(D5, OUTPUT);
  pinMode(D3, OUTPUT);

    pinMode(D0, LOW);
  pinMode(D1, LOW);
  pinMode(D5, LOW);
  pinMode(D3, LOW);
  initWiFi();
 
  
  pinMode(pirPin, INPUT_PULLUP);
  
  Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);

   lcd.clear(); 

}

void loop() {
if(WiFi.status() != WL_CONNECTED){
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(1000);
    ESP.restart();
  }
}else{
  
  if (Blynk.connected()) { 
  Blynk.run(); 
      timer.run();
      Blynk.syncVirtual(V0);  
      Blynk.syncVirtual(V2); 
        Blynk.syncVirtual(V3);  
          Blynk.syncVirtual(V4);
      notifiaction();

      // If connected run as normal
    
      
  } else {
    
    if (ReCnctFlag == 0) {
      ESP.restart();
      resetMCU();
    }
    ReCnctFlag = 1;  // Set reconnection Flag
    Serial.println("Starting reconnection timer in 30 seconds...");
    timer.setTimeout(30000L, []() {  // Lambda Reconnection Timer Function
      ReCnctFlag = 0;  // Reset reconnection Flag
      ReCnctCount++;  // Increment reconnection Counter
      Serial.print("Attempting reconnection #");
      Serial.println(ReCnctCount);
      Blynk.connect();  // Try to reconnect to the server
    });
    while (Blynk.connect() == false)
        {
          ; // Wait until connected
        }
    // END Timer Function
  }
}

   

  
 
  
}
void notifiaction()
{
  if(pinValue){
    int sensor = digitalRead(pirPin);

    if (sensor == 1) {
      Serial.println("WARNING! Please check your security system");
         Blynk.notify("WARNING! Please check your security system");  
      lcd3.print(0, 0, "MOTION DETACTED.."); 
    }else{
       lcd3.print(0, 0, "MOTION  NOT DETACTED.."); 
    }
  }
 
}```


serial monitor output
```⸮⸮System Started...
Connecting to WiFi ......192.168.8.107
[4793] Connecting to Dialog 4G 045
[4793] Connected to WiFi
[4793] IP: 192.168.8.107
[4793] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v1.0.1 on ESP8266

[4923] Connecting to blynk-cloud.com:8442
[5145] Ready (ping: 111ms).```

@Mohamed_Aflal please edit your post, using pencil icon at the bottom, and add triple backticks at the beginning and end of your code and your serial monitor output so that they display correctly.
Triple backticks look like this:
```

Pete.

@PeteKnight Thank you for replying…
I have edited the code and serial monitor…
Please check it.

You’re running Blynk Legacy , so should be using library version 0.6.1

Your problem is your void loop.

What do you think the Blynk.syncVirtual(vPin) command does, snd why are you calling it on every cycle of the void loop, along with your notification function?

Pete.

@PeteKnight
I used the command Blynk.syncVirtual to sync blynk.
I don’t know whether it is correct or wrong.Can you Please rewrite the sketch to make the device stay online permanently?And also attach the supported blynk library and supported port.

Thank you.I really appreciate the help.

Blynk.syncVirtual(vPin) is used to force the server to send the lates value for the VPin to the hardware, triggering the corresponding BLYNK_WRITE(Vpin) callback function.

However, this is only needed when the device first connects to the Blynk server, or when it re-connects.
To achieve this, you normally put the Blynk.syncVirtual(vPin) in a BLYNK_CONNECTED callback function, which executes once when the device connects/reconnects to Blynk.

BLYNK_CONNECTED is very different to Blynk.connected, which is used to check if the device is currently vonnevted to Blynk and it returns a true/false value.

No, you need to do that, and in the process learn how to code correctly with Blynk.

The library is available in the Arduino IDE. The correct port is 8080.

Pete.

@PeteKnight
Thanks for replying sir
The library I already mentioned is working good?
I will try it.
come to the same problem.
I will connect you
Thanks @PeteKnight

Version 0.6.1 of the library works very well with Blynk Leegacy, but anything since then is designed for Blynk IoT

You may also want to read this:

http://help.blynk.cc/getting-started-library-auth-token-code-examples/blynk-basics/keep-your-void-loop-clean

And the documentation for Blynk Legacy…

Pete.

@PeteKnight
Thanks sir for replying and solved the problem.

Hi sir need help again
Iam changed the coding but coming same offline problem sir after 2 days

This is changed sketche

#include <SoftwareSerial.h>
#define BLYNK_PRINT Serial

//#define BLYNK_MAX_SENDBYTES 128
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char ssid[] = "xxxxxxx";
char pass[] = "xxxxxxxx";

char auth[] = "auth";

#define BLYNK_PRINT Serial


int val = 0; 
int pirState;
int x = 0;
WidgetLCD lcd(V1);
WidgetLCD lcd2(V5);
WidgetLCD lcd3(V7);
int ReCnctFlag;  // Reconnection Flag
int ReCnctCount = 0;  
BlynkTimer timer;

BLYNK_WRITE(V0)   
{

  if(param.asInt() == 0){
    pinMode(D0, LOW);
      lcd.print(0, 0, "SWITCH 01 OFF"); 
  }else{
     pinMode(D0, HIGH);
      lcd.print(0, 0, "SWITCH 01 ONN"); 
  }
   
}

BLYNK_WRITE(V2)   
{

  if(param.asInt() == 0){
    pinMode(D1, LOW);
     lcd.print(0, 1, "SWITCH 02 OFF"); 
  }else{
     pinMode(D1, HIGH);
      lcd.print(0, 1, "SWITCH 02 ONN"); 
  }
   
}

BLYNK_WRITE(V3)   
{

  if(param.asInt() == 0){
    pinMode(D5, LOW);
  lcd2.print(0, 0, "SWITCH 03 OFF"); 
  }else{
     pinMode(D5, HIGH);
 lcd2.print(0, 0, "SWITCH 03 ONN"); 
  }
   
}

#define pirPin D4               
int pirValue;   
char replybuffer[255];             
int pinValue;  
SoftwareSerial sim(10, 11);
int _timeout;
String _buffer;

BLYNK_WRITE(V4)   
{
  
  if(param.asInt() == 0){
    pinMode(D3, LOW);
     lcd2.print(0, 1, "SWITCH 04 OFF"); 
  }else{
     pinMode(D3, HIGH);
      lcd2.print(0, 1, "SWITCH 04 ONN"); 
  }
   
}

BLYNK_WRITE(V6)   
{
 pinValue = param.asInt();    
  if(param.asInt() == 0){
    lcd3.clear();
     lcd3.print(0, 1, "HUMAN OFF"); 
  }else{
      lcd3.print(0, 1, "HUMAN ONN"); 
  }
   
}

BLYNK_CONNECTED(){
  Blynk.syncAll();  
}

int calibrationTime = 30;
long unsigned int lowIn;
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
int PIRValue = 0;

void initWiFi() {
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, pass);
  Serial.print("Connecting to WiFi ..");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(1000);
  }
  Serial.println(WiFi.localIP());
  //The ESP8266 tries to reconnect automatically when the connection is lost
  WiFi.setAutoReconnect(true);
  WiFi.persistent(true);
}




void setup() { 
  
   Serial.begin(9600);
   
   _buffer.reserve(50);
  Serial.println("System Started...");


  pinMode(D0, OUTPUT);
  pinMode(D1, OUTPUT);
  pinMode(D5, OUTPUT);
  pinMode(D3, OUTPUT);

  pinMode(D0, LOW);
  pinMode(D1, LOW);
  pinMode(D5, LOW);
  pinMode(D3, LOW);
  initWiFi();
 
  
  pinMode(pirPin, INPUT_PULLUP);
  
  Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8080);

  lcd.clear(); 

  timer.setInterval(2500L, notifiaction);
}

void loop() {
  if(WiFi.status() != WL_CONNECTED){
    while (WiFi.status() != WL_CONNECTED) {
      Serial.print('.');
      delay(1000);
      ESP.restart();
    }
  }else{
      Blynk.run(); 
      timer.run();
  }
}

void notifiaction()
{
  if(pinValue){
    int sensor = digitalRead(pirPin);

    if (sensor == 1) {
      Serial.println("WARNING! Please check your security system");
         Blynk.notify("WARNING! Please check your security system");  
      lcd3.print(0, 0, "MOTION DETACTED.."); 
    }else{
       lcd3.print(0, 0, "MOTION  NOT DETACTED.."); 
    }
  }
 
}```

I don’t understand why you are managing the WiFi connection yourself, but then using Blynk.begin rather than Blynk.config and Blynk.connect

Pete.

Sir i changed my sketch please check

#include <SoftwareSerial.h>
#define BLYNK_PRINT Serial

//#define BLYNK_MAX_SENDBYTES 128
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char ssid[] = "xxxxxx";
char pass[] = "xxxxxxx";

char auth[] = "auth token";

#define BLYNK_PRINT Serial


int val = 0; 
int pirState;
int x = 0;
WidgetLCD lcd(V1);
WidgetLCD lcd2(V5);
WidgetLCD lcd3(V7);
int ReCnctFlag;  // Reconnection Flag
int ReCnctCount = 0;  
BlynkTimer timer;

BLYNK_WRITE(V0)   
{

  if(param.asInt() == 0){
    pinMode(D0, LOW);
      lcd.print(0, 0, "SWITCH 01 OFF"); 
  }else{
     pinMode(D0, HIGH);
      lcd.print(0, 0, "SWITCH 01 ONN"); 
  }
   
}

BLYNK_WRITE(V2)   
{

  if(param.asInt() == 0){
    pinMode(D1, LOW);
     lcd.print(0, 1, "SWITCH 02 OFF"); 
  }else{
     pinMode(D1, HIGH);
      lcd.print(0, 1, "SWITCH 02 ONN"); 
  }
   
}

BLYNK_WRITE(V3)   
{

  if(param.asInt() == 0){
    pinMode(D5, LOW);
  lcd2.print(0, 0, "SWITCH 03 OFF"); 
  }else{
     pinMode(D5, HIGH);
 lcd2.print(0, 0, "SWITCH 03 ONN"); 
  }
   
}

#define pirPin D4               
int pirValue;   
char replybuffer[255];             
int pinValue;  
SoftwareSerial sim(10, 11);
int _timeout;
String _buffer;

BLYNK_WRITE(V4)   
{
  
  if(param.asInt() == 0){
    pinMode(D3, LOW);
     lcd2.print(0, 1, "SWITCH 04 OFF"); 
  }else{
     pinMode(D3, HIGH);
      lcd2.print(0, 1, "SWITCH 04 ONN"); 
  }
   
}

BLYNK_WRITE(V6)   
{
 pinValue = param.asInt();    
  if(param.asInt() == 0){
    lcd3.clear();
     lcd3.print(0, 1, "HUMAN OFF"); 
  }else{
      lcd3.print(0, 1, "HUMAN ONN"); 
  }
   
}

BLYNK_CONNECTED(){
  Blynk.syncAll();  
}

int calibrationTime = 30;
long unsigned int lowIn;
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
int PIRValue = 0;

void setup() { 
  
   Serial.begin(9600);
   
  _buffer.reserve(50);
  Serial.println("System Started...");


  pinMode(D0, OUTPUT);
  pinMode(D1, OUTPUT);
  pinMode(D5, OUTPUT);
  pinMode(D3, OUTPUT);

  pinMode(D0, LOW);
  pinMode(D1, LOW);
  pinMode(D5, LOW);
  pinMode(D3, LOW);
 
  pinMode(pirPin, INPUT_PULLUP);
  
  Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8080);

  lcd.clear(); 

  timer.setInterval(2500L, notifiaction);
}

void loop() {
    Blynk.run(); 
    timer.run();
}

void notifiaction()
{
  if(pinValue){
    int sensor = digitalRead(pirPin);

    if (sensor == 1) {
      Serial.println("WARNING! Please check your security system");
         Blynk.notify("WARNING! Please check your security system");  
      lcd3.print(0, 0, "MOTION DETACTED.."); 
    }else{
       lcd3.print(0, 0, "MOTION  NOT DETACTED.."); 
    }
  }
 
}```

```⸮⸮System Started...
[67] Connecting to Aflal
[4294] Connected to WiFi
[4295] IP: 192.168.189.48
[4295] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on ESP8266

[4373] Connecting to blynk-cloud.com:8080
[4712] Ready (ping: 125ms).```

As I said, I would have gone the other way.

Pete.

Sir,i couldn’t understand what you are saying.
Can you please repeat it?

Pete.

Sir,How can i change the sketch?
Can you give some examples?