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 "globals.h"
#include "timekeeper.h" #include "timekeeper.h"
#include "esp_sntp.h"
#define IF482_FRAME_SIZE (17) #define IF482_FRAME_SIZE (17)
#define IF482_SYNC_FIXUP (10) // calibration to fixup processing time [milliseconds] #define IF482_SYNC_FIXUP (10) // calibration to fixup processing time [milliseconds]

View File

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

View File

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

View File

@ -88,18 +88,10 @@ String IF482_Frame(time_t t) {
char mon, out[IF482_FRAME_SIZE + 1]; char mon, out[IF482_FRAME_SIZE + 1];
switch (sntp_get_sync_status()) { // indicates if time has been set and recently synced if (sntp_get_sync_status() == SNTP_SYNC_STATUS_IN_PROGRESS)
case SNTP_SYNC_STATUS_COMPLETED: // time is set and is synced mon = 'M'; // time had been set but sync not completed
mon = 'A'; else
break; mon = 'A'; // time has been set and was recently synced
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
// generate IF482 telegram // generate IF482 telegram
// snprintf(out, sizeof(out), "O%cL%s\r", mon, myTZ.dateTime(t, UTC_TIME, // snprintf(out, sizeof(out), "O%cL%s\r", mon, myTZ.dateTime(t, UTC_TIME,

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_sec = time_to_set;
tv.tv_usec = 0; 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); 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 #endif
timeSource = mytimesource; // set global variable timeSource = mytimesource; // set global variable
sntp_set_sync_status(SNTP_SYNC_STATUS_COMPLETED);
timesyncer.attach(TIME_SYNC_INTERVAL * 60, setTimeSyncIRQ); timesyncer.attach(TIME_SYNC_INTERVAL * 60, setTimeSyncIRQ);
ESP_LOGD(TAG, "[%0.3f] Timesync finished, time was set | timesource=%d", 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 // helper function to setup a pulse per second for time synchronisation
uint8_t timepulse_init() { 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 // use time pulse from GPS as time base with fixed 1Hz frequency
#ifdef GPS_INT #ifdef GPS_INT
@ -273,16 +276,9 @@ void clock_loop(void *taskparameter) { // ClockTask
// wait for timepulse and store UTC time // wait for timepulse and store UTC time
xTaskNotifyWait(0x00, ULONG_MAX, &current_time, portMAX_DELAY); xTaskNotifyWait(0x00, ULONG_MAX, &current_time, portMAX_DELAY);
/* if ((sntp_get_sync_status() == SNTP_SYNC_STATUS_IN_PROGRESS) ||
// 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)) !(timeIsValid(current_time)) || (current_time == previous_time))
continue; continue;
*/
// no confident or no recent time -> suppress clock output
if (!(timeIsValid(current_time)) || (current_time == previous_time))
continue;
// set calendar time for next second of clock output // set calendar time for next second of clock output
tt = (time_t)(current_time + 1); tt = (time_t)(current_time + 1);