diff --git a/src/main.cpp b/src/main.cpp index d5b4927..e82dfbc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -111,6 +111,15 @@ struct __attribute__((packed)) SENSOR_DATA { float rssi; } sd; +static uint32_t cal = system_rtc_clock_cali_proc(); // WARNING: UPDATING THIS WILL MAKE THE rtcMillis() RETURN A LOWER VALUE THEN BEFORE, EVEN IF TIME PASSED, BREAKING INTERVAL CHECKS! +unsigned long rtcMillis() { + uint64_t rtc_t = system_get_rtc_time(); +// 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 sendESPNOW() { // unsigned long espnowmillis = millis(); @@ -170,9 +179,9 @@ void ICACHE_FLASH_ATTR sendValues() { // sds.sleep(); - if (millis() - geiger_previousMillis > 60000) { - sd.cpm = geiger_counts * 60000 / (millis() - geiger_previousMillis); - geiger_previousMillis = millis(); + if (rtcMillis() - geiger_previousMillis > 60000) { + sd.cpm = geiger_counts * 60000 / (rtcMillis() - geiger_previousMillis); + geiger_previousMillis = rtcMillis(); geiger_counts = 0; geigeraverage.addValue(sd.cpm); } @@ -255,7 +264,7 @@ void ICACHE_FLASH_ATTR sendValues() { if ( ip != INADDR_NONE && dns != INADDR_NONE && gateway != INADDR_NONE && subnet != INADDR_NONE && ((ip[0] == 192 && ip[1] == 168) || (ip[0] == 172 && ip[1] == 16)) && strlen(ssid) > 0 && strlen(password) > 0 - && (millis() - last_dhcp < dhcp_interval) + && (rtcMillis() - last_dhcp < dhcp_interval) ) { DEBUG_MSG("static ip\n"); @@ -303,7 +312,7 @@ void ICACHE_FLASH_ATTR sendValues() { subnet = WiFi.subnetMask(); strncpy(ssid, WiFi.SSID().c_str(), 64); strncpy(password, WiFi.psk().c_str(), 64); - last_dhcp = millis(); + last_dhcp = rtcMillis(); } sd.rssi = WiFi.RSSI(); @@ -367,7 +376,7 @@ void ICACHE_FLASH_ATTR sendValues() { } loop_count++; - last_wifi_activity = millis(); + last_wifi_activity = rtcMillis(); WiFi.disconnect(); WiFi.mode(WIFI_OFF); WiFi.forceSleepBegin(); @@ -408,7 +417,7 @@ void setup() { } void loop() { - //DEBUG_MSG("%d\n", millis() - last_wifi_activity); + //DEBUG_MSG("%d\n", rtcMillis() - last_wifi_activity); sendValues(); delay(postingInterval); }