I think this is the correct way to implement a rolling average

I can probably the time conversion part into the "saving" section when
that's done
This commit is contained in:
2023-10-28 17:17:50 +01:00
parent bbfc55a066
commit ac1fcbc5a5

View File

@@ -78,6 +78,9 @@ void read_from_dht22() {
latest_datapoint.rh = dht22_relative_humidity();
if (has_ntp_time_obtained_once()) {
// I _think_ I only want to be doing that rolling average and saving if/when
// we've got an actual time to do that with. I think that'd avoid some weird
// bugs (time changing from 1970 -> current time within a 10 min period)
time_t epoch;
time(&epoch);
struct tm *timeinfo = localtime(&epoch);
@@ -89,10 +92,20 @@ void read_from_dht22() {
latest_datapoint.time.myt_day = timeinfo->tm_mday;
latest_datapoint.time.myt_month = timeinfo->tm_mon;
latest_datapoint.time.myt_year = timeinfo->tm_year;
// TODO: update ten minute rolling average
//
// TODO: Write the data out somewhere - into RAM maybe - I think only every 10 minutes
// though
if (num_samples_last_ten_mins == 0) {
ten_minute_rolling_average.rh = latest_datapoint.rh;
ten_minute_rolling_average.temp = latest_datapoint.temp;
} else {
ten_minute_rolling_average.rh +=
(latest_datapoint.rh - ten_minute_rolling_average.rh) /
num_samples_last_ten_mins;
ten_minute_rolling_average.temp +=
(latest_datapoint.temp - ten_minute_rolling_average.temp) / num_samples_last_ten_mins;
}
num_samples_last_ten_mins += 1;
// TODO: If 10 minute average taken, save it and reset num_samples and ten_min_av
// TODO: Write the data out somewhere - into RAM maybe
}
}
vTaskDelay(pdMS_TO_TICKS(CONFIG_DHT22_PERIOD_POLL));