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

Thank you very much for your kind reply PeterKnight.

In my serial monitor, what I see is, connecting to wifi, connected and then the cool Blynk symbol and a successful connection to Blynk plantform. Immediately after that, it starts reconnecting again after 10 seconds or less.

I used the 9600 baud rate. I will try a higher one as you have recommended.
With 115200, I get this:


wdt reset
load 0x4010f000, len 3460, room 16 
tail 4
chksum 0xcc
load 0x3fff20b8, len 40, room 4 
tail 4
chksum 0xc9
csum 0xc9
v000461b0
~ld
[66] Connecting to DIEUDONNE-ANDROID
[4289] Connected to WiFi
[4289] IP: 192.168.43.248
[4289] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v1.0.1 on ESP8266

[4296] Connecting to blynk.cloud:80
[4955] Ready (ping: 300ms).

 ets Jan  8 2013,rst cause:4, boot mode:(3,6)

wdt reset
load 0x4010f000, len 3460, room 16 
tail 4
chksum 0xcc
load 0x3fff20b8, len 40, room 4 
tail 4
chksum 0xc9
csum 0xc9
v000461b0
~ld
[66] Connecting to DIEUDONNE-ANDROID

I used a 5.1 V, 2.1A capable charger, yet I got the same results. I will try using a different cable like you have suggested.

Concerning the digital pins I used, I only used them because I couldn’t have a any output using the Virtual pins. Perhaps it is because I thought D0 to be V0, D1, V1 and so on respectively. Perhaps I was wrong. I would love to have guidance on the Virtual pins and their corresponding pins on the NodeMCU please.

Your “Cause 4” reset is a watchdog timer issue, which is making the device reboot. Not sure why, but maybe something to do with the digital pins.
You could try deleting those datastreams and seeing if you have the same issue.

If you read the link to the virtual pin tutorial that I provided, you’ll realise that there is absolutely no link between virtual pins in Blynk and physical pins on the device.
You need to create that link yourself using pinMode statements, virtialWrite commands and virtual pin handler callback functions.
Sounds complex, but read the tutorial and you’ll see that it’s not really.

BTW, when you post serial output it’s far better to copy/paste it, and use triple backticks to format it correctly, rather than posting screenshots, as it’s impossible to quote pieces of text from a screenshot.

Pete.

Thank you very much. I will try all that you have recommended.
Forgive me for the screenshot. I am using my phone. I will correct that right away.

Greetings sir. I tried all that you recommended. It turns out the digital pins were the main problem after all. After I deleted the datastreams that I created using digital pins, my device became constantly online. I read through your explanation of virtual pins and used the knowledge to build my code. I wish to control four LEDs, using switches as well as the Blynk planform. After writing my code, I am told “Error compiling for board NodeMCU 1.0 (ESP-12E Module).”. Here is my code and I don’t know what I may have done wrong.

#define BLYNK_PRINT Serial

// 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);
  
}

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

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

BLYNK_CONNECTED()
    {
      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() 
{
  Blynk.run();
  
  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);
      }
}

First of all you should clean your void loop.

Should I create a function out of the void loop to execute all that I have put in there? Or should it be in the void setup?

If you would like to execute the code once only put it in the setup, if you would like to execute it continually put it in a void function and call it using a timer.

Alright. Thank you. Please what is the syntax for putting it in a void function and being able to call it using a timer?
And will I I be calling it in the void loop? Because that’s what I have in mind.

Read this article

Thank you… Going through it now.

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.