ESP32 SNTP broken with GMT ?

zweieuro
Posts: 3
Joined: Sat Sep 25, 2021 7:23 pm

ESP32 SNTP broken with GMT ?

Postby zweieuro » Sat Sep 25, 2021 7:29 pm

Hi,
I tried setting my SNTP to "GMT+2" and now i get nothing in response (even when synced) then 0.


I use this for init:

Code: Select all

   void initialize_sntp(void)
    {
      if (inited_sntp)
        return;
      inited_sntp = true;
      ESP_LOGI(TAG, "Initializing SNTP to %s ", MgConfig::instance()->tz_.c_str());

      //setenv("TZ","GMT+2", 1);
      setenv("TZ", "EST5EDT", 1);
      tzset();

      sntp_setoperatingmode(SNTP_OPMODE_POLL);
      sntp_setservername(0, "pool.ntp.org");
      sntp_init();
    }

    void init_sntp()
    {
      if (isSet())
      {
        ESP_LOGW(TAG, "init_sntp called although already set inited_sntp: %d", inited_sntp);
      }
      initialize_sntp();

      time_t now = 0;
      struct tm timeinfo = {0};

      int retry = 0;
      const int retry_count = 10;

      int status = sntp_get_sync_status();

      while (status == SNTP_SYNC_STATUS_RESET && ++retry < retry_count)
      {
        status = sntp_get_sync_status();
        ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d) status: %d", retry, retry_count, status);
        vTaskDelay(2000 / portTICK_PERIOD_MS);
      }
      ESP_LOGI(TAG, "sntp_get_sync_status() %d", status);
      time(&now);
      localtime_r(&now, &timeinfo);
      print_now();
      if (retry >= retry_count)
      {
        ESP_LOGW(TAG, "SNTP sync failed!");
      }
 
    }


and in a seperate task i call indefinetly :

Code: Select all

  void print_now()
    {
      time_t now;
      char strftime_buf[64];
      struct tm timeinfo;
      localtime_r(&now, &timeinfo);
      strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
      ESP_LOGI(TAG, "The current date/time in %s is %s", MgConfig::instance()->tz_.c_str(), strftime_buf);

      timeval t = {};
      sntp_sync_time(&t);
      ESP_LOGI(TAG, " %ld", t.tv_sec);
    }

but all i get is 0
The time seems to be synchronized since the loop from init breaks on mostly the second try, but i just get very wrong values:

I (602729) time-mod: The current date/time in GMT+2 is Tue Jan 6 15:24:44 2004
I (602729) time-mod: 0
I (602769) time-mod: The current date/time in GMT+2 is Wed Dec 31 19:00:00 1969

can anyone help me with this ?

ESP_YJM
Posts: 300
Joined: Fri Feb 26, 2021 10:30 am

Re: ESP32 SNTP broken with GMT ?

Postby ESP_YJM » Sun Sep 26, 2021 7:28 am

It seems SNTP not sync successfully.

zweieuro
Posts: 3
Joined: Sat Sep 25, 2021 7:23 pm

Re: ESP32 SNTP broken with GMT ?

Postby zweieuro » Mon Sep 27, 2021 7:07 am

ESP_YJM wrote:
Sun Sep 26, 2021 7:28 am
It seems SNTP not sync successfully.
Yes it seems so, but 'sntp_get_sync_status()' returns that the sync is complete, why would the status be complete if it failed? how can i detect if it failed ? and how can i detect why it failed ?

ESP_YJM
Posts: 300
Joined: Fri Feb 26, 2021 10:30 am

Re: ESP32 SNTP broken with GMT ?

Postby ESP_YJM » Mon Sep 27, 2021 11:58 am

I think you can enable LWIP_DEBUG and SNTP_DEBUG, add some debug log to trace.

Who is online

Users browsing this forum: Baidu [Spider] and 54 guests