Modified algorithm to stay awake and retry nightscout request to avoid missing last reading
This commit is contained in:
@@ -20,8 +20,6 @@ ThinkInk_290_Tricolor_Z10 display(EPD_DC, EPD_RESET, EPD_CS, SRAM_CS, EPD_BUSY);
|
||||
#define COLOR1 EPD_BLACK
|
||||
#define COLOR2 EPD_RED
|
||||
|
||||
const char* ssid = "Breqtest";
|
||||
const char* password = "aidsaids";
|
||||
|
||||
const char* nightscoutUrl = "http://nightscout.chrispr.org:8082/api/v1/entries?count=20";
|
||||
|
||||
@@ -59,9 +57,17 @@ void setup() {
|
||||
pinMode(EPD_ENABLE, OUTPUT);
|
||||
digitalWrite(EPD_ENABLE, HIGH);
|
||||
|
||||
int timeSinceLastSyncInSeconds = 0;
|
||||
JsonArray jsonArray;
|
||||
JsonVariant latestReading;
|
||||
time_t lastSyncTime;
|
||||
DynamicJsonDocument doc(8192);
|
||||
|
||||
while(timeSinceLastSyncInSeconds < (15 * 60))
|
||||
{ //break out of the loop if we haven't received a reading in 15 minutes to conserve battery
|
||||
String nightscoutResponse = httpGETRequest(nightscoutUrl);
|
||||
//Serial.println(nightscoutResponse);
|
||||
DynamicJsonDocument doc(8192);
|
||||
|
||||
DeserializationError error = deserializeJson(doc, nightscoutResponse);
|
||||
|
||||
if (error) {
|
||||
@@ -70,26 +76,40 @@ void setup() {
|
||||
return;
|
||||
}
|
||||
|
||||
JsonArray array = doc.as<JsonArray>();
|
||||
//Serial.println(nightscoutResponse);
|
||||
|
||||
|
||||
display.begin();
|
||||
display.clearBuffer();
|
||||
|
||||
JsonVariant latestReading = array[0];
|
||||
jsonArray = doc.as<JsonArray>();
|
||||
latestReading = jsonArray[0];
|
||||
Serial.printf("Received %d sgv records\n", jsonArray.size());
|
||||
|
||||
SleepIfCGMWarmup(latestReading);
|
||||
|
||||
RenderGraph(array);
|
||||
unsigned long long dateInt = latestReading["date"].as<unsigned long long>();
|
||||
int correctedDateInt = dateInt / 1000;
|
||||
lastSyncTime = (time_t)correctedDateInt;
|
||||
|
||||
timeSinceLastSyncInSeconds = GetLastSyncTimeDifference(lastSyncTime);
|
||||
Serial.printf("timeSinceLastSyncInSeconds %d\n", timeSinceLastSyncInSeconds);
|
||||
|
||||
if(timeSinceLastSyncInSeconds < 60)
|
||||
break; //got the most recent reading, display it
|
||||
|
||||
if(timeSinceLastSyncInSeconds < 300 && timeSinceLastSyncInSeconds > 120)
|
||||
break; //we missed it, or came in between readings, so sleep until the next one
|
||||
|
||||
//otherwise, try again in a few seconds
|
||||
Serial.printf("timeSinceLastSyncInSeconds is %d, so trying again", timeSinceLastSyncInSeconds);
|
||||
delay(1000);
|
||||
}
|
||||
display.begin();
|
||||
display.clearBuffer();
|
||||
|
||||
RenderGraph(jsonArray);
|
||||
RenderRightPane(latestReading);
|
||||
RenderBatteryPercentage();
|
||||
|
||||
display.display();
|
||||
|
||||
unsigned long long dateInt = latestReading["date"].as<unsigned long long>();
|
||||
int correctedDateInt = dateInt / 1000;
|
||||
time_t lastSyncTime = (time_t)correctedDateInt;
|
||||
|
||||
SleepUntilNextReading(lastSyncTime);
|
||||
}
|
||||
|
||||
@@ -126,6 +146,7 @@ void initTime()
|
||||
tzset();
|
||||
}
|
||||
|
||||
//Deprecated
|
||||
int GetLastSyncTimeInSecondsWithinHour(String dateString) {
|
||||
//"dateString":"2022-02-11T19:59:42.481-0500"
|
||||
int idx = dateString.indexOf("T");
|
||||
@@ -141,6 +162,18 @@ void SleepIfCGMWarmup(JsonVariant latestReading) {
|
||||
}
|
||||
}
|
||||
|
||||
int GetLastSyncTimeDifference(time_t lastReading) {
|
||||
struct tm timeinfo;
|
||||
time_t localTime;
|
||||
|
||||
if(!getLocalTime(&timeinfo)){
|
||||
Serial.println("Error getting local time");
|
||||
}
|
||||
localTime = mktime(&timeinfo);
|
||||
|
||||
double timeDiff = difftime(localTime, lastReading);
|
||||
return timeDiff;
|
||||
}
|
||||
void SleepUntilNextReading(time_t lastReading) {
|
||||
struct tm timeinfo;
|
||||
time_t localTime;
|
||||
@@ -229,16 +262,25 @@ void RenderGraph(JsonArray sgvRecords) {
|
||||
//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--) {
|
||||
for(int i=sgvRecords.size()-1; i >= 0; i--) {
|
||||
|
||||
JsonObject obj = sgvRecords[i].as<JsonObject>(); //v.as<JsonObject>();
|
||||
|
||||
if(&obj == NULL || obj["sgv"].as<String>() == NULL)
|
||||
continue;
|
||||
Serial.println("sgv");
|
||||
Serial.println(obj["sgv"].as<String>());
|
||||
Serial.println("device");
|
||||
Serial.println(obj["device"].as<String>());
|
||||
|
||||
|
||||
if(obj["device"].as<String>().indexOf("xDrip") == -1) {
|
||||
continue;
|
||||
}
|
||||
if(cnt > 0)
|
||||
continue;
|
||||
unsigned long readingTime = (unsigned long)(obj["date"].as<unsigned long long>() / 1000);
|
||||
|
||||
Serial.printf("readingTime: %d\n", readingTime);
|
||||
if(readingTime < timeCutoff)
|
||||
continue;
|
||||
|
||||
Reference in New Issue
Block a user