Think I found the bug

Some kind of integer issue with dividing or something
This commit is contained in:
2023-10-31 22:40:17 +00:00
parent 6ed826ed7a
commit a62732a1b5

View File

@@ -60,14 +60,16 @@ typedef struct {
my_dht22_data latest_datapoint;
my_dht22_data ten_minute_rolling_average;
uint32_t num_samples_last_ten_mins = 0;
void read_from_dht22() {
// From datasheet:
// - Don't start within 1 second of powering on
vTaskDelay(pdMS_TO_TICKS(1000));
uint32_t num_samples_last_ten_mins = 0;
float relative_humidity_tmp;
float temperature_tmp;
my_dht22_data ten_minute_average;
while (run_dht) {
int ret = dht22_read();
if (ret != DHT22_OK) {
@@ -171,28 +173,31 @@ I (643351) YASPAM: ten_minute_average.temp: 16613, num_samples: 5
^ Did capture 10 mins of samples though! And appears to reset correctly.
I think the problem is with the integer math truncating any changes properly...
Or something weird like that - I forgot how careful to be around dividing with integers
*/
if (num_samples_last_ten_mins == 0) {
ten_minute_rolling_average.rh = latest_datapoint.rh;
ten_minute_rolling_average.temp = latest_datapoint.temp;
relative_humidity_tmp = latest_datapoint.rh;
temperature_tmp = 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_aevrage.temp) /
num_samples_last_ten_mins;
relative_humidity_tmp +=
((float) latest_datapoint.rh - relative_humidity_tmp) /
((float) num_samples_last_ten_mins + 1.0);
temperature_tmp +=
((float) latest_datapoint.temp - temperature_tmp ) /
((float) num_samples_last_ten_mins + 1.0);
}
num_samples_last_ten_mins += 1;
ESP_LOGI(TAG, "latest_datapoint.temp: %"PRIi16, latest_datapoint.temp);
ESP_LOGI(TAG, "ten_minute_average.temp: %"PRIi16", num_samples: %"PRIu32, ten_minute_rolling_average.temp, num_samples_last_ten_mins);
ESP_LOGI(TAG, "ten_minute_average.temp: %f, num_samples: %"PRIu32, temperature_tmp, num_samples_last_ten_mins);
// This should be: if (10 minutes of sample taken)
// TODO: check max samples / second that can fit in 10 minutes
if (num_samples_last_ten_mins >=
((10 * 60) / (CONFIG_DHT22_PERIOD_POLL))) {
ESP_LOGW(TAG, "10 minutes of samples collected. Average temp: %i", ten_minute_rolling_average.temp);
ESP_LOGW(TAG, "10 minutes of samples collected. Average temp: %f", temperature_tmp);
num_samples_last_ten_mins = 0;
time_t epoch;
@@ -201,13 +206,17 @@ I (643351) YASPAM: ten_minute_average.temp: 16613, num_samples: 5
// 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;
ten_minute_average.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;
ten_minute_average.time.myt_min = timeinfo->tm_min - 5;
ten_minute_average.time.myt_hour = timeinfo->tm_hour;
ten_minute_average.time.myt_day = timeinfo->tm_mday;
ten_minute_average.time.myt_month = timeinfo->tm_mon;
ten_minute_average.time.myt_year = timeinfo->tm_year;
// TODO check this conversion just works
ten_minute_average.temp = temperature_tmp;
ten_minute_average.rh = relative_humidity_tmp;
// TODO: Write the data out somewhere - into RAM maybe
// TODO: maybe push the data somewhere - or put a flag up that data