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?
1 Answer 1
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
-
there are two parameters you need to pass. Whereas in my example is only oneMurad– Murad2021年09月13日 06:57:41 +00:00Commented 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.2021年09月13日 07:11:46 +00:00Commented 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 similarMurad– Murad2021年09月13日 13:05:35 +00:00Commented 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 set2021年09月13日 13:11:53 +00:00Commented Sep 13, 2021 at 13:11
lang-cpp