diff --git a/NightscoutEInkClient.ino b/NightscoutEInkClient.ino index 5d87cdd..0772b8e 100644 --- a/NightscoutEInkClient.ino +++ b/NightscoutEInkClient.ino @@ -33,13 +33,15 @@ const char* ntpServer = "pool.ntp.org"; #define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */ #define TIME_TO_SLEEP 5 /* Time ESP32 will go to sleep (in seconds) */ +RTC_DATA_ATTR int sleepCount = 0; + void setup() { // put your setup code here, to run once: Serial.begin(115200); while (!Serial) { delay(10); } Serial.println("Nightscout CGM BS Monitor starting..."); - - print_wakeup_reason(); + + //print_wakeup_reason(); ConnectToWifi(); @@ -57,7 +59,6 @@ void setup() { pinMode(EPD_ENABLE, OUTPUT); digitalWrite(EPD_ENABLE, HIGH); - //ConnectToWifi(); String nightscoutResponse = httpGETRequest(nightscoutUrl); //Serial.println(nightscoutResponse); DynamicJsonDocument doc(8192); @@ -75,31 +76,20 @@ void setup() { display.begin(); display.clearBuffer(); + JsonVariant latestReading = array[0]; + + SleepIfCGMWarmup(latestReading); + RenderGraph(array); - RenderRightPane(array[0]); + RenderRightPane(latestReading); RenderBatteryPercentage(); display.display(); - String dateString = array[0]["dateString"].as(); - unsigned long long dateInt = array[0]["date"].as(); + unsigned long long dateInt = latestReading["date"].as(); int correctedDateInt = dateInt / 1000; time_t lastSyncTime = (time_t)correctedDateInt; - /* - Serial.println(dateInt); - Serial.println(correctedDateInt); - struct tm *lastSync; - //Serial.println(lastSync); - - lastSync = localtime(&lastSyncTime); - Serial.print("Last Sync Time "); - Serial.println(lastSync, "%A, %B %d %Y %H:%M:%S"); - int lastReadInSecondsWithinTheHour = GetLastSyncTimeInSecondsWithinHour(dateString); - Serial.print("lastReadInSecondsWithinTheHour: "); - Serial.println(lastReadInSecondsWithinTheHour); - - SleepWithTimeSync(lastReadInSecondsWithinTheHour); - */ + SleepUntilNextReading(lastSyncTime); } @@ -123,7 +113,10 @@ void initTime() { struct tm timeinfo; //if(!getLocalTime(&timeinfo)) - configTime(0, 0, ntpServer); + if(sleepCount % 5 == 0) { + Serial.println("Syncing with NTP server.."); + configTime(0, 0, ntpServer); + } while(!getLocalTime(&timeinfo)){ Serial.println("Sleeping until time is set.."); delay(100); @@ -141,6 +134,13 @@ int GetLastSyncTimeInSecondsWithinHour(String dateString) { return (minutesInTheHour * 60) + secondsInTheHour; } +void SleepIfCGMWarmup(JsonVariant latestReading) { + if(latestReading["direction"].as().equalsIgnoreCase("NOT COMPUTABLE")) { + Serial.println("CGM warmup period. Sleeping 5"); + SleepFiveMins(); + } +} + void SleepUntilNextReading(time_t lastReading) { struct tm timeinfo; time_t localTime; @@ -155,7 +155,8 @@ void SleepUntilNextReading(time_t lastReading) { Serial.println(" seconds diff between lastReading and current time"); int secondsToSleep = 300 - (int)timeDiff; //Add one more to not jump the gun - secondsToSleep += 20; + secondsToSleep += 10; + sleepCount++; if(secondsToSleep > 0 && secondsToSleep < 300) { //secondsToSleep = secondsToSleep + 7; Serial.print("Sleeping "); @@ -169,34 +170,16 @@ void SleepUntilNextReading(time_t lastReading) { SleepFiveMins(); } } -void SleepWithTimeSync(int lastReadingInSeconds) { - - //configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); + +unsigned long GetCurrentEpochTime() { struct tm timeinfo; + time_t localTime; + if(!getLocalTime(&timeinfo)){ - Serial.println("Failed to obtain time"); - SleepFiveMins(); + Serial.println("Error getting local time"); } - //Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S"); - - int secondsInTheHour = (timeinfo.tm_min * 60) + timeinfo.tm_sec; - Serial.print("currentSecondsWithinTheHour"); - Serial.println(secondsInTheHour); - int secondsToSleep = 300 - (secondsInTheHour - lastReadingInSeconds); - Serial.print("Would like to sleep "); - Serial.println(secondsToSleep); - if(secondsToSleep > 0 && secondsToSleep < 300) { - //buffer for new processing - secondsToSleep = secondsToSleep + 7; - esp_sleep_enable_timer_wakeup(secondsToSleep * uS_TO_S_FACTOR); - Serial.flush(); - TurnOffEPD(); - esp_deep_sleep_start(); - } - else { - SleepFiveMins(); - } - + localTime = mktime(&timeinfo); + return (unsigned long)localTime; } void RenderBatteryPercentage() { @@ -241,22 +224,28 @@ void RenderGraph(JsonArray sgvRecords) { boolean redrawScreen = true; double x = 0; double y = 100; - double cnt = -45; - - //for(JsonVariant v : sgvRecords) { - for(int i=sgvRecords.size(); i >= 0; i--) { - + int cnt = -45; + + //Get 46 minutes into the past, to filter out older records + unsigned long timeCutoff = GetCurrentEpochTime() - (46 * 60); + + for(int i=sgvRecords.size(); i >= 0; i--) { + JsonObject obj = sgvRecords[i].as(); //v.as(); Serial.println(obj["sgv"].as()); - if(obj["device"].as().indexOf("xDrip") == -1) + if(obj["device"].as().indexOf("xDrip") == -1) { continue; + } if(cnt > 0) continue; + unsigned long readingTime = (unsigned long)(obj["date"].as() / 1000); + Serial.printf("readingTime: %d\n", readingTime); + if(readingTime < timeCutoff) + continue; - //Graph(display, cnt, obj["sgv"].as(), 20, 110, 210, 105, 0,45, 5,60,180,10,"", "", "", COLOR1, COLOR1, COLOR2, COLOR1, COLOR0, redrawScreen); - Graph(display, cnt, obj["sgv"].as(), 20, 110, 210, 105, -45,0, 5,60,180,10,"", "", "", COLOR1, COLOR1, COLOR2, COLOR1, COLOR0, redrawScreen); - //GraphReversed(display, cnt, obj["sgv"].as(), 20, 110, 210, 105, 0,45, 5,60,180,10,"", "", "", COLOR1, COLOR1, COLOR2, COLOR1, COLOR0, redrawScreen); - cnt += 5; + Graph(display, cnt, obj["sgv"].as(), 20, 110, 210, 105, -45,0, 5,60,180,10,"", "", "", COLOR1, COLOR1, COLOR2, COLOR1, COLOR0, redrawScreen); + Serial.printf("Graphing x: %+d y: %f\n", cnt, obj["sgv"].as()); + cnt += 5; } } @@ -307,10 +296,6 @@ void RenderRightPane(JsonVariant sgvRecord) { display.print(delta); - //display.setCursor(235, 70); - //display.setTextSize(3); - //display.setTextColor(COLOR1); - //display.drawChar(235, 60, 0x1A, COLOR1, COLOR0, 3); RenderTrendArrow(sgvRecord["direction"].as()); //time