1031 views|2 replies

155

Posts

1

Resources
The OP
 

[Construction Monitoring and Security System] 10. Kaluga Network Timing and Timer [Copy link]

 

Relying on the SNTP example (..\esp-idf-v4.4\examples\protocols\sntp) and timer example (..\esp-idf-v4.4\examples\system\esp_timer) provided by ESP32 IDF, I continued to develop the functions of the project, so that the Kaluga board can obtain the network time after starting and connecting to the Internet, and then start the S-level timer - in preparation for subsequent periodic monitoring. The time.c source file is still created separately, and the time-related code is written here.

#include "main.h"


/* Private macro -------------------------------------------------- */
#define TAG                                "AITA TIME"
#define CONFIG_SNTP_TIME_SYNC_METHOD_IMMED 1
#define AITA_SNTP_MAX_RETRY_TIMES          10


// SNTP related --------------------------------------------------- //
/* Private functions ---------------------------------------------- */
/*********************************************************************
 * FunctionName : sntp_notification_cb()
 * Description  : time synchronization callback
 * Parameters   : struct timeval *tv ——> epoch time struct pointer
 * Returns      : void
*********************************************************************/
void sntp_notification_cb(struct timeval *tv) {
    ESP_LOGI(TAG, "Epoch sec: %d, usec: %d", 
                (int)tv->tv_sec, (int)tv->tv_usec);
}
/*********************************************************************
 * FunctionName : initialize_sntp()
 * Description  : initialize sntp & synchronize system time
 * Parameters   : void
 * Returns      : void
*********************************************************************/
void initialize_sntp(void) {
    ESP_LOGI(TAG, "Initializing SNTP");
    sntp_setoperatingmode(SNTP_OPMODE_POLL);
// 0 - server number, "pool.ntp.org" - server url
    sntp_setservername(0, "pool.ntp.org");
    sntp_set_time_sync_notification_cb(sntp_notification_cb);
    sntp_init();
}
/*********************************************************************
 * FunctionName : obtain_time()
 * Description  : obtain system time
 * Parameters   : void
 * Returns      : void
*********************************************************************/
void obtain_time(void) {
    initialize_sntp();

// wait for time to be set
    int retry = 0;
    while(sntp_get_sync_status()==SNTP_SYNC_STATUS_RESET && 
                ++retry<AITA_SNTP_MAX_RETRY_TIMES) {
        ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", 
                    retry, AITA_SNTP_MAX_RETRY_TIMES);
        vTaskDelay(2000 / portTICK_PERIOD_MS);
    }
}
/* Exported functions --------------------------------------------- */
/*********************************************************************
 * FunctionName : aita_PrintCST8()
 * Description  : print cst-8 timezone time
 * Parameters   : time_t *now ——> sytem local time
 * Returns      : void
*********************************************************************/
void aita_PrintCST8(time_t *now) {
    char buf[64];
    struct tm timeinfo;
// set timezone to China standard time
    setenv("TZ", "CST-8", 1);
    tzset();
    localtime_r(now, &timeinfo);
    strftime(buf, sizeof(buf), "%c", &timeinfo);
    ESP_LOGI(TAG, "current date/time in Shanghai: %s", buf);
}
/*********************************************************************
 * FunctionName : aita_InitSNTP()
 * Description  : sntp & print cst-8 timezone time
 * Parameters   : void
 * Returns      : void
*********************************************************************/
void aita_InitSNTP(void) {
    time_t now;
    struct tm timeinfo;
    time(&now);
    localtime_r(&now, &timeinfo);

// If time is not set, tm_year will be (1970 - 1900).
    if(timeinfo.tm_year < (2016-1900)) {
        ESP_LOGI(TAG, "time is not set yet.");
        ESP_LOGI(TAG, "getting time over NTP.");
        obtain_time();
    // update 'now' variable with current time
        time(&now);
    }
// print China timezone time
    aita_PrintCST8(&now);
}


// timer related -------------------------------------------------- //
/* Private functions ---------------------------------------------- */
/*********************************************************************
 * FunctionName : periodic_timer_callback()
 * Description  : periodic timer callback function
 * Parameters   : void* arg ——> callback's argument
 * Returns      : void
*********************************************************************/
static void periodic_timer_callback(void* arg) {
    int64_t time_since_boot = esp_timer_get_time();
    ESP_LOGI(TAG, "periodic timer called, time since boot: %lld us", 
                    time_since_boot);
    aita_InitSNTP();
}
/* Exported functions --------------------------------------------- */
/*********************************************************************
 * FunctionName : aita_InitTimer()
 * Description  : initialize timer
 * Parameters   : void
 * Returns      : void
*********************************************************************/
void aita_InitTimer(void) {
    const esp_timer_create_args_t periodic_timer_args = {
            .callback = &periodic_timer_callback,
            .name = "periodic"
    };

    esp_timer_handle_t periodic_timer;
    ESP_ERROR_CHECK(
        esp_timer_create(&periodic_timer_args, &periodic_timer)
    );
    
    /* Start the timers */
    ESP_ERROR_CHECK(
        esp_timer_start_periodic(periodic_timer, 1000000)
    );
    ESP_LOGI(TAG, "started timers, time since boot: %lld us", 
                    esp_timer_get_time());
}


/*********** (C) COPYRIGHT AITA TCU ********** END OF FILE **********/

In the project entry app_main(), aita_InitSNTP() is called to obtain network timing, and aita_InitTimer() is called to start a 1s periodic timer.

Figure 10-1 SNTP and timer function test output

This post is from DigiKey Technology Zone

Latest reply

Network timing, call aita_InitTimer() to start a 1s periodic timer. There should be no problem here.   Details Published on 2022-9-22 07:20
 
 

6580

Posts

0

Resources
2
 

Network timing, call aita_InitTimer() to start a 1s periodic timer. There should be no problem here.

This post is from DigiKey Technology Zone

Comments

The test is fine, because the later functions of my project require regular collection, and also consider recording local logs, sending heartbeat packets and other functions, which require accurate time, so I consider doing time synchronization first, and then starting the timer. The actual time synchronization uses RTC, and the Timer uses a general timer, which complements each other.  Details Published on 2022-9-22 09:09
 
 
 

155

Posts

1

Resources
3
 
Jacktang posted on 2022-9-22 07:20 Network timing, call aita_InitTimer() to start a 1s periodic timer. There should be no problem here

There is no problem with the test, because the later functions of my project require regular collection, and I also consider recording local logs, sending heartbeat packets and other functions, which require accurate time, so I consider doing time synchronization first and then starting the timer.

The actual timing uses RTC, and the Timer uses a general timer, which has a complementary effect.

This post is from DigiKey Technology Zone
 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list