timesync
This commit is contained in:
@ -2,4 +2,5 @@ idf_component_register(SRCS "app_main.c"
|
|||||||
"network_mngr.c"
|
"network_mngr.c"
|
||||||
"gpio.c"
|
"gpio.c"
|
||||||
"counter.c"
|
"counter.c"
|
||||||
|
"timesync.c"
|
||||||
INCLUDE_DIRS ".")
|
INCLUDE_DIRS ".")
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
|
|
||||||
#include "gpio.h"
|
#include "gpio.h"
|
||||||
#include "counter.h"
|
#include "counter.h"
|
||||||
|
#include "timesync.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -36,9 +37,13 @@ void deviceInit() {
|
|||||||
void app_main(void)
|
void app_main(void)
|
||||||
{
|
{
|
||||||
deviceInit();
|
deviceInit();
|
||||||
|
|
||||||
|
// it is important to initialize the counter before the gpios
|
||||||
counterInit();
|
counterInit();
|
||||||
|
|
||||||
gpioInit();
|
gpioInit();
|
||||||
networkInit(isGpioForceProv());
|
networkInit(isGpioForceProv());
|
||||||
|
timesyncInit();
|
||||||
|
|
||||||
/* Start main application now */
|
/* Start main application now */
|
||||||
while (1) {
|
while (1) {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
#include "counter.h"
|
#include "counter.h"
|
||||||
|
#include "timesync.h"
|
||||||
|
|
||||||
#include <driver/timer.h>
|
#include <driver/timer.h>
|
||||||
#include <esp_log.h>
|
#include <esp_log.h>
|
||||||
@ -33,7 +34,8 @@ static void counterZeroCrossingAveragerTask(void *arg) {
|
|||||||
if (currentCounterValue == QUEUE_MARKER) {
|
if (currentCounterValue == QUEUE_MARKER) {
|
||||||
if (counterCnt > 0) {
|
if (counterCnt > 0) {
|
||||||
uint32_t counterSecondAverage = ((uint32_t)(counterSum)) / ((uint32_t)(counterCnt));
|
uint32_t counterSecondAverage = ((uint32_t)(counterSum)) / ((uint32_t)(counterCnt));
|
||||||
ESP_LOGI(TAG, "%u %u %u", (uint32_t)counterCnt, (uint32_t)counterSum, counterSecondAverage);
|
int ts = timesyncReady();
|
||||||
|
ESP_LOGI(TAG, "%d %u %u %u", ts, (uint32_t)counterCnt, (uint32_t)counterSum, counterSecondAverage);
|
||||||
} else {
|
} else {
|
||||||
ESP_LOGW(TAG, "counterCnt is zero");
|
ESP_LOGW(TAG, "counterCnt is zero");
|
||||||
}
|
}
|
||||||
|
29
src/main/timesync.c
Normal file
29
src/main/timesync.c
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
#include "timesync.h>"
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <esp_log.h>
|
||||||
|
#include <esp_sntp.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
|
||||||
|
static const char *TAG = "ts";
|
||||||
|
|
||||||
|
static bool synchronized = false;
|
||||||
|
|
||||||
|
void timesyncCallback(struct timeval *tv) {
|
||||||
|
ESP_LOGI(TAG, "time is synchronized now");
|
||||||
|
synchronized = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void timesyncInit() {
|
||||||
|
ESP_LOGI(TAG, "Initializiing SNTP");
|
||||||
|
sntp_setoperatingmode(SNTP_OPMODE_POLL);
|
||||||
|
sntp_setservername(0, SNTP_SERVER);
|
||||||
|
sntp_set_time_sync_notification_cb(timesyncCallback);
|
||||||
|
sntp_set_sync_mode(SNTP_SYNC_MODE_IMMED);
|
||||||
|
sntp_set_sync_interval(60*1000); // 1 minute
|
||||||
|
sntp_init();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool timesyncReady() {
|
||||||
|
return synchronized;
|
||||||
|
}
|
16
src/main/timesync.h
Normal file
16
src/main/timesync.h
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#ifndef _TIMESYNC_H_
|
||||||
|
#deifne _TIMESYNC_H_
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
|
||||||
|
#define SNTP_SERVER "pool.ntp.org"
|
||||||
|
|
||||||
|
|
||||||
|
void timesyncInit();
|
||||||
|
bool timesyncReady();
|
||||||
|
|
||||||
|
#endif // _TIMESYNC_H_
|
||||||
|
|
||||||
|
|
||||||
|
|
Reference in New Issue
Block a user