From 16112830159e840d5dffa19f55ecbe931482bc8a Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Mon, 18 Jun 2018 10:00:47 +0200 Subject: [PATCH] fix millis() --- src/main.cpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 973bb3a..f796cbd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -370,6 +370,16 @@ void ICACHE_FLASH_ATTR sendValues() { delay(1); // yield(); } +// WARNING: SOME TIME THE CALIBRATION VALUE CHANGES AND IT WILL RETURN A LOWER VALUE THAN BEFORE, BREAKING INTERVAL CHECKS!!! +unsigned long getTime() { + uint64_t rtc_t = system_get_rtc_time(); + uint32_t cal = system_rtc_clock_cali_proc(); +// uint32_t timemicrosec = rtc_t*((uint64) ((cal * 1000) >> 12)); +// Serial.printf("cal: %d.%d \r\n", ((cal*1000)>>12)/1000, ((cal*1000)>>12)%1000 ); +// Serial.printf("cal: %u\n", cal); + return ((uint64_t)(rtc_t*(uint64_t)cal) >> 12)/1000; +} + void fpm_wakup_cb_func1(void) { //gpio_pin_wakeup_disable(); //ESP.wdtFeed(); @@ -439,11 +449,13 @@ void setup() { } void loop() { - if (millis() - previousMillis >= postingInterval) { + unsigned long currentMillis = getTime(); + if ((long)(currentMillis - previousMillis) >= postingInterval) { + DEBUG_MSG("sending values... previousMillis:%u, millis():%u, difference:%d\n", previousMillis, currentMillis, currentMillis-previousMillis); sendValues(); - previousMillis = millis(); + previousMillis = getTime(); } - DEBUG_MSG("sleeping now... previousMillis:%u, millis():%u\n", previousMillis, millis()); + DEBUG_MSG("sleeping now...\n"); delay(10); lightsleep(); delay(10);