Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve sync status check method in rtc example #20

Merged
merged 1 commit into from
Mar 4, 2022

Conversation

mayopan
Copy link
Contributor

@mayopan mayopan commented Mar 4, 2022

In my circumstance (M5STACK CORE2 connecting to home wifi),
even after coming out from while ((t = time(nullptr)) < 65536) loop, time update is not completed yet.
Adding some delay after while loop is one solution, but using sntp_get_sync_status() is better.

API Reference by espressif

@lovyan03 lovyan03 merged commit 0a3fbdb into m5stack:master Mar 4, 2022
@lovyan03
Copy link
Collaborator

lovyan03 commented Mar 4, 2022

@mayopan
Thank you for your cooperation !

@patfelst
Copy link

patfelst commented Mar 5, 2022

this is interesting. I hadn't noticed this example before. I hope you don't mind but can I ask a few questions?

I was setting the Core2 RTC using some code I found on the internet (below) but never really understood how it woks. Does the configTime() function start the NTP sync? And this is similar to configTzTime() in your example?

And does this set the ESP32's internal RTC, then once it's set, we need to set the Core2's RTC with this time?

Do you know why are there different functions to achieve the same things, and is there a benefit to using one method over another?

  const long gmtOffset_sec = (long)(10.5 * 60.0 * 60.0);
  struct tm timeinfo;
  daylightOffset_sec = 3600;

  // Set timezone and get the time from NTP server. Sets ESP32 internal RTC?
  configTime(gmtOffset_sec, daylightOffset_sec, "pool.ntp.org");

  // Without a delay of >= 500, NTP fails randomly. No idea why
  delay(1000);

  // Read time back from ESP32 RTC
  if (!getLocalTime(&timeinfo)) {
    if (debug_mode) Serial.println("Failed to obtain time");
    M5.Lcd.setTextColor(TFT_WHITE, TFT_RED);
    M5.Lcd.drawString("NTP Failed", ntp_msg_x, ntp_msg_y);
    return;
  }
  strftime(time_txt, 80, "%A %e-%m-%Y, %H:%M:%S", &timeinfo);
  log_d("getLocalTime()=%s", time_txt);

  // Convert tm time structure into RTC structure
  RTCtime.hours = (uint8_t)timeinfo.tm_hour;
  RTCtime.minutes = (uint8_t)timeinfo.tm_min;
  RTCtime.seconds = (uint8_t)timeinfo.tm_sec;
  M5.Rtc.setTime(&RTCtime);  // and writes the set time to the real time clock

  // Set the date
  RTCdate.date = (uint8_t)timeinfo.tm_mday;
  RTCdate.month = (uint8_t)(timeinfo.tm_mon + 1);
  RTCdate.weekDay = (uint8_t)timeinfo.tm_wday;
  RTCdate.year = (uint16_t)(timeinfo.tm_year + 1900);
  M5.Rtc.setDate(&RTCdate);

@lovyan03
Copy link
Collaborator

lovyan03 commented Mar 5, 2022

@patfelst
configTime and configTzTime are functions included in ArduinoESP32.

https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/esp32-hal-time.c#L48-L81

Although there are differences in time zone handling, both use sntp for time synchronization.

Since these functions are intended only for the ESP32's internal clock, setting to the RTC connected via I2C must be done separately.

Since the M5Unified library is different from the traditional library, it may not implement the same functions.

@mayopan mayopan deleted the rtc-patch branch March 5, 2022 14:59
@patfelst
Copy link

patfelst commented Mar 5, 2022

thnkas @lovyan03, that Espressif link is very useful, and your explanation.

@patfelst
Copy link

Hello there @mayopan. I tried to use the example in your pull request, however the function in this line is not recognised

while (sntp_get_sync_status() != SNTP_SYNC_STATUS_COMPLETED)

I believe it is an IDF function, so not sure why it is in the example, or am I missing something?

Thanks for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants