Not Connect After RESET nodemcu Lolin, TimeLib

Hello,

I did not find similar topics;

• Hardware: ESP8266 NodeMCU Lolin V3;
• Smartphone OS: Android 7 (Samsung, J5 Pro);
• Blynk server: Cloud;
• Blynk Library version: 2.24.0;

My problem is:
I can run and connect to the application. But if I reset the module (ESP) both the Serial Monitor and the app stop receiving the date and time data (no connection or event ever occurs). The system locks completely and only returns when the code is rewritten in ESP.

Would anyone know what the problem is? I really need this project to have no problems getting and keeping date and time, as it is a datalogger and I have had many problems with physical (hardware) RTCs.

Thank you.

My sketch code:

/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************

  Blynk can provide your device with time data, like an RTC.
  Please note that the accuracy of this method is up to several seconds.

  App project setup:
    RTC widget (no pin required)
    Value Display widget on V1
    Value Display widget on V2

  WARNING :
  For this example you'll need Time keeping library:
    https://github.com/PaulStoffregen/Time

  This code is based on an example from the Time library:
    https://github.com/PaulStoffregen/Time/blob/master/examples/TimeSerial/TimeSerial.ino
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial


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

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "***************";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "***************";
char pass[] = "***************";

BlynkTimer timer;

WidgetRTC rtc;

// Digital clock display of the time
void clockDisplay()
{
  // You can call hour(), minute(), ... at any time
  // Please see Time library examples for details

  String currentTime = String(hour()) + ":" + minute() + ":" + second();
  String currentDate = String(day()) + " " + month() + " " + year();
  Serial.print("Current time: ");
  Serial.print(currentTime);
  Serial.print(" ");
  Serial.print(currentDate);
  Serial.println();

  // Send time to the App
  Blynk.virtualWrite(V5, currentTime);
  // Send date to the App
  Blynk.virtualWrite(V6, currentDate);
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);

  // Begin synchronizing time
  rtc.begin();

  // Other Time library functions can be used, like:
  //   timeStatus(), setSyncInterval(interval)...
  // Read more: http://www.pjrc.com/teensy/td_libs_Time.html

  // Display digital clock every 10 seconds
  timer.setInterval(5000L, clockDisplay);
}

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

Are you sure?? Doing lots of quality searching in this forum before asking is always recommended as there is rarely an issue that hasn’t already been solved.

Are you sure a simple reset will not reconnect??

Blynk.begin() is a blocking command, so if there is no connection the processing stops. But of course as long as the rest of the network is back and working, then so will the device be upon reboot.

If you need automatic reconnection, then you use Blynk.config() instead and other stuff to monitor and attempt reconnection as needed.

Here is an example for ESP8266…

PS… The delay in the void loop() is a BIG no no!!

As is showing your WiFi password and cloud based AUTH code to the forum :stuck_out_tongue:

You might also want to add in a time sync interval

      // Other Time library functions can be used, like:
      //   timeStatus(), setSyncInterval(interval)...
      // Read more: http://www.pjrc.com/teensy/td_libs_Time.html

      setSyncInterval(10 * 60); // Sync interval in seconds (10 minutes)

Thank you for your patience.
I’m really newbie, and I do not know much about this programming universe and ESP8266.
My search in the forum used the keywords: nodemcu lolin (could be a specific problem of the module), timelib (I’m using this library) and reset (where the problem happens). The few texts that returned I did the careful reading (and delayed, I already explained). I do not speak English (I’m Brazilian) and I have difficulties finding good materials in my language. So I’m an adventurer here.

But most importantly: I do not want anyone to do my job, I just want to understand how this problem can happen if I just copied the text generated by the site itself here from the site.

Yes, this is the problem.

In another application that I used, there was no problem reconnecting after losing the connection (same module, same network), just that along with void loop () there were several other lines and commands executed before running blynk.begin () (so I used some delay attempts to see if it would solve.

In the previous project I used blynk.begin () and blynk.run () in the same place where they are in this code, in the same way.

And it did not lose connection either when the module had the reset button pressed or when disconnected and reconnected to the power (USB or power supply).

That’s what I need, but it’s not working …

Thanks again.
I’m going to test this and your reference, although I find it strange to have to do this (as I said, it worked in another application without messing with anything in these configurations).

Obrigado, eu vou pesquisar isso também…

Thanks, I’ll research this too …

Problem SOLVED

I did the polls as Gunner told me …

I’ve tested reconnect options. The problem persisted … BUT


I have my NodeMCU connected to the application of my prototype, and I decided to disconnect all the modules that could be blocking some port … I removed the signal from port D4 (nothing) … romovi the signal from port D1 (nothing) … I disconnected my current RTC module (nothing) … then I noticed that my source (two 3.3V and 5.0V voltage regulators) was connected (5.0V signal) to my GND and Vin module pins. I turned it off and the LED on the module came on brighter! (I thought I discovered the error) …

I then re-tested the code indicated by Gardner and it worked! By doing the RESET or removing from the USB and reconnecting soon after the ESP reconnects to the Blynk server (confirmed by the serial monitor and also by the App).

I then tested the previous code, and it worked too!

But now I’m curious … I’ll need to keep this NodeMCU powered off the computer in my final application. If it has this problem being fed with signals from the Vin and GND pins, then I need to figure out why and how to solve this. I’ll need to create another topic, probably.

For me not to be a bland person, I’ll leave the two codes tested and functional here.

Thanks Gunner, for the help and, somehow, do not leave me on a blind search.

This first code has the strings inside the void loop () because I needed to test if I could get these values to save to my SD memory card


/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial


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

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "***************";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "***************";
char pass[] = "***************";

BlynkTimer timer;

WidgetRTC rtc;

// Digital clock display of the time
void clockDisplay()
{
  // You can call hour(), minute(), ... at any time
  // Please see Time library examples for details

  String currentTime = String(hour()) + ":" + minute() + ":" + second();
  String currentDate = String(day()) + " " + month() + " " + year();
  Serial.print("Current time: ");
  Serial.print(currentTime);
  Serial.print(" ");
  Serial.print(currentDate);
  Serial.println();

  // Send time to the App
  Blynk.virtualWrite(V5, currentTime);
  // Send date to the App
  Blynk.virtualWrite(V6, currentDate);
}

void setup()
{
  // Debug console
  Serial.begin(9600);

 Serial.println("Blink begin...");
  Blynk.begin(auth, ssid, pass);
  Serial.println("Blink begin OK");
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);

  // Begin synchronizing time
  rtc.begin();

  // Other Time library functions can be used, like:
  //   timeStatus(), setSyncInterval(interval)...
  // Read more: http://www.pjrc.com/teensy/td_libs_Time.html

  // Display digital clock every 10 seconds
  timer.setInterval(5000L, clockDisplay);
}

void loop()
{
  Blynk.run();
  Serial.println("Blink run...");
  timer.run();
  Serial.println("timer run...");

  String currentTime = String(hour()) + ":" + minute() + ":" + second();
  String currentDate = String(day()) + "/" + month() + "/" + year() + ";";
  Serial.print("TESTE NO LOOP: ");
  Serial.print(currentTime);
  Serial.print(" ");
  Serial.print(currentDate);
  Serial.println();
  
  delay(5000);
}

The second code is what Gunner instructed me to study and analyze:


#define BLYNK_PRINT Serial // This prints to Serial Monitor
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "***************";
char ssid[] = "***************";
char pass[] = "***************";
//char server[] = "xxx.xxx.xxx.xxx";  // IP for Local Cloud Server
char server[] = "blynk-cloud.com";  // URL for Blynk Cloud Server
int port = 8080;

int DeviceLED = 2;
int ReCnctFlag;  // Reconnection Flag
int ReCnctCount = 0;  // Reconnection counter

BlynkTimer timer;

//////////////////////////////////////////////////////////////////
void setup() {
  Serial.begin(115200);
  pinMode(2, OUTPUT);

  WiFi.begin(ssid, pass);  // Non-blocking if no WiFi available
  Blynk.config(auth, server, port);
  Blynk.connect();

timer.setInterval(1000L, UpTime);
}

BLYNK_CONNECTED() {
  Serial.println("Connected");
  ReCnctCount = 0;
}

//////////////////////////////////////////////////////////////////
void UpTime() {
  Blynk.virtualWrite(V5, millis() / 1000);  // Send UpTime seconds to App
  Serial.print("UpTime: ");
  Serial.println(millis() / 1000);  // Send UpTime seconds to Serial
  digitalWrite(DeviceLED, !digitalRead(DeviceLED));  // Blink onboard LED
}

//////////////////////////////////////////////////////////////////
void loop() {
timer.run();

  if (Blynk.connected()) {  // If connected run as normal
    Blynk.run();
  } else if (ReCnctFlag == 0) {  // If NOT connected and not already trying to reconnect, set timer to try to reconnect in 30 seconds
    ReCnctFlag = 1;  // Set reconnection Flag
    Serial.println("Starting reconnection timer in 5 seconds...");
    timer.setTimeout(5000L, []() {  // 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
    });  // END Timer Function
  }
}