diff --git a/main/main.c b/main/main.c index fd9ed60..8824090 100644 --- a/main/main.c +++ b/main/main.c @@ -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));