add time sync cycle (experimental)

This commit is contained in:
Klaus K Wilting 2018-11-16 09:43:47 +01:00
parent 9c66167303
commit 3e5a0e4058
2 changed files with 18 additions and 3 deletions

View File

@ -8,6 +8,7 @@
static const char TAG[] = "main"; static const char TAG[] = "main";
uint32_t userUTCTime; // Seconds since the UTC epoch uint32_t userUTCTime; // Seconds since the UTC epoch
unsigned long nextTimeSync = millis();
// do all housekeeping // do all housekeeping
void doHousekeeping() { void doHousekeeping() {
@ -20,7 +21,15 @@ void doHousekeeping() {
do_reset(); do_reset();
spi_housekeeping(); spi_housekeeping();
do_timesync();
// time sync once per TIME_SYNC_INTERVAL_MIN
#ifdef TIME_SYNC
if (millis() >= nextTimeSync) {
nextTimeSync = millis() + TIME_SYNC_INTERVAL_MIN *
60000; // set up next time sync period
do_timesync();
}
#endif
// task storage debugging // // task storage debugging //
ESP_LOGD(TAG, "Wifiloop %d bytes left", ESP_LOGD(TAG, "Wifiloop %d bytes left",
@ -75,6 +84,7 @@ void reset_counters() {
} }
void do_timesync() { void do_timesync() {
#ifdef TIME_SYNC
// sync time & date if we have valid gps time // sync time & date if we have valid gps time
#ifdef HAS_GPS #ifdef HAS_GPS
if (gps.time.isValid()) { if (gps.time.isValid()) {
@ -84,13 +94,14 @@ void do_timesync() {
second()); second());
return; return;
} else { } else {
ESP_LOGW(TAG, "No valid GPS time"); ESP_LOGI(TAG, "No valid GPS time");
} }
#endif #endif
// Schedule a network time request at the next possible time // Schedule a network time request at the next possible time
LMIC_requestNetworkTime(user_request_network_time_callback, &userUTCTime); LMIC_requestNetworkTime(user_request_network_time_callback, &userUTCTime);
ESP_LOGI(TAG, "Network time request scheduled"); ESP_LOGI(TAG, "Network time request scheduled");
} #endif
} // do_timesync()
#ifndef VERBOSE #ifndef VERBOSE
int redirect_log(const char *fmt, va_list args) { int redirect_log(const char *fmt, va_list args) {

View File

@ -72,5 +72,9 @@
#define OTA_MIN_BATT 3600 // minimum battery level for OTA [millivolt] #define OTA_MIN_BATT 3600 // minimum battery level for OTA [millivolt]
#define RESPONSE_TIMEOUT_MS 60000 // firmware binary server connection timeout [milliseconds] #define RESPONSE_TIMEOUT_MS 60000 // firmware binary server connection timeout [milliseconds]
// setting for syncing time of node
#define TIME_SYNC 1 // comment out if no time sync of node necessary
#define TIME_SYNC_INTERVAL_MIN 60 // sync time each ... minutes [default = 60]
// LMIC settings // LMIC settings
// moved to src/lmic_config.h // moved to src/lmic_config.h