Nodemcu with blynk constantly keeps connecting and disconnecting

Hi everyone,

This is my first post and I’m pretty new to blynk/nodemcu field.

I use nodemcu (ESP8266) with blynk code to control relays.
Nodemcu connects to a WiFi / router which is not subject to change, so I have to find a solution within the nodemcu itself.

Nodemcu connects to wifi network easily, but disconnects in matter of minutes (1-3), reconnects again, and keeps doing this constantly. Whenever I open the android app I see it changing online/offline status every minute or two. After a longer period of time (sometimes 10 days, sometimes 2 months) nodemcu goes offline and never comes back. Than it has to be restarted manually.

I assume the router might be the problem, but I cannot change neither the router nor its config.

What I would like is the nodemcu restarts itself once it does not connect to blynk server for longer than 10 minutes or so.

I did try to experiment with codes I found within this community, but had no luck so far :frowning:
I am not a programmer and would appreciate very much if someone could help me with this.


#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "xxxxxxxxxx";
char ssid[] = "xxxxxxxxxx";
char pass[] = "xxxxxxxxxx";

void setup()
{
  // Debug console
  Serial.begin(9600);
  WiFi.setSleepMode(WIFI_NONE_SLEEP);
  WiFi.setAutoReconnect(true);
  WiFi.persistent(true);

  pinMode(16, OUTPUT); //D5 is GPIO 16 
  pinMode(5, OUTPUT); //D7 is GPIO 5 
  pinMode(4, OUTPUT); //D7 is GPIO 4 
  pinMode(0, OUTPUT); //D7 is GPIO 0 
  pinMode(14, OUTPUT); //D5 is GPIO 14 
  pinMode(12, OUTPUT); //D7 is GPIO 12 
  pinMode(13, OUTPUT); //D7 is GPIO 13 
  pinMode(2, OUTPUT); //D7 is GPIO 2
  digitalWrite(16, HIGH); //Set GPIO 16 LOW
  digitalWrite(5, HIGH); // Set GPIO 5 LOW
  digitalWrite(4, HIGH); // Set GPIO 4 LOW
  digitalWrite(0, HIGH); // Set GPIO 0 LOW
  digitalWrite(14, HIGH); //Set GPIO 14 LOW
  digitalWrite(12, HIGH); // Set GPIO 12 LOW
  digitalWrite(13, HIGH); // Set GPIO 13 LOW
  digitalWrite(2, HIGH); // Set GPIO 2 LOW
    
  Blynk.begin(auth, ssid, pass);
}

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

What device type did you choose in your app project, and what digital pins do you have widgets attached to in the app?

Pete.

Thank you for the reply.
I think I understand the first part of the question :slight_smile:
Board: “NodeMCU 0.9 (ESP-12 Module)” > ESP8266 Boards (3.0.3) > NodeMCU 0.9 (ESP-12 Module)

Regarding the second part, if this is what you ask, 4 buttons in app control the following outputs: D0, D1, D2 and D3.

P.S. It should be something like the code below, but it says strictly not to use delay within loop:

void loop()
{
  Blynk.run();
  delay(10000);
  ESP.restart();
}

That’s the board type you chose in the Arduino IDE.
I want to know the board type you chose in the Blynk app when you created the project and added the device (nut icon, devices, device, device settings).

Why do you have pinMode statements for some of these pins in your void setup?
What is the purpose of the other pinMode statements?
Why do your comments for these pinMode statements all state that the GPIOs all translate to either D7 or D5 ?

I would remove these lines of code if I were you…

That is absolutely the last thing you should do if you don’t want the device to reboot and re-connect every 10 seconds - making it virtually unusable with Blynk.

Pete.

1 Like

I choose NodeMCU. Connection type WiFi.

I use that code to have relays “opened” (off) when nodemcu starts.
If these statements are excluded, whenever the nodemcu restarts, relays are “closed” (on).
The relay board has 8 relays, that is the reason for commenting all those pins, but at the moment, I use only 4 of them through the app.

OK. It did not change anything so far.

My bad. It was just an example. I was thinking of putting delay of 86.400.000 to make nodemcu restart once a day, but it says I should avoid using delay () within void loop ().

Read this first

and avoid delays.
You can use blynk timer instead, more details here

I wold start by simplifying your code and app setup so that you do nothing other than connect to Blynk within the sketch, and have no widgets in the app.
Observe what happens with your Blynk connection.

If it still disconnects then the problem lies either with your NodeMCU, Router or ISP.

If it stays online then the problem lies with your sketch or app setup.

I’d always recommend using virtual pins (which do require a pinMode statement in your sketch). Read this:

You will also see from that tutorial how to synchronise your relays with the app when the device boots-up or re-connects with Blynk.

What would happen then is that the code would enter the void loop, execute the Blynk.run command then do nothing for 1 day. At the end of that day, the next line of code would be executed, which would restart the device.
10 seconds into this one day delay, the Blynk server would disconnect your device because it had stopped doing handshakes with the server - which are part of the Blynk.run command.

This is because delay() is a blocking command. It blocks all code execution, and therefore freezes the device, until the delay period has completed.

On a different note, it your device, router and ISP turn-out to be fine, I’d recommend migrating to Blynk IoT, as the version you are currently using has a limited life and is no longer being developed or supported…

Pete.

Thank you for the comprehensive guidance.

What I am certain of is that my initial code works very well on other location/wifi. It stays connected for months without problems. Thing is I cannot do anything about the problematic location/rooter/wifi setup, so the only certain solution for me is to set the nodemcu to restart once a day, which I still haven’t figured out.

You can use blynk timer instead of delay, create a void function and call it using a timer, something like this

void restart() 
{
  ESP.restart();
}

and in the void setup add this line

timer.setInterval(3,600,000L, restart); //restart the nodemcu every hour
1 Like

Thanks John93. I think that is something I am looking for, just to make it work now :slight_smile:

This is how I did it, and it doesn’t work so far. I changed it to one minute for testing purposes.


#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

BlynkTimer timer;

char auth[] = "xxxx";
char ssid[] = "xxxx";
char pass[] = "xxxx";

void restart() 
{
  ESP.restart();
}
 
void setup()
{
  Serial.begin(9600);
  WiFi.setSleepMode(WIFI_NONE_SLEEP);
  WiFi.setAutoReconnect(true);
  WiFi.persistent(true);

  pinMode(16, OUTPUT);
  pinMode(5, OUTPUT); 
  pinMode(4, OUTPUT);
  pinMode(0, OUTPUT);
  pinMode(14, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(2, OUTPUT);
  digitalWrite(16, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(0, HIGH);
  digitalWrite(14, HIGH);
  digitalWrite(12, HIGH);
  digitalWrite(13, HIGH);
  digitalWrite(2, HIGH);

  timer.setInterval(60.000L, restart); //restart the nodemcu every hour
  Blynk.begin(auth, ssid, pass);

}

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

If you’d taken the time to read my tutorial, which John linked in post #6, then you’d realise that you are missing the timer.run() command in your void loop.

Pete.

2 Likes

Dear Pete and John,

First of all, thank you both for helping me solve this. It works like a charm now. I’ve set it to restart ever 24h and will set it up at the location with the problematic router/wifi. I did read lot of resources including your comprehensive guide on how to use the BlynkTimer, but since I’m a rookie it is a bit tricky to connect all the dots. Not an excuse.

For the sake of other people wishing to do something similar, here’s the code I use to control an 8-channel relay board with a 24-hour restart function.

All the best :beers:

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

BlynkTimer timer;

char auth[] = "xxx";
char ssid[] = "xxx";
char pass[] = "xxx";

void restart() 
{
  ESP.restart();
}
 
void setup()
{
  // Debug console
  Serial.begin(9600);
  WiFi.setSleepMode(WIFI_NONE_SLEEP);
  WiFi.setAutoReconnect(true);
  WiFi.persistent(true);

  pinMode(16, OUTPUT);
  pinMode(5, OUTPUT); 
  pinMode(4, OUTPUT);
  pinMode(0, OUTPUT);
  pinMode(14, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(2, OUTPUT);
  digitalWrite(16, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(0, HIGH);
  digitalWrite(14, HIGH);
  digitalWrite(12, HIGH);
  digitalWrite(13, HIGH);
  digitalWrite(2, HIGH);

  timer.setInterval(86400000.000L, restart); //restart nodemcu every 24h
  
  Blynk.begin(auth, ssid, pass);
}

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

For anyone who reads this in the future, a few comments that you may have missed if you didn’t study the rest of the topic…

  1. this is a Blynk Legacy project and will not work with Blynk IoT without modification. Blynk Legacy is now discontinued and unsupported, and will cease to function at some point in the future when the legacy cloud servers are decommissioned.

  2. this project uses digital pins rather than virtual pins to control the relays. This is a bad approach, and one that I would strongly recommend avoiding.

  3. this project has no code to synchronise the device with the app, or the app with the device. This means that when the device restarts (which it will do once every 24 hours) the relays will be turned off by the digitalWrite commands in the void setup, but the app will not reflect this change, so the user will not know if relays are on or off by looking at the app.

  4. it should not be necessary to restart a device one per day. This is a clunky workaround to an issue that appears to be caused by the router or ISP.

  5. some of the GPIO pins used in this sketch are not ideal pins to use with a NodeMCU. In reality, using a NodeMCU to control an 8-way relay board is asking too much. It would be far better to use an ESP32 board, or an ESP8266 with a multiplexer board like the MCP23017 or 74HC595, as described here:

and here:

Pete.

2 Likes