Getting wrong Time after using esp_efuse_mac_get_default(mac) API
Getting wrong Time after using esp_efuse_mac_get_default(mac) API
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.
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
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
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.
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
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
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.
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
Code: Select all
char esp_mac[6];
Re: Getting wrong Time after using esp_efuse_mac_get_default(mac) API
What it should be?
uint8_t esp_mac[6]; ??
Please suggest.
Thanks
R.
uint8_t esp_mac[6]; ??
Please suggest.
Thanks
R.
Re: Getting wrong Time after using esp_efuse_mac_get_default(mac) API
Code: Select all
char esp_mac[13];
Re: Getting wrong Time after using esp_efuse_mac_get_default(mac) API
You just asked it to put a 12 character string (02 * 6) plus NULL there.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]);
Who is online
Users browsing this forum: Google [Bot], Reenis and 94 guests