ESP32 timestamping & midnite reset

I have several ESP32 microcontrollers on my homestead spread about among various outbuildings. They are all tied to my LAN and allow me to interrogate them through a browser.
I recently added another one of these controllers to the field and have an issue I am trying to resolve. Whenever I do a client request with my other ESPs the response populates the webpage with the latest data, including the current time. But on this latest ESP addition the time is not being updated nor is my code to reset certain data values at just before midnite executing.
I copied the same code to this new controller from one of the other ESPs that works perfectly.
Using Wifi.h & esp_sntp.h libraries and the functions initTime, and GetLocalTime.
The serial monitor output shows the successful retrieval of the NTP timestamp when the controller first initializes.
I am currently calling GetLocalTime once every 10 minutes; on the other controllers I am doing the call once an hour.
If I wait and do a client request just a minute before the next GetLocalTime update it shows the time from the last update 9 minutes ago. And then the following minute it shows the updated time.
I am trying to decide if I should just start over with a basic sketch and get the webpage synced with the time updates and themidnite reset functioning & verified and then add in the remainder of the code.
With all those other controllers working properly it perplexes me that this one ESP is acting differently despite having the same code.

Which ESP32. Some have tiny aerials, and could have issues with the distance.
Try mounting the board in a different angle.
Or test with a program that continually displays the WiFi strength of the router.

Seeing "initTime" and "GetLocalTime" makes me think you have taken the long way with NTP, which is now built into the latest ESP core. This is all you need. NTP updates happen every hour, unless you change it. The ESP32 could loose/gain about 2 seconds per day.

#include <WiFi.h>  // ESP32
time_t now;  // epoch
tm tm;       // time struct

void setup() {
  WiFi.begin("My_SSID", "My_PASS"); // credentials
  configTzTime("CST6CDT,M3.2.0,M11.1.0", "pool.ntp.org");  // https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
}

void loop() {
  time(&now); // UTC
  localtime_r(&now, &tm); // local time struct
}

With added wait for NTP sync after booting, and printing the time every second.

#include <WiFi.h>  // ESP32
#include "esp_sntp.h" // for callback
unsigned long prevTime;
bool reSync;
time_t now;  // epoch
tm tm;       // time struct

void cbSyncTime(struct timeval *tv) {  // sync callback
  reSync = true;
}

void setup() {
  Serial.begin(115200);
  WiFi.begin("My_SSID", "My_PASS");
  sntp_set_time_sync_notification_cb(cbSyncTime);          // enable callback
  configTzTime("CST6CDT,M3.2.0,M11.1.0", "pool.ntp.org");  // https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
  while (!reSync) yield();                                 // wait here for a valid time string
}

void loop() {
  time(&now);
  localtime_r(&now, &tm);

  // print
  if (now != prevTime) { // if time has changed
    prevTime = now;
    printf("\n%02u:%02u:%02u", tm.tm_hour, tm.tm_min, tm.tm_sec);
  }
}

The WiFi signal strength is not an issue as I am reading that out on the webpage and it typically is around 65 -70 db.

ESP-32S NodeMCU with a DEV Module board type.

I will definitely try the code you offered and see how that goes. :+1:

thanks for the feedback...

clarification - I should note that the underside of the microcontroller says ESP32S NodeMCU but the processor on top says ESP-WROOM-32. The IDE 2 will not load the ESP if the board type is selected AS ESP32S NodeMCU; set up as ESP32 Wrover Module it loads just fine. I think it will also load as DEV Module.

The localtime struct is filled with more variables, year month day weekday DST etc.
This page shows how to use them.
Leo..

1 Like

Thanks for that link Leo! You are a mindreader.....lol :+1:

Haha, no. But I do have a crystal ball, which isn't always right.
It says you live in the Chicago area. That's why I used this line in setup().

configTzTime("CST6CDT,M3.2.0,M11.1.0", "pool.ntp.org"); // Chicago

This line also does automatic daylight savings.
Leo..

1 Like

Sorry, didn't give a hint yet for the midnight detection you asked for.
The variable tm.tm_wday (weekday number) could be used to detect midnight.
Leo..

if (tm.tm_wday != prevDay) { // midnight?
  prevDay = tm.tm_wday; // remember
  // midnight code
}
1 Like

I like that current day / previous day comparison for midnite rollover! :+1:

With input from Leo (Wawa) on several issues and changing up/simplifying

configTime & getLocalTime function calls the code is executing much better.

thanks Leo!! :white_check_mark: :white_check_mark: