Page 1 of 2
Getting wrong Time after using esp_efuse_mac_get_default(mac) API
Posted: Fri Oct 26, 2018 7:07 am
by Ritu21
Hi,
I needed the current date and time and used the example of sntp given in esp-idf. I was getting correct date and time and then I needed the MAC address of esp32 and used esp_efuse_mac_get_default(mac) API and started getting wrong time, around half an hour discrepancy in time. So far, date remains correct.
Please suggest why is this happening?
Waiting for your early response.
Thanks
R.
Re: Getting wrong Time after using esp_efuse_mac_get_default(mac) API
Posted: Fri Oct 26, 2018 12:17 pm
by ESP_igrr
The esp_efuse_mac_get_default function only reads a couple of registers and stores the results into a provided buffer. Are you sure you aren't for example accidentally overwriting part of your time variable when reading the mac address? Could you perhaps post the code which demonstrates the issue?
Re: Getting wrong Time after using esp_efuse_mac_get_default(mac) API
Posted: Fri Oct 26, 2018 12:58 pm
by Ritu21
Hi,
I am not overwriting the time, but you can check the below code:
time(&now);
localtime_r(&now, &timeinfo);
if (timeinfo.tm_year < (2016 - 1900))
{
ESP_LOGI(TAG, "Time is not set yet. Connecting to WiFi and getting time over NTP.");
obtain_time();
time(&now);
}
setenv("TZ", "IST-5:30", 1);
tzset();
localtime_r(&now, &timeinfo);
strftime(strftime_buf, sizeof(strftime_buf), "%d/%m/%Y %H:%M:%S", &timeinfo);
ESP_LOGI(HTAG, "The current date/time in India is: %s",strftime_buf);
ESP_ERROR_CHECK(esp_efuse_mac_get_default(mac_id));
for(int m = 0; m<6; m++)
sprintf(esp_mac, "%02x%02x%02x%02x%02x%02x", mac_id[0],mac_id[1],mac_id[2],mac_id[3], mac_id[4],mac_id[5]);
printf("ESP_MAC - %s\n", esp_mac);
Only on first execution, it gives accurate time and later there is half an hour discrepancy in time.
Thanks
R.
Re: Getting wrong Time after using esp_efuse_mac_get_default(mac) API
Posted: Fri Oct 26, 2018 3:05 pm
by ESP_igrr
Can you add the missing declarations of mac_id, esp_mac, now variables. You are saying "on first execution", does it mean that there is some additional code which calls this in a loop? If you add the complete source file as an attachment, it will be a lot easier for someone reading this forum to offer advice.
Re: Getting wrong Time after using esp_efuse_mac_get_default(mac) API
Posted: Sat Oct 27, 2018 5:14 am
by Ritu21
Hi,
These are the declarations:
time_t now;
struct tm timeinfo;
uint8_t mac_id[6];
char esp_mac[6];
This is running as a Free RTOS task, so it is continuously executing.
Thanks
R.
Re: Getting wrong Time after using esp_efuse_mac_get_default(mac) API
Posted: Sat Oct 27, 2018 6:00 am
by WiFive
Re: Getting wrong Time after using esp_efuse_mac_get_default(mac) API
Posted: Sat Oct 27, 2018 6:59 am
by Ritu21
What it should be?
uint8_t esp_mac[6]; ??
Please suggest.
Thanks
R.
Re: Getting wrong Time after using esp_efuse_mac_get_default(mac) API
Posted: Sat Oct 27, 2018 7:04 am
by WiFive
Re: Getting wrong Time after using esp_efuse_mac_get_default(mac) API
Posted: Sat Oct 27, 2018 8:14 am
by Ritu21
why so?
Re: Getting wrong Time after using esp_efuse_mac_get_default(mac) API
Posted: Sat Oct 27, 2018 8:48 am
by WiFive
sprintf(esp_mac, "%02x%02x%02x%02x%02x%02x", mac_id[0],mac_id[1],mac_id[2],mac_id[3], mac_id[4],mac_id[5]);
You just asked it to put a 12 character string (02 * 6) plus NULL there.