From 556e0786da2e41926fd9c51853a3289588d4bca9 Mon Sep 17 00:00:00 2001 From: Arthur Roberts Date: Sat, 28 Oct 2023 17:33:49 +0100 Subject: [PATCH] Only get and convert time when using it --- main/main.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/main/main.c b/main/main.c index 2150a42..1c99702 100644 --- a/main/main.c +++ b/main/main.c @@ -81,18 +81,6 @@ void read_from_dht22() { // 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); - - // There must be a better way - I couldn't figure out how to do it - // nicely all at once - latest_datapoint.time.myt_sec = timeinfo->tm_sec; - latest_datapoint.time.myt_min = timeinfo->tm_min; - latest_datapoint.time.myt_hour = timeinfo->tm_hour; - latest_datapoint.time.myt_day = timeinfo->tm_mday; - latest_datapoint.time.myt_month = timeinfo->tm_mon; - latest_datapoint.time.myt_year = timeinfo->tm_year; if (num_samples_last_ten_mins == 0) { ten_minute_rolling_average.rh = latest_datapoint.rh; @@ -114,6 +102,21 @@ void read_from_dht22() { if (num_samples_last_ten_mins >= (CONFIG_DHT22_PERIOD_POLL * 1000 * 60 * 10)) { num_samples_last_ten_mins = 0; + + time_t epoch; + time(&epoch); + struct tm *timeinfo = localtime(&epoch); + + // There must be a better way - I couldn't figure out how to do it + // nicely all at once + latest_datapoint.time.myt_sec = timeinfo->tm_sec; + // -5 to get the middle of the averaged window + latest_datapoint.time.myt_min = timeinfo->tm_min - 5; + latest_datapoint.time.myt_hour = timeinfo->tm_hour; + 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: Write the data out somewhere - into RAM maybe // TODO: maybe push the data somewhere - or put a flag up that data // can be pushed