another bugfixes in realtime handling

This commit is contained in:
Klaus K Wilting 2019-01-29 22:54:16 +01:00
parent 110e969ba8
commit 8c0ad6f849
6 changed files with 26 additions and 30 deletions

View File

@ -9,19 +9,11 @@
#include "gpsread.h" #include "gpsread.h"
#endif #endif
typedef enum {
useless = 0, // waiting for good enough signal
dirty = 1, // time data available but inconfident
reserve = 2, // clock was once synced but now may deviate
synced_LORA = 3, // clock driven by LORAWAN network
synced_GPS = 4 // best possible quality, clock is driven by GPS
} clock_state_t;
extern RtcDS3231<TwoWire> Rtc; // make RTC instance globally available extern RtcDS3231<TwoWire> Rtc; // make RTC instance globally available
int rtc_init(void); int rtc_init(void);
int set_rtctime(uint32_t UTCTime); int set_rtctime(RtcDateTime t);
int set_rtctime(RtcDateTime now); int set_rtctime(uint32_t t);
void sync_rtctime(void); void sync_rtctime(void);
time_t get_rtctime(void); time_t get_rtctime(void);
float get_rtctemp(void); float get_rtctemp(void);

View File

@ -9,6 +9,7 @@ static const char TAG[] = "main";
time_t userUTCTime; // Seconds since the UTC epoch time_t userUTCTime; // Seconds since the UTC epoch
unsigned long nextLoraTimeSync = millis(); unsigned long nextLoraTimeSync = millis();
unsigned long nextRTCTimeSync = millis() + TIME_WRITE_INTERVAL_RTC * 60000;
// do all housekeeping // do all housekeeping
void doHousekeeping() { void doHousekeeping() {
@ -34,6 +35,19 @@ void doHousekeeping() {
} }
#endif #endif
// do cyclic write back system time to RTC if we have an external time source
#if (defined TIME_SYNC_INTERVAL_LORA || defined TIME_SYNC_INTERVAL_GPS) && \
defined HAS_RTC
if ((millis() >= nextRTCTimeSync) && timeSet) {
nextRTCTimeSync = millis() + TIME_WRITE_INTERVAL_RTC *
60000; // set up next time sync period
if (!set_rtctime(now()))
ESP_LOGE(TAG, "RTC set time failure");
else
ESP_LOGI(TAG, "RTC time updated");
}
#endif
// task storage debugging // // task storage debugging //
ESP_LOGD(TAG, "Wifiloop %d bytes left | Taskstate = %d", ESP_LOGD(TAG, "Wifiloop %d bytes left | Taskstate = %d",
uxTaskGetStackHighWaterMark(wifiSwitchTask), uxTaskGetStackHighWaterMark(wifiSwitchTask),

View File

@ -457,7 +457,7 @@ void user_request_network_time_callback(void *pVoidUserUTCTime,
setTime(*pUserUTCTime); setTime(*pUserUTCTime);
ESP_LOGI(TAG, "LoRaWAN network has set the system time"); ESP_LOGI(TAG, "LoRaWAN network has set the system time");
#ifdef HAS_RTC #ifdef HAS_RTC
if (set_rtctime(*pUserUTCTime)) if (!set_rtctime(*pUserUTCTime))
ESP_LOGE(TAG, "RTC set time failure"); ESP_LOGE(TAG, "RTC set time failure");
#endif #endif
} }

View File

@ -429,7 +429,7 @@ void setup() {
else { else {
ESP_LOGI(TAG, "GPS has set the system time"); ESP_LOGI(TAG, "GPS has set the system time");
#ifdef HAS_RTC #ifdef HAS_RTC
if (set_rtctime(now())) if (!set_rtctime(now()))
ESP_LOGE(TAG, "RTC set time failure"); ESP_LOGE(TAG, "RTC set time failure");
#endif #endif
} }

View File

@ -82,9 +82,10 @@
#define RESPONSE_TIMEOUT_MS 60000 // firmware binary server connection timeout [milliseconds] #define RESPONSE_TIMEOUT_MS 60000 // firmware binary server connection timeout [milliseconds]
// settings for syncing time of node and external time sources // settings for syncing time of node and external time sources
#define TIME_SYNC_INTERVAL_GPS 60 // sync time each ... minutes with GPS [default = 60], comment out means off #define TIME_SYNC_INTERVAL_GPS 60 // sync time each .. minutes from source GPS [default = 60], comment out means off
#define TIME_SYNC_INTERVAL_RTC 60 // sync time each ... minutes with RTC [default = 60], comment out means off #define TIME_SYNC_INTERVAL_RTC 60 // sync time each .. minutes from RTC [default = 60], comment out means off
//#define TIME_SYNC_INTERVAL_LORA 60 // sync time each ... minutes with LORA network [default = 60], comment out means off #define TIME_WRITE_INTERVAL_RTC 60 // write time each .. minutes from GPS/LORA to RTC [default = 60], comment out means off
//#define TIME_SYNC_INTERVAL_LORA 60 // sync time each .. minutes from LORA network [default = 60], comment out means off
#define IF482_OFFSET 984 // 1sec minus IF482 serial transmit time [ms]: e.g. 9 bits * 17 bytes * 1/9600 bps = 16ms #define IF482_OFFSET 984 // 1sec minus IF482 serial transmit time [ms]: e.g. 9 bits * 17 bytes * 1/9600 bps = 16ms
// time zone, see https://github.com/JChristensen/Timezone/blob/master/examples/WorldClock/WorldClock.ino // time zone, see https://github.com/JChristensen/Timezone/blob/master/examples/WorldClock/WorldClock.ino

View File

@ -57,28 +57,17 @@ error:
} // rtc_init() } // rtc_init()
int set_rtctime(uint32_t t) {
// return = 0 -> error / return = 1 -> success
// block i2c bus access
if (I2C_MUTEX_LOCK()) {
Rtc.SetDateTime(RtcDateTime(t));
I2C_MUTEX_UNLOCK(); // release i2c bus access
return 1;
}
return 0;
} // set_rtctime()
int set_rtctime(RtcDateTime t) { int set_rtctime(RtcDateTime t) {
// return = 0 -> error / return = 1 -> success
// block i2c bus access
if (I2C_MUTEX_LOCK()) { if (I2C_MUTEX_LOCK()) {
Rtc.SetDateTime(t); Rtc.SetDateTime(t);
I2C_MUTEX_UNLOCK(); // release i2c bus access I2C_MUTEX_UNLOCK(); // release i2c bus access
return 1; return 1; // success
} }
return 0; return 0; // failure
} // set_rtctime() } // set_rtctime()
int set_rtctime(uint32_t t) { return set_rtctime(RtcDateTime(t)); };
time_t get_rtctime(void) { time_t get_rtctime(void) {
// never call now() in this function, this would cause a recursion! // never call now() in this function, this would cause a recursion!
time_t t = 0; time_t t = 0;