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?

asked Sep 11, 2021 at 11:25
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.

answered Sep 12, 2021 at 8:18
4
  • there are two parameters you need to pass. Whereas in my example is only one 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. 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 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 Commented Sep 13, 2021 at 13:11

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.