Fixed graphing issue causing old data to be graphed in wrong time bucket; Handled CGM warmup; Cleaned up code

This commit is contained in:
2022-02-16 20:49:02 -05:00
parent 59330768c5
commit d7b82b37ef

View File

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