nodeMCU is not reconnecting after a wifi disruption

Hi Costas,
I have implemented your MyWifi function in my code. But still the nodeMCU is not reconnecting after a wifi disruption.
Actually it attempts to reconnect once. Then an error stack comes and freezes. I am sure that it is due to some issue in my coding, but I can’t find out what it is. I was trying to figure it out for the last 3 days.
Will you please look in to the stack error and code.

#define BLYNK_PRINT Serial

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

char auth[] = "xxx";
char ssid[] = "xxx";
char pass[] = "xxx";
bool Connected2Blynk = false;

BlynkTimer timer;

void NotifyF()
  {
    Serial.println("Front Door is opened");
    Blynk.notify("Front Door is opened!");
    Blynk.email("eldhog@gmail.com", " Front Door is opened", "Front Door is opened");
  }
 
void NotifyBout()
  {
    Serial.println("Back-outside Door is opened");
    Blynk.notify("Back-outside Door is opened!");
    Blynk.email("eldhog@ggmail.com", " Back-outside Door is opened", "Back-outside Door is opened");
  }  

void NotifyBins()
  {
    Serial.println("Back-inside Door is opened");
    Blynk.notify("Back-inside Door is opened!");
    Blynk.email("eldhog@gmail.com", " Back-inside Door is opened", "Back-inside Door is opened");
  }

void NotifyTop()
  {
    Serial.println("Top Door is opened");
    Blynk.notify("Top Door is opened!");
    Blynk.email("eldhog@gmail.com", " Top Door is opened", "Top Door is opened");
  }
  
void NotifyMF()
  {
    Serial.println("Motion is detected-Front");
    Blynk.notify("Motion is detected-Front!");
    Blynk.email("eldhog@gmail.com", " Motion is detected-Front", "Motion is detected-Front");
  }

void NotifyMB()
  {
    Serial.println("Motion is detected-Back");
    Blynk.notify("Motion is detected-Back!");
    Blynk.email("eldhog@gmail.com", " Motion is detected-Back", "Motion is detected-Back");
  }

  int btnState = LOW;
void HooterStatus() 
  {
  int newBtnState = digitalRead(D3);
  if (btnState != newBtnState) 
    {
    btnState = newBtnState;
    Blynk.virtualWrite(V3, btnState);
    }
  }

BLYNK_WRITE(V3) 
  {
  digitalWrite(D3, param.asInt());
  HooterStatus();
  }

void FDoorStatus()
 {
  if (digitalRead(D1)==HIGH) {
    Blynk.virtualWrite(V1, 25);
  }
  else {
    Blynk.virtualWrite(V1, 255);
  }
 }

void BoutDoorStatus()
 {
  if (digitalRead(D2)==HIGH) {
    Blynk.virtualWrite(V2, 25);
  }
  else {
    Blynk.virtualWrite(V2, 255);
  }
 }

void BinsDoorStatus()
 {
  if (digitalRead(D5)==HIGH) {
    Blynk.virtualWrite(V5, 25);
  }
  else {
    Blynk.virtualWrite(V5, 255);
  }
 }

 void TDoorStatus()
 {
  if (digitalRead(D6)==HIGH) {
    Blynk.virtualWrite(V6, 25);
  }
  else {
    Blynk.virtualWrite(V6, 255);
  }
 }

 void MFStatus()
 {
  if (digitalRead(D7)==HIGH) {
    Blynk.virtualWrite(V7, 255);
  }
  else {
    Blynk.virtualWrite(V7, 25);
  }
 }

 void MBStatus()
 {
  if (digitalRead(D8)==HIGH) {
    Blynk.virtualWrite(V8, 255);
  }
  else {
    Blynk.virtualWrite(V8, 25);
  }
 }

 void ElecStatus()
 {
  if (digitalRead(D0)==HIGH) {
    Blynk.virtualWrite(V0, 25);
  }
  else {
    Blynk.virtualWrite(V0, 255);
  }
 } 
 

void MyWiFi(){
  int mytimeout = millis() / 1000;
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    if((millis() / 1000) > mytimeout + 3){ // try for less than 4 seconds to connect to WiFi router
      break;
    }
  }

  if(WiFi.status() == WL_CONNECTED){  
    Serial.print("\nIP address: ");
    Serial.println(WiFi.localIP()); 
  }
  else{
    Serial.println("\nCheck Router ");    
  }
  Blynk.config(auth);
  Connected2Blynk = Blynk.connect(1000);  // 1000 is a timeout of 3333 milliseconds 
  mytimeout = millis() / 1000;
  while (Blynk.connect(1000) == false) { 
    if((millis() / 1000) > mytimeout + 3){ // try for less than 4 seconds
      break;
    }
  }  
}

void CheckConnection(){
  Connected2Blynk = Blynk.connected();
  if(!Connected2Blynk){
    Serial.println("Not connected to Blynk server");
    MyWiFi();  
  }
  else{
    Serial.println("Still connected to Blynk server");    
  }
}

void setup()
{
  Serial.begin(9600);
  delay(10);
  timer.setInterval(11000L, CheckConnection); // check if still connected every 11 seconds
  Serial.println("\nStarted");
  MyWiFi();
  

  pinMode(D0, INPUT);
  
  pinMode(D3, OUTPUT);

  pinMode(D4, OUTPUT);
    
  pinMode(D1, INPUT);
  attachInterrupt(digitalPinToInterrupt(D1), NotifyF, FALLING);

  pinMode(D2, INPUT);
  attachInterrupt(digitalPinToInterrupt(D2), NotifyBout, FALLING);

  pinMode(D5, INPUT);
  attachInterrupt(digitalPinToInterrupt(D5), NotifyBins, FALLING);

  pinMode(D6, INPUT);
  attachInterrupt(digitalPinToInterrupt(D6), NotifyTop, FALLING);

  pinMode(D7, INPUT);
  attachInterrupt(digitalPinToInterrupt(D7), NotifyMF, RISING);

  pinMode(D8, INPUT);
  attachInterrupt(digitalPinToInterrupt(D8), NotifyMB, RISING);

  
  timer.setInterval(504L, HooterStatus);
  timer.setInterval(953L, FDoorStatus);
  timer.setInterval(1000L, BoutDoorStatus);
  timer.setInterval(600L, BinsDoorStatus);
  timer.setInterval(1058L, TDoorStatus);
  timer.setInterval(1022L, MFStatus);
  timer.setInterval(896L, MBStatus);
  timer.setInterval(1168L, ElecStatus);
  
}

void loop()
{
    if(Connected2Blynk){
    Blynk.run();  // only process Blyk.run() function if we are connected to Blynk server
  }
  timer.run();
}

COM dispays this

Still connected to Blynk server
Still connected to Blynk server
Still connected to Blynk server
[1652020] Connecting to blynk-cloud.com:80

Exception (28):
epc1=0x402045e1 epc2=0x00000000 epc3=0x00000000 excvaddr=0x000000bb depc=0x00000000

ctx: cont 
sp: 3fff0780 end: 3fff0a10 offset: 01a0

>>>stack>>>
3fff0920:  3ffef6bc 00000000 3fff1954 402045da  
3fff0930:  2bcea6bc 0000000e 3ffe8c42 3ffe8c34  
3fff0940:  3ffe88a8 00000002 3ffef928 3ffe8c34  
3fff0950:  3ffe88a8 00000050 3ffef6bc 402041a4  
3fff0960:  3ffe93d8 2bcea6bc 3ffe93d8 2bcea6bc  
3fff0970:  3ffe8c34 3ffef928 3ffef928 40204fdc  
3fff0980:  3fffdad0 3ffef928 3ffef6a8 4020252c  
3fff0990:  3fff0590 3ffef6a8 3ffef684 40204a10  
3fff09a0:  000000ff 00000000 3ffef684 00000000  
3fff09b0:  00193534 00000000 3ffef684 40202dfa  
3fff09c0:  00000005 3ffef43c 3ffef9e0 40204a24  
3fff09d0:  3fffdad0 3ffef928 3ffef43c 3ffef9e8  
3fff09e0:  3fffdad0 00000000 3ffef9e0 40203c1c  
3fff09f0:  3fffdad0 00000000 3ffef9e0 402053b0  
3fff0a00:  feefeffe feefeffe 3ffef9f0 40100710  
<<<stack<<<
zn⸮⸮⸮⸮

And Exceptio Decoder says

Decoding 9 results
0x402045da: WiFiClient::connect(IPAddress, unsigned short) at C:\Users\Sherin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0\libraries\ESP8266WiFi\src/WiFiClient.cpp line 329
0x402041a4: WiFiClient::connect(char const*, unsigned short) at C:\Users\Sherin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0\libraries\ESP8266WiFi\src/WiFiClient.cpp line 329
0x40204fdc: Print::println(int, int) at C:\Users\Sherin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0\cores\esp8266/Print.h line 93
0x4020252c: BlynkArduinoClientGen ::connect() at J:\My Documents\Arduino\libraries\Blynk\src/Adapters/BlynkArduinoClient.h line 58
0x40204a10: BlynkDelay(unsigned int) at J:\My Documents\Arduino\libraries\Blynk\src\utility/BlynkDebug.cpp line 249
0x40202dfa: BlynkProtocol   >::run(bool) at J:\My Documents\Arduino\libraries\Blynk\src/Blynk/BlynkProtocol.h line 215
0x40204a24: BlynkMillis() at J:\My Documents\Arduino\libraries\Blynk\src\utility/BlynkDebug.cpp line 256
0x40203c1c: loop at J:\My Documents\Arduino\Blynk_My Final Project\push_notification_Email___led_Formatted2with_Hooter_LDRT1.ino.i/push_notification_Email___led_Formatted2with_Hooter_LDRT1.ino.i.ino line 233
0x402053b0: loop_wrapper at C:\Users\Sherin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0\cores\esp8266/core_esp8266_main.cpp line 57

I would really appreciate your help.

@Eldho I have seen exception errors similar to what you mention recently. At the moment I am guessing it relates to some of the bugs in the ESP8266 core.

Please start a new thread and be sure to use backticks to format your sketch for forum use.

1 Like

Thanks for the quick reply and backtick trick ( I had spent half an hour before posting that query…but couldn’t figure it out myself :slight_smile: )

@Eldho I will try to run your sketch or something similar through my MCU but in the meantime we need precise details of your exact setup.

Local or Blynk’s cloud server.
Blynk library version.
Blynk App version.
Android and iOS together with make and model of phone.

ESP8266 Core version you are using (2.3.0, 2.4.0-rc2, 2.4.0 or Git Master).
lwIP version e.g. prebuilt v1.4 or v2 Mss =536 etc

I notice you have a lot of pinInterrupts and they are very sensitive on ESP’s so I will probably test your sketch without them to start with.

But when I use a simple code as shown below, the nodeMCU is able to reconnect to wifi multiple times (eventhough it shows some stack errors).
Does it mean that the issue is with my large code?

The simple code that I tried is

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

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

BlynkTimer timer;

void NotifyF()
{
    Serial.println("Front Door is opened");
    Blynk.notify("Front Door is opened!");
  }

void FDoorStatus()
{
  if (digitalRead(D2)==HIGH) {
    Blynk.virtualWrite(V1, 0);
  }
  else {
    Blynk.virtualWrite(V1, 255);
  }
}

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);

  pinMode(D1, INPUT);
  attachInterrupt(digitalPinToInterrupt(D1), NotifyF, FALLING);

    
  timer.setInterval(550L, FDoorStatus);
  
}



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

COM dispay


[77022] Connecting to blynk-cloud.com:80

Exception (28):
epc1=0x40203cf9 epc2=0x00000000 epc3=0x00000000 excvaddr=0x000000bb depc=0x00000000

ctx: cont 
sp: 3fff0560 end: 3fff07f0 offset: 01a0

>>>stack>>>
3fff0700:  3ffef494 00000000 3fff168c 40203cf2  
3fff0710:  2bcea6bc 0000000e 3ffe8a6a 3ffe8a5c  
3fff0720:  3ffe88a8 00000002 3ffef700 3ffe8a5c  
3fff0730:  3ffe88a8 00000050 3ffef494 402038b8  
3fff0740:  3ffe91b0 2bcea6bc 3ffe91b0 2bcea6bc  
3fff0750:  3ffe8a5c 3ffef700 3ffef700 402046cc  
3fff0760:  3fffdad0 3ffef700 3ffef480 4020260c  
3fff0770:  3fff0370 3ffef480 3ffef45c 40204128  
3fff0780:  00000000 00000000 3ffef45c 00000000  
3fff0790:  00012cde 00000000 3ffef45c 40202ede  
3fff07a0:  00000000 3ffef218 3ffef7b8 4020413c  
3fff07b0:  00000000 0000c9ab 3ffef45c 3ffef7c0  
3fff07c0:  3fffdad0 00000000 3ffef7b8 4020334b  
3fff07d0:  3fffdad0 00000000 3ffef7b8 40204aa0  
3fff07e0:  feefeffe feefeffe 3ffef7d0 40100710  
<<<stack<<<
Z+⸮⸮<⸮⸮[47] Connecting to Appu
[274589] Connected to WiFi
[274589] IP: 192.168.43.84
[274589] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.5.1 on NodeMCU

[274666] Connecting to blynk-cloud.com:80
[279667] Connecting to blynk-cloud.com:80
[279887] Ready (ping: 113ms).

Exceptio decoder shows

Decoding 10 results
0x40203cf2: WiFiClient::connect(IPAddress, unsigned short) at C:\Users\Sherin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0\libraries\ESP8266WiFi\src/WiFiClient.cpp line 329
0x402038b8: WiFiClient::connect(char const*, unsigned short) at C:\Users\Sherin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0\libraries\ESP8266WiFi\src/WiFiClient.cpp line 329
0x402046cc: Print::println(int, int) at C:\Users\Sherin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0\cores\esp8266/Print.h line 93
0x4020260c: BlynkArduinoClientGen ::connect() at J:\My Documents\Arduino\libraries\Blynk\src/Adapters/BlynkArduinoClient.h line 58
0x40204128: BlynkDelay(unsigned int) at J:\My Documents\Arduino\libraries\Blynk\src\utility/BlynkDebug.cpp line 249
0x40202ede: BlynkProtocol   >::run(bool) at J:\My Documents\Arduino\libraries\Blynk\src/Blynk/BlynkProtocol.h line 215
0x4020413c: BlynkMillis() at J:\My Documents\Arduino\libraries\Blynk\src\utility/BlynkDebug.cpp line 256
0x4020334b: loop at J:\My Documents\Arduino\Blynk_My Final Project\push_notification___led_Formatted.ino/push_notification___led_Formatted.ino.ino line 45
0x40204aa0: loop_wrapper at C:\Users\Sherin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0\cores\esp8266/core_esp8266_main.cpp line 57
0x40100710: cont_norm at C:\Users\Sherin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0\cores\esp8266/cont.S line 109

Probably not.
Comment out the last remaining attachInterrupt().

See you are using library v0.5.1 but please provide the rest of the information we require.

I can also see you are using core 2.4.0 but is this the latest release (buggy) or Git pull of the master (less buggy)?

I am sorry. I dont know exactly how to find it out.
I updated all the libraries through Arduino IDE.

This is the address of Board Manager I am using
http://arduino.esp8266.com/stable/package_esp8266com_index.json

You are using the latest official release of the Core without the bug fixes.

Other details?

Blynk app version 2.18.2
using android mobile. Xiaomi Redmi Note 4.
Using Blynk Server

You are using the more buggy lwIP, try v1.4.

ok. I’ll try

The developers behind the ESP8266 Core just have one issue to fix relating to WiFi.disconnect and they will then issue a new release with the recent bug fixes.

It works fine now. Reconnected to wifi multiple times without any issues.
I thank you from the bottom of my heart.
Experts like you are finding time to reply to such basic/stupid questions posted by people like me who are new to programming is simply heart whelming. It means a lot to us. In my case I was almost about to stop my 3 weeks effort and throw it away :slight_smile:

1 Like

They are not stupid questions and the fixes are there to help all of us.

As per my last post, top man Ivan is at a Trade Fair at the moment but he is hoping to look at the WiFi.disconnect issue this weekend. They will then have to run various tests before rolling out the next release.

2 Likes

What exactly did you do to fix the issue? Did you downgrade ESP8266 Arduino to 2.3.0?

How do you enable lwIP variant?

@alberms You already opened a topic with your issue… please ask your questions there.