NodeMCU with Blynk goes offline and back online as frequent as 10 seconds

You should have just one BLYNK_CONNECTED callback function, with all three Blynk.syncVirtual(vPin) commands within it. That’s what’s causing your compilation error.

You should read this:

Pete.

1 Like

Thank you. I have followed all the syntax and merged the BLYNK_CONNECTED() sections to one. My error message now is that " ‘BlynkTimer’ does not name a type". I am now quite confused because I installed the simple timer library.

Post the updated sketch please.

Thank you John93. Here is the new sketch:

#define BLYNK_PRINT Serial
BlynkTimer timer;
// device name info from app
#define BLYNK_TEMPLATE_ID "TMPLlnQxJk6j"
#define BLYNK_DEVICE_NAME "Automation"


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

char auth[] = "was entered";

char ssid[] = "DIEUDONNE-ANDROID";
char pass[] = "13111.PD";

//--------------------------------------------------------------------------------------------------------------------//

int switch1=12;
int led1=5;
int s1;

int switch2=13;
int led2=4;
int s2;

int switch3=15;
int led3=14;
int s3;

String s1_on="Switch 1 is ON";
String s2_on="Switch 2 is ON";
String s3_on="Switch 3 is ON";

String s1_off="Switch 1 is OFF";
String s2_off="Switch 2 is OFF";
String s3_off="Switch 3 is OFF";



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

  Blynk.begin(auth, ssid, pass);
  
  pinMode(switch1,INPUT);
  pinMode(led1,OUTPUT);

  pinMode(switch2,INPUT);
  pinMode(led2,OUTPUT);
  
  pinMode(switch3,INPUT);
  pinMode(led3,OUTPUT);

  timer.setInterval(5000L, manual_control);
}

BLYNK_CONNECTED()
    {
      Blynk.syncVirtual(V1); 
      Blynk.syncVirtual(V2); 
      Blynk.syncVirtual(V3); 
    }

BLYNK_WRITE(V1)
        {
          if(param.asInt() == 1)
              { 
                digitalWrite(led1,HIGH);  
              }
          else
              {
                digitalWrite(led1,LOW);      
              }
        }
BLYNK_WRITE(V2)
        {
          if(param.asInt() == 1)
              { 
                digitalWrite(led2,HIGH);  
              }
          else
              {
                digitalWrite(led2,LOW);      
              }
        }
BLYNK_WRITE(V3)
        {
          if(param.asInt() == 1)
              { 
                digitalWrite(led3,HIGH);  
              }
          else
              {
                digitalWrite(led3,LOW);      
              }
        }

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

void manual_control()
  {
  s1=digitalRead(switch1);
  s2=digitalRead(switch2);
  s3=digitalRead(switch3);
  
  if (s1==HIGH)
      {
        digitalWrite(led1,HIGH);
        Serial.println(s1_on);
        Blynk.virtualWrite(V1,1);
      }
   else
      { 
        digitalWrite(led1,LOW);
        Serial.println(s1_off);
        Blynk.virtualWrite(V1,0);
      }
  if (s2==HIGH)
      {
        digitalWrite(led2,HIGH);
        Serial.println(s2_on);
        Blynk.virtualWrite(V2,1);
      }
   else
      { 
        digitalWrite(led2,LOW);
        Serial.println(s2_off);
        Blynk.virtualWrite(V2,0);
      }
   if (s3==HIGH)
      {
        digitalWrite(led3,HIGH);
        Serial.println(s3_on);
        Blynk.virtualWrite(V3,1);
      }
   else
      { 
        digitalWrite(led3,LOW);
        Serial.println(s3_off);
        Blynk.virtualWrite(V3,0);
      }
  }

This line of code:

needs to go after this line of code:

because it’s the BlynkSimpleEsp8266.h library which contains the reference to the BlynkTimer / SimpleTimer library.

By having the line that creates the timer object before the line that references the BlynkTimer library the compiler can’t make sense of the sketch.

Always have your #defines and #includes at the top of the sketch, and everything else after them.

Pete.

1 Like

Thank you sir, it has compiled successfully. Another problem sir, the manual part of my code isn’t executing. Is it because I used the BlynkTimer and it requires a connection?
And please sir, what do you recommend I do?

You are only polling your physical switched once every 5 seconds. This means that if these switches are push-button switches then they need to be pushed for up to 5 seconds before that button push is detected by the sketch.

BlynkTimer doesn’t require a connection of any type, but Blynk.begin is a blocking function, so code execution will stop at that point of a connection with WiFi or the Blynk server can’t be established.

If you require manual control via the physical switches in an off-line situation, you need to manually manage your WiFi connection, then use Blynk.config and Blynk.begin, as these don’t block code execution in the same way. You should however avoid calling Blynk.run when your device isn’t connected to WiFi, or to the Blynk server, as this will cause an attempted re-connection to the Blynk server, and you’ll need to wait until the Blynk.connect timeout period has ended until code execution can continue.

Pete.

Thank you sir. Though I must admit that it sounds kind of complex.
Should I create two functions, one that runs when offline and one that runs online? With the one online incorporating the one that runs offline?

I have tried using the Blynk.config in place of the Blynk.begin (that’s if you meant I do so), and I got an error message of nvalid conversion from ‘char’ to ‘uint16_t’.

No, you can’t replace one command with another.

This issue has been covered many times before on the forum. If you search for Blynk.config and using Blynk when offline you’ll find lots of examples.

Pete.

Thank you sir. I will research on it.