Getting wrong Time after using esp_efuse_mac_get_default(mac) API

Ritu21
Posts: 123
Joined: Sat Aug 04, 2018 9:58 am

Getting wrong Time after using esp_efuse_mac_get_default(mac) API

Postby Ritu21 » Fri Oct 26, 2018 7:07 am

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.

ESP_igrr
Posts: 2071
Joined: Tue Dec 01, 2015 8:37 am

Re: Getting wrong Time after using esp_efuse_mac_get_default(mac) API

Postby ESP_igrr » Fri Oct 26, 2018 12:17 pm

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?

Ritu21
Posts: 123
Joined: Sat Aug 04, 2018 9:58 am

Re: Getting wrong Time after using esp_efuse_mac_get_default(mac) API

Postby Ritu21 » Fri Oct 26, 2018 12:58 pm

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.

ESP_igrr
Posts: 2071
Joined: Tue Dec 01, 2015 8:37 am

Re: Getting wrong Time after using esp_efuse_mac_get_default(mac) API

Postby ESP_igrr » Fri Oct 26, 2018 3:05 pm

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.

Ritu21
Posts: 123
Joined: Sat Aug 04, 2018 9:58 am

Re: Getting wrong Time after using esp_efuse_mac_get_default(mac) API

Postby Ritu21 » Sat Oct 27, 2018 5:14 am

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.

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Getting wrong Time after using esp_efuse_mac_get_default(mac) API

Postby WiFive » Sat Oct 27, 2018 6:00 am

Code: Select all

char esp_mac[6];
Fail

Ritu21
Posts: 123
Joined: Sat Aug 04, 2018 9:58 am

Re: Getting wrong Time after using esp_efuse_mac_get_default(mac) API

Postby Ritu21 » Sat Oct 27, 2018 6:59 am

What it should be?

uint8_t esp_mac[6]; ??

Please suggest.

Thanks
R.

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Getting wrong Time after using esp_efuse_mac_get_default(mac) API

Postby WiFive » Sat Oct 27, 2018 7:04 am

Code: Select all

char esp_mac[13];

Ritu21
Posts: 123
Joined: Sat Aug 04, 2018 9:58 am

Re: Getting wrong Time after using esp_efuse_mac_get_default(mac) API

Postby Ritu21 » Sat Oct 27, 2018 8:14 am

why so?

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Getting wrong Time after using esp_efuse_mac_get_default(mac) API

Postby WiFive » Sat Oct 27, 2018 8:48 am

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.

Who is online

Users browsing this forum: Bing [Bot], jesper and 89 guests