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 COLOR1 EPD_BLACK
|
||||||
#define COLOR2 EPD_RED
|
#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";
|
const char* nightscoutUrl = "http://nightscout.chrispr.org:8082/api/v1/entries?count=20";
|
||||||
|
|
||||||
@@ -58,38 +56,60 @@ void setup() {
|
|||||||
//turn EPD on
|
//turn EPD on
|
||||||
pinMode(EPD_ENABLE, OUTPUT);
|
pinMode(EPD_ENABLE, OUTPUT);
|
||||||
digitalWrite(EPD_ENABLE, HIGH);
|
digitalWrite(EPD_ENABLE, HIGH);
|
||||||
|
|
||||||
String nightscoutResponse = httpGETRequest(nightscoutUrl);
|
|
||||||
//Serial.println(nightscoutResponse);
|
|
||||||
DynamicJsonDocument doc(8192);
|
|
||||||
DeserializationError error = deserializeJson(doc, nightscoutResponse);
|
|
||||||
|
|
||||||
if (error) {
|
|
||||||
Serial.print(F("deserializeJson() failed: "));
|
|
||||||
Serial.println(error.f_str());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonArray array = doc.as<JsonArray>();
|
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);
|
||||||
|
|
||||||
|
DeserializationError error = deserializeJson(doc, nightscoutResponse);
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
Serial.print(F("deserializeJson() failed: "));
|
||||||
|
Serial.println(error.f_str());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Serial.println(nightscoutResponse);
|
||||||
|
|
||||||
|
jsonArray = doc.as<JsonArray>();
|
||||||
|
latestReading = jsonArray[0];
|
||||||
|
Serial.printf("Received %d sgv records\n", jsonArray.size());
|
||||||
|
|
||||||
|
SleepIfCGMWarmup(latestReading);
|
||||||
|
|
||||||
|
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.begin();
|
||||||
display.clearBuffer();
|
display.clearBuffer();
|
||||||
|
|
||||||
JsonVariant latestReading = array[0];
|
RenderGraph(jsonArray);
|
||||||
|
|
||||||
SleepIfCGMWarmup(latestReading);
|
|
||||||
|
|
||||||
RenderGraph(array);
|
|
||||||
RenderRightPane(latestReading);
|
RenderRightPane(latestReading);
|
||||||
RenderBatteryPercentage();
|
RenderBatteryPercentage();
|
||||||
|
|
||||||
display.display();
|
display.display();
|
||||||
|
|
||||||
unsigned long long dateInt = latestReading["date"].as<unsigned long long>();
|
|
||||||
int correctedDateInt = dateInt / 1000;
|
|
||||||
time_t lastSyncTime = (time_t)correctedDateInt;
|
|
||||||
|
|
||||||
SleepUntilNextReading(lastSyncTime);
|
SleepUntilNextReading(lastSyncTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,6 +146,7 @@ void initTime()
|
|||||||
tzset();
|
tzset();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Deprecated
|
||||||
int GetLastSyncTimeInSecondsWithinHour(String dateString) {
|
int GetLastSyncTimeInSecondsWithinHour(String dateString) {
|
||||||
//"dateString":"2022-02-11T19:59:42.481-0500"
|
//"dateString":"2022-02-11T19:59:42.481-0500"
|
||||||
int idx = dateString.indexOf("T");
|
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) {
|
void SleepUntilNextReading(time_t lastReading) {
|
||||||
struct tm timeinfo;
|
struct tm timeinfo;
|
||||||
time_t localTime;
|
time_t localTime;
|
||||||
@@ -229,16 +262,25 @@ void RenderGraph(JsonArray sgvRecords) {
|
|||||||
//Get 46 minutes into the past, to filter out older records
|
//Get 46 minutes into the past, to filter out older records
|
||||||
unsigned long timeCutoff = GetCurrentEpochTime() - (46 * 60);
|
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>();
|
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(obj["sgv"].as<String>());
|
||||||
|
Serial.println("device");
|
||||||
|
Serial.println(obj["device"].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);
|
unsigned long readingTime = (unsigned long)(obj["date"].as<unsigned long long>() / 1000);
|
||||||
|
|
||||||
Serial.printf("readingTime: %d\n", readingTime);
|
Serial.printf("readingTime: %d\n", readingTime);
|
||||||
if(readingTime < timeCutoff)
|
if(readingTime < timeCutoff)
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
Reference in New Issue
Block a user