A lot of changes to get RTC clock synced and do accurate lastReading math to avoid missed readings
This commit is contained in:
@@ -39,13 +39,25 @@ void setup() {
|
||||
while (!Serial) { delay(10); }
|
||||
Serial.println("Nightscout CGM BS Monitor starting...");
|
||||
|
||||
ConnectToWifi();
|
||||
|
||||
struct tm timeinfo;
|
||||
initTime();
|
||||
|
||||
if(!getLocalTime(&timeinfo)){
|
||||
Serial.println("Failed to obtain time");
|
||||
}
|
||||
else {
|
||||
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
|
||||
}
|
||||
|
||||
//turn EPD on
|
||||
pinMode(EPD_ENABLE, OUTPUT);
|
||||
digitalWrite(EPD_ENABLE, HIGH);
|
||||
|
||||
ConnectToWifi();
|
||||
//ConnectToWifi();
|
||||
String nightscoutResponse = httpGETRequest(nightscoutUrl);
|
||||
Serial.println(nightscoutResponse);
|
||||
//Serial.println(nightscoutResponse);
|
||||
DynamicJsonDocument doc(8192);
|
||||
DeserializationError error = deserializeJson(doc, nightscoutResponse);
|
||||
|
||||
@@ -68,16 +80,39 @@ void setup() {
|
||||
display.display();
|
||||
|
||||
String dateString = array[0]["dateString"].as<String>();
|
||||
unsigned long long dateInt = array[0]["date"].as<unsigned long long>();
|
||||
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);
|
||||
|
||||
randomSeed(analogRead(0));
|
||||
//re-sync every 5th time or so
|
||||
if(random(5) == 0)
|
||||
SleepWithTimeSync(lastReadInSecondsWithinTheHour);
|
||||
else
|
||||
SleepFiveMins();
|
||||
*/
|
||||
SleepUntilNextReading(lastSyncTime);
|
||||
}
|
||||
|
||||
void initTime()
|
||||
{
|
||||
struct tm timeinfo;
|
||||
if(!getLocalTime(&timeinfo))
|
||||
configTime(0, 0, ntpServer);
|
||||
while(!getLocalTime(&timeinfo)){
|
||||
Serial.println("Sleeping until time is set..");
|
||||
delay(100);
|
||||
}
|
||||
//Setting EST timezone
|
||||
setenv("TZ", "EST+5EDT,M3.2.0/2,M11.1.0/2",1);
|
||||
tzset();
|
||||
}
|
||||
|
||||
int GetLastSyncTimeInSecondsWithinHour(String dateString) {
|
||||
@@ -87,9 +122,38 @@ int GetLastSyncTimeInSecondsWithinHour(String dateString) {
|
||||
int secondsInTheHour = dateString.substring(idx+7, idx+9).toInt();
|
||||
return (minutesInTheHour * 60) + secondsInTheHour;
|
||||
}
|
||||
|
||||
void SleepUntilNextReading(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);
|
||||
Serial.print(timeDiff);
|
||||
Serial.println(" seconds diff between lastReading and current time");
|
||||
int secondsToSleep = 300 - (int)timeDiff;
|
||||
//Add one more to not jump the gun
|
||||
secondsToSleep += 3;
|
||||
if(secondsToSleep > 0 && secondsToSleep < 300) {
|
||||
//secondsToSleep = secondsToSleep + 7;
|
||||
Serial.print("Sleeping ");
|
||||
Serial.println(secondsToSleep);
|
||||
esp_sleep_enable_timer_wakeup(secondsToSleep * uS_TO_S_FACTOR);
|
||||
Serial.flush();
|
||||
TurnOffEPD();
|
||||
esp_deep_sleep_start();
|
||||
}
|
||||
else {
|
||||
SleepFiveMins();
|
||||
}
|
||||
}
|
||||
void SleepWithTimeSync(int lastReadingInSeconds) {
|
||||
|
||||
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
|
||||
//configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
|
||||
struct tm timeinfo;
|
||||
if(!getLocalTime(&timeinfo)){
|
||||
Serial.println("Failed to obtain time");
|
||||
@@ -274,12 +338,21 @@ void ConnectToWifi() {
|
||||
//WiFi.mode(WIFI_STA);
|
||||
//WiFi.disconnect();
|
||||
WiFi.begin(ssid, password);
|
||||
int cnt=0;
|
||||
Serial.println("Connecting to WiFi...");
|
||||
while(WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print("status:" );
|
||||
Serial.println(WiFi.status());
|
||||
Serial.println(WiFi.localIP());
|
||||
Serial.print('.');
|
||||
//Serial.print("status:" );
|
||||
//Serial.println(WiFi.status());
|
||||
//Serial.println(WiFi.localIP());
|
||||
cnt++;
|
||||
if(cnt > 50){
|
||||
Serial.println("Cycling wifi");
|
||||
WiFi.disconnect();
|
||||
WiFi.begin(ssid, password);
|
||||
cnt = 0;
|
||||
}
|
||||
}
|
||||
Serial.println("");
|
||||
Serial.print("Connected to WiFi network with IP Address: ");
|
||||
|
||||
Reference in New Issue
Block a user