sntp & ds3231 increase accuracy

NIXIE_123
Posts: 32
Joined: Fri Aug 02, 2019 9:51 am

sntp & ds3231 increase accuracy

Postby NIXIE_123 » Wed Aug 11, 2021 5:30 pm

Hello

Code: Select all

if(bits & WIFI_CONNECTED_BIT){
    	sntp_setoperatingmode(SNTP_OPMODE_POLL);
        sntp_setservername(0, "pool.ntp.org");
        sntp_set_sync_mode(SNTP_SYNC_MODE_IMMED);
        sntp_init();

        time_t now = 0;
        struct tm timeinfo = { 0 };
        int retry = 0;
        const int retry_count = 10;
        while (sntp_get_sync_status() == SNTP_SYNC_STATUS_RESET && ++retry < retry_count) {
            ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", retry, retry_count);
            vTaskDelay(2000 / portTICK_PERIOD_MS);
        }
        time(&now);
        setenv("TZ", "CET-1CEST-2,M3.5.0/02:00:00,M10.5.0/03:00:00", 1);	//TZ environment variable
        tzset();
        localtime_r(&now, &timeinfo);

        wpisz_do_ds3231(0,(uint8_t)timeinfo.tm_sec);
        wpisz_do_ds3231(1,(uint8_t)timeinfo.tm_min);
        wpisz_do_ds3231(2,(uint8_t)timeinfo.tm_hour);
        wpisz_do_ds3231(4,(uint8_t)timeinfo.tm_mday);
        wpisz_do_ds3231(5,(uint8_t)timeinfo.tm_mon+1);
        wpisz_do_ds3231(6,(uint8_t)timeinfo.tm_year-100);
}
For now accuracy of sntp example is 1 second.
I want to have it at the level of miliseconds.
How to achieve this?

NIXIE_123
Posts: 32
Joined: Fri Aug 02, 2019 9:51 am

Re: sntp & ds3231 increase accuracy

Postby NIXIE_123 » Thu Aug 12, 2021 12:42 pm

solution:

Code: Select all

struct tm timeinfo = { 0 };
        int retry = 0;
        const int retry_count = 10;
        while (sntp_get_sync_status() == SNTP_SYNC_STATUS_RESET && ++retry < retry_count) {
            ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", retry, retry_count);
            vTaskDelay(2000 / portTICK_PERIOD_MS);
        }
        sntp_stop();
        setenv("TZ", "CET-1CEST-2,M3.5.0/02:00:00,M10.5.0/03:00:00", 1);	//TZ environment variable
        tzset();

        time_t pamiec, now;
	time(&now);
	pamiec = now;
	while(now == pamiec){
     		time(&now);
     		vTaskDelay(1/portTICK_PERIOD_MS);
	}
	localtime_r(&now, &timeinfo);

        wpisz_do_ds3231(0,(uint8_t)timeinfo.tm_sec);
        wpisz_do_ds3231(1,(uint8_t)timeinfo.tm_min);
        wpisz_do_ds3231(2,(uint8_t)timeinfo.tm_hour);
        wpisz_do_ds3231(4,(uint8_t)timeinfo.tm_mday);
        wpisz_do_ds3231(5,(uint8_t)timeinfo.tm_mon+1);
        wpisz_do_ds3231(6,(uint8_t)timeinfo.tm_year-100);

Who is online

Users browsing this forum: No registered users and 89 guests