[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
|