integrate esp32 sntp

This commit is contained in:
cyberman54 2022-01-23 21:12:28 +01:00
parent 0b0542f091
commit 529059edf4
5 changed files with 15 additions and 24 deletions

View File

@ -3,6 +3,7 @@
#include "globals.h"
#include "timekeeper.h"
#include "esp_sntp.h"
#define IF482_FRAME_SIZE (17)
#define IF482_SYNC_FIXUP (10) // calibration to fixup processing time [milliseconds]

View File

@ -10,6 +10,7 @@
#include "sensor.h"
#include "cyclic.h"
#include "timekeeper.h"
#include "esp_sntp.h"
#include "timesync.h"
#include "power.h"
#include "antenna.h"

View File

@ -8,6 +8,7 @@
#include "gpsread.h"
#include "if482.h"
#include "dcf77.h"
#include "esp_sntp.h"
#define SECS_YR_2000 (946684800UL) // the time at the start of y2k
#define GPS_UTC_DIFF 315964800UL // seconds diff between gps and utc epoch

View File

@ -88,24 +88,16 @@ String IF482_Frame(time_t t) {
char mon, out[IF482_FRAME_SIZE + 1];
switch (sntp_get_sync_status()) { // indicates if time has been set and recently synced
case SNTP_SYNC_STATUS_COMPLETED: // time is set and is synced
mon = 'A';
break;
case SNTP_SYNC_STATUS_IN_PROGRESS: // time had been set but sync attempt did not succeed
case SNTP_SYNC_STATUS_RESET:
mon = 'M';
break;
default: // unknown time status (should never be reached)
mon = '?';
break;
} // switch
if (sntp_get_sync_status() == SNTP_SYNC_STATUS_IN_PROGRESS)
mon = 'M'; // time had been set but sync not completed
else
mon = 'A'; // time has been set and was recently synced
// generate IF482 telegram
// snprintf(out, sizeof(out), "O%cL%s\r", mon, myTZ.dateTime(t, UTC_TIME,
// "ymdwHis").c_str());
//ESP_LOGD(TAG, "[%s] IF482 date/time: %s", ctime(time(NULL), out);
// ESP_LOGD(TAG, "[%s] IF482 date/time: %s", ctime(time(NULL), out);
return out;
}

View File

@ -103,7 +103,7 @@ void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec,
tv.tv_sec = time_to_set;
tv.tv_usec = 0;
settimeofday(&tv, NULL);
sntp_sync_time(&tv);
ESP_LOGI(TAG, "[%0.3f] UTC time: %d.000 sec", _seconds(), time_to_set);
@ -122,7 +122,6 @@ void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec,
#endif
timeSource = mytimesource; // set global variable
sntp_set_sync_status(SNTP_SYNC_STATUS_COMPLETED);
timesyncer.attach(TIME_SYNC_INTERVAL * 60, setTimeSyncIRQ);
ESP_LOGD(TAG, "[%0.3f] Timesync finished, time was set | timesource=%d",
@ -140,6 +139,10 @@ void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec,
// helper function to setup a pulse per second for time synchronisation
uint8_t timepulse_init() {
// set esp-idf API sntp sync mode
//sntp_init();
sntp_set_sync_mode(SNTP_SYNC_MODE_IMMED);
// use time pulse from GPS as time base with fixed 1Hz frequency
#ifdef GPS_INT
@ -273,15 +276,8 @@ void clock_loop(void *taskparameter) { // ClockTask
// wait for timepulse and store UTC time
xTaskNotifyWait(0x00, ULONG_MAX, &current_time, portMAX_DELAY);
/*
// no confident or no recent time -> suppress clock output
if ((sntp_get_sync_status() != SNTP_SYNC_STATUS_COMPLETED) ||
!(timeIsValid(current_time)) || (current_time == previous_time))
continue;
*/
// no confident or no recent time -> suppress clock output
if (!(timeIsValid(current_time)) || (current_time == previous_time))
if ((sntp_get_sync_status() == SNTP_SYNC_STATUS_IN_PROGRESS) ||
!(timeIsValid(current_time)) || (current_time == previous_time))
continue;
// set calendar time for next second of clock output