diff --git a/OpenCO2_Sensor.ino b/OpenCO2_Sensor.ino index 035e791..7bd1c95 100644 --- a/OpenCO2_Sensor.ino +++ b/OpenCO2_Sensor.ino @@ -10,12 +10,14 @@ - WiFiManager: https://github.com/tzapu/WiFiManager - ArduinoMqttClient (if MQTT is defined) */ -#define VERSION "v5.2" +#define VERSION "v5.3" #define HEIGHT_ABOVE_SEA_LEVEL 50 // Berlin #define TZ_DATA "CET-1CEST,M3.5.0,M10.5.0/3" // Europe/Berlin time zone from https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv #define LIGHT_SLEEP_TIME 500 -#define DEEP_SLEEP_TIME 29500 +#define DEEP_SLEEP_TIME 29124 +#define DEEP_SLEEP_TIME_NO_DISPLAY_UPDATE DEEP_SLEEP_TIME + 965 // offset for no display update +static unsigned long lastMeasurementTimeMs = 0; /* Includes display */ #include "DEV_Config.h" @@ -812,11 +814,22 @@ void loop() { #endif /* airgradient */ } + // force 5 seconds measurement Interval when not on Battery + if (!BatteryMode && !comingFromDeepSleep && (millis() - lastMeasurementTimeMs < 5000)) { + if (useWiFi) { + if (millis() - lastMeasurementTimeMs > LIGHT_SLEEP_TIME) { + goto_light_sleep(LIGHT_SLEEP_TIME); + return; // otherwise continues running! + } + } + goto_light_sleep(5000 - (millis() - lastMeasurementTimeMs)); + } + bool isDataReady = false; uint16_t ready_error = scd4x.getDataReadyFlag(isDataReady); if (ready_error || !isDataReady) { - if (BatteryMode && comingFromDeepSleep) goto_deep_sleep(DEEP_SLEEP_TIME); - else goto_light_sleep(LIGHT_SLEEP_TIME); + if (BatteryMode && comingFromDeepSleep) goto_deep_sleep(DEEP_SLEEP_TIME/2); + else goto_light_sleep(LIGHT_SLEEP_TIME/2); return; // otherwise continues running! } @@ -824,6 +837,7 @@ void loop() { uint16_t new_co2 = 400; float new_temperature = 0.0f; uint16_t error = scd4x.readMeasurement(new_co2, new_temperature, humidity); + lastMeasurementTimeMs = millis(); if (error) { char errorMessage[256]; errorToString(error, errorMessage, 256); @@ -834,7 +848,7 @@ void loop() { /* don't update in Battery mode, unless CO2 has changed by 3% or temperature by 0.5°C */ if (!TEST_MODE && BatteryMode && comingFromDeepSleep) { if ((abs(new_co2 - co2) < (0.03 * co2)) && (fabs(new_temperature - temperature) < 0.5)) { - goto_deep_sleep(DEEP_SLEEP_TIME); + goto_deep_sleep(DEEP_SLEEP_TIME_NO_DISPLAY_UPDATE); } } diff --git a/epd_abstraction.ino b/epd_abstraction.ino index 7880281..cf3bfaa 100644 --- a/epd_abstraction.ino +++ b/epd_abstraction.ino @@ -15,6 +15,15 @@ #define EINK_1IN54V2 //#define EINK_4IN2 +#ifdef EINK_1IN54V2 +#define WIDTH (float)EPD_1IN54_V2_WIDTH +#define HEIGHT (float)EPD_1IN54_V2_HEIGHT +#endif +#ifdef EINK_4IN2 +#define WIDTH EPD_4IN2_WIDTH +#define HEIGHT EPD_4IN2_HEIGHT +#endif + /* welcome */ #include @@ -477,7 +486,7 @@ void calculateTempHumStats(int* mintemp, int* maxtemp, int* avgtemp, int* minhum uint16_t index; if (overflow) index = NUM_MEASUREMENTS / 3; else index = ceil(currentIndex / 3.0); - for (int i=0; i<=index; i++) { + for (int i=0; i *maxtemp) *maxtemp = value; @@ -502,7 +511,7 @@ void calculateStatsCO2(int* min, int* max, int* avg) { uint16_t index; if (overflow) index = NUM_MEASUREMENTS; else index = currentIndex; - for (int i=0; i<=index; i++) { + for (int i=0; i *max) *max = value; @@ -515,11 +524,11 @@ void calculateStatsCO2(int* min, int* max, int* avg) { void displayCO2HistoryGraph() { int min, max, avg; calculateStatsCO2(&min, &max, &avg); - float yscale = 184.0 / (max - min); + float yscale = (HEIGHT-16.0) / (max - min); uint16_t index; if (overflow) index = NUM_MEASUREMENTS; else index = currentIndex; - float stepsPerPixel = index / 200.0; + float stepsPerPixel = index / (float)WIDTH; Paint_Clear(WHITE); Paint_DrawNum(0, 0, min, &Font16, BLACK, WHITE); @@ -532,17 +541,17 @@ void displayCO2HistoryGraph() { char duration[20]; sprintf(duration, "%.1f", index/120.0); strcat(duration, "h"); - Paint_DrawString_EN(0, 200-16, "-", &Font16, WHITE, BLACK); - Paint_DrawString_EN(11, 200-16, duration, &Font16, WHITE, BLACK); - Paint_DrawString_EN(200-11*3, 200-16, "now", &Font16, WHITE, BLACK); + Paint_DrawString_EN(0, HEIGHT-16, "-", &Font16, WHITE, BLACK); + Paint_DrawString_EN(11, HEIGHT-16, duration, &Font16, WHITE, BLACK); + Paint_DrawString_EN(WIDTH-11*3, HEIGHT-16, "now", &Font16, WHITE, BLACK); int privY = getCO2Measurement(0); - for (int x=1; x<200; x++) { + for (int x=1; xy; i--) { + for (int i = HEIGHT; i>y; i--) { if (!(i%5)) Paint_DrawPoint(x, i, BLACK, DOT_PIXEL_1X1, DOT_FILL_AROUND); } } @@ -553,13 +562,13 @@ void displayTempHumHistoryGraph() { int mintemp, maxtemp, avgtemp, minhum, maxhum, avghum; calculateTempHumStats(&mintemp, &maxtemp, &avgtemp, &minhum, &maxhum, &avghum); - float hight = 200.0 - 2*16.0; + float hight = HEIGHT - 2*16.0; float yscaletemp = hight / (maxtemp - mintemp); float yscalehum = hight / (maxhum - minhum); uint16_t index; if (overflow) index = NUM_MEASUREMENTS / 3; else index = ceil(currentIndex / 3.0); - float stepsPerPixel = index / 200.0; + float stepsPerPixel = index / (float)WIDTH; Paint_Clear(WHITE); char temp[20]; @@ -584,20 +593,20 @@ void displayTempHumHistoryGraph() { char duration[20]; sprintf(duration, "%.1f", index/40.0); strcat(duration, "h"); - Paint_DrawString_EN(0, 200-16, "-", &Font16, WHITE, BLACK); - Paint_DrawString_EN(11, 200-16, duration, &Font16, WHITE, BLACK); - Paint_DrawString_EN(200-11*3, 200-16, "now", &Font16, WHITE, BLACK); + Paint_DrawString_EN(0, HEIGHT-16, "-", &Font16, WHITE, BLACK); + Paint_DrawString_EN(11, HEIGHT-16, duration, &Font16, WHITE, BLACK); + Paint_DrawString_EN(WIDTH-11*3, HEIGHT-16, "now", &Font16, WHITE, BLACK); int privYtemp = getTempMeasurement(0); int privYhum = getHumMeasurement(0); - for (int x=1; x<200; x++) { + for (int x=1; xytemp; i--) { + for (int i = HEIGHT; i>ytemp; i--) { if (!(i%5)) Paint_DrawPoint(x, i, BLACK, DOT_PIXEL_2X2, DOT_FILL_AROUND); } Paint_DrawLine(x-1, privYhum, x, yhum, BLACK, DOT_PIXEL_2X2, LINE_STYLE_SOLID);