Skip to content

Commit

Permalink
v5.3 Website timing and History graph bugfix
Browse files Browse the repository at this point in the history
Website graph timing improvement: Store CO2 values exactly every 30 seconds.

History graph:
4.2" display now supports full screen.
Bugfix for min Value.
  • Loading branch information
davidkreidler committed Oct 10, 2024
1 parent 67e9cec commit 24bbb80
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 24 deletions.
24 changes: 19 additions & 5 deletions OpenCO2_Sensor.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -812,18 +814,30 @@ 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!
}

// Read co2 measurement
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);
Expand All @@ -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);
}
}

Expand Down
47 changes: 28 additions & 19 deletions epd_abstraction.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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 <EEPROM.h>

Expand Down Expand Up @@ -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<index; i++) {
value = getTempMeasurement(i);
if (value < *mintemp) *mintemp = value;
if (value > *maxtemp) *maxtemp = value;
Expand All @@ -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<index; i++) {
value = getCO2Measurement(i);
if (value < *min) *min = value;
if (value > *max) *max = value;
Expand All @@ -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);
Expand All @@ -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; x<WIDTH; x++) {
int y = getCO2Measurement((int)(x * stepsPerPixel));
y = 200.0 - ((y - min) * yscale);
y = (float)HEIGHT - ((y - min) * yscale);
Paint_DrawLine(x-1, privY, x, y, BLACK, DOT_PIXEL_2X2, LINE_STYLE_SOLID);
privY = y;
for (int i = 200; i>y; i--) {
for (int i = HEIGHT; i>y; i--) {
if (!(i%5)) Paint_DrawPoint(x, i, BLACK, DOT_PIXEL_1X1, DOT_FILL_AROUND);
}
}
Expand All @@ -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];
Expand All @@ -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; x<WIDTH; x++) {
int ytemp = getTempMeasurement((int)(x * stepsPerPixel));
int yhum = getHumMeasurement((int)(x * stepsPerPixel));
ytemp = 200.0 - ((ytemp - mintemp) * yscaletemp);
yhum = 200.0 - ((yhum - minhum) * yscalehum);
ytemp = (float)HEIGHT - ((ytemp - mintemp) * yscaletemp);
yhum = (float)HEIGHT - ((yhum - minhum) * yscalehum);
Paint_DrawLine(x-1, privYtemp, x, ytemp, BLACK, DOT_PIXEL_2X2, LINE_STYLE_SOLID);
for (int i = 200; i>ytemp; 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);
Expand Down

0 comments on commit 24bbb80

Please sign in to comment.