1

I am getting

error: 'getLocalTime' was not declared in this scope

when I am compiling this code for ESP8266, however works for ESP32:

#include "time.h"

void gettime()
{
  static int16_t delaycount = 0 ;                         // To reduce number of NTP requests
  static int16_t retrycount = 100 ;

  if ( timeinfo.tm_year )                                 // Legal time found?
  {
    sprintf ( timetxt, "%02d:%02d:%02d",                  // Yes, format to a string
              timeinfo.tm_hour,
              timeinfo.tm_min,
              timeinfo.tm_sec ) ;
  }
  if ( --delaycount <= 0 )                                // Sync every few hours
  {
    delaycount = 7200 ;                                   // Reset counter
    if ( timeinfo.tm_year )                               // Legal time found?
    {
      dbgprint ( "Sync TOD, old value is %s", timetxt ) ;
    }
    dbgprint ( "Sync TOD" ) ;
    if ( !getLocalTime ( &timeinfo ) )                    // Read from NTP server
    {
      dbgprint ( "Failed to obtain time!" ) ;             // Error
      timeinfo.tm_year = 0 ;                              // Set current time to illegal
      if ( retrycount )                                   // Give up syncing?
      {
        retrycount-- ;                                    // No try again
        delaycount = 5 ;                                  // Retry after 5 seconds
      }
    }
    else
    {
      sprintf ( timetxt, "%02d:%02d:%02d",                // Format new time to a string
                timeinfo.tm_hour,
                timeinfo.tm_min,
                timeinfo.tm_sec ) ;
      dbgprint ( "Sync TOD, new value is %s", timetxt ) ;
    }
  }
}

What shall I replace getLocalTime with?

0

1 Answer 1

3

It looks like the esp8266 Arduino core don't have a similar function.

The getLocalTime in esp32 arduino core is a simple function implemented in esp32-hal-time.c.

bool getLocalTime(struct tm * info, uint32_t ms)
{
    uint32_t start = millis();
    time_t now;
    while((millis()-start) <= ms) {
        time(&now);
        localtime_r(&now, info);
        if(info->tm_year > (2016 - 1900)){
            return true;
        }
        delay(10);
    }
    return false;
}

The function compiles on esp8266 arduino as it is so you can copy it to your sketch.

4
  • there are two parameters you need to pass. Whereas in my example is only one
    – Murad
    Commented Sep 13, 2021 at 6:57
  • @BJackson, the declaration in header file has a default value for the second parameter bool getLocalTime(struct tm * info, uint32_t ms = 5000); (you can't set default value on function definition). you can call the function with 5000 as second parameter or remove the parameter and use the fixed value in the function.
    – Juraj
    Commented Sep 13, 2021 at 7:11
  • It works, thanks. But is there a ESP8266 way? I was looking at github.com/scanlime/esp8266-Arduino/blob/master/cores/esp8266/… and there is nothing similar
    – Murad
    Commented Sep 13, 2021 at 13:05
  • @BJackson, it is a only convenience function only to wait with timeout until the very first response from NTP server is received and RTC is set
    – Juraj
    Commented Sep 13, 2021 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.