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:
21
main/main.c
21
main/main.c
@@ -78,6 +78,9 @@ void read_from_dht22() {
|
|||||||
latest_datapoint.rh = dht22_relative_humidity();
|
latest_datapoint.rh = dht22_relative_humidity();
|
||||||
|
|
||||||
if (has_ntp_time_obtained_once()) {
|
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_t epoch;
|
||||||
time(&epoch);
|
time(&epoch);
|
||||||
struct tm *timeinfo = localtime(&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_day = timeinfo->tm_mday;
|
||||||
latest_datapoint.time.myt_month = timeinfo->tm_mon;
|
latest_datapoint.time.myt_month = timeinfo->tm_mon;
|
||||||
latest_datapoint.time.myt_year = timeinfo->tm_year;
|
latest_datapoint.time.myt_year = timeinfo->tm_year;
|
||||||
// TODO: update ten minute rolling average
|
|
||||||
//
|
if (num_samples_last_ten_mins == 0) {
|
||||||
// TODO: Write the data out somewhere - into RAM maybe - I think only every 10 minutes
|
ten_minute_rolling_average.rh = latest_datapoint.rh;
|
||||||
// though
|
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));
|
vTaskDelay(pdMS_TO_TICKS(CONFIG_DHT22_PERIOD_POLL));
|
||||||
|
|||||||
Reference in New Issue
Block a user