Browse Source

add time sync via sntp

main
Nicolas Massé 4 years ago
parent
commit
ee0a73e255
  1. 2
      main/CMakeLists.txt
  2. 3
      main/main.c
  3. 68
      main/sntp.c
  4. 7
      main/sntp.h

2
main/CMakeLists.txt

@ -1,2 +1,2 @@
idf_component_register(SRCS "main.c" "tic.c" "wifi.c" "mqtt.c" "libteleinfo.cpp" "libteleinfo/src/LibTeleinfo.cpp" idf_component_register(SRCS "main.c" "tic.c" "wifi.c" "mqtt.c" "sntp.c" "libteleinfo.cpp" "libteleinfo/src/LibTeleinfo.cpp"
INCLUDE_DIRS ".") INCLUDE_DIRS ".")

3
main/main.c

@ -12,6 +12,7 @@
#include "tic.h" #include "tic.h"
#include "wifi.h" #include "wifi.h"
#include "mqtt.h" #include "mqtt.h"
#include "sntp.h"
// Embedded via component.mk // Embedded via component.mk
extern const uint8_t cacert_pem_start[] asm("_binary_cacert_pem_start"); extern const uint8_t cacert_pem_start[] asm("_binary_cacert_pem_start");
@ -28,6 +29,8 @@ void app_main(void) {
wifi_init_sta(); wifi_init_sta();
wifi_wait_for_online(); wifi_wait_for_online();
mqtt_init(); mqtt_init();
sntp_start();
mqtt_wait_for_readiness(); mqtt_wait_for_readiness();
sntp_wait_for_readiness();
tic_uart_init(); tic_uart_init();
} }

68
main/sntp.c

@ -0,0 +1,68 @@
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_attr.h"
#include "esp_sntp.h"
#include "sntp.h"
/* FreeRTOS event group to signal when we are connected*/
static EventGroupHandle_t s_sntp_event_group;
/* The event group allows multiple bits for each event, but we only care about one event:
* - time is synchronized
*/
#define TIME_SYNC_BIT BIT0
static const char *SNTP_LOGGER = "sntp";
void sntp_callback(struct timeval *tv) {
if (sntp_get_sync_mode() == SNTP_SYNC_MODE_IMMED) {
time_t now;
time(&now);
struct tm now_tm;
localtime_r(&now, &now_tm);
char strftime_buf[64];
if (strftime(strftime_buf, sizeof(strftime_buf), "%c", &now_tm)) {
ESP_LOGI(SNTP_LOGGER, "Time synchronized: %s", strftime_buf);
}
sntp_set_sync_mode(SNTP_SYNC_MODE_SMOOTH);
xEventGroupSetBits(s_sntp_event_group, TIME_SYNC_BIT);
}
}
void sntp_start() {
s_sntp_event_group = xEventGroupCreate();
ESP_LOGI(SNTP_LOGGER, "Initializing SNTP...");
sntp_setoperatingmode(SNTP_OPMODE_POLL);
sntp_setservername(0, "pool.ntp.org");
sntp_set_time_sync_notification_cb(sntp_callback);
sntp_set_sync_mode(SNTP_SYNC_MODE_IMMED);
sntp_init();
}
void sntp_wait_for_readiness(void) {
/* Waiting until the connection is established (MQTT_CONNECTED_BIT).
* The bits are set by mqtt_event_handler_cb() (see above)
*/
EventBits_t bits = xEventGroupWaitBits(s_sntp_event_group,
TIME_SYNC_BIT,
pdFALSE,
pdFALSE,
portMAX_DELAY);
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
* happened. */
if (bits & TIME_SYNC_BIT) {
ESP_LOGI(SNTP_LOGGER, "Time is synchronized!");
} else {
ESP_LOGE(SNTP_LOGGER, "UNEXPECTED EVENT");
}
//vEventGroupDelete(s_sntp_event_group);
}

7
main/sntp.h

@ -0,0 +1,7 @@
#ifndef __SNTP_H__
#define __SNTP_H__
void sntp_start();
void sntp_wait_for_readiness(void);
#endif
Loading…
Cancel
Save