Think I found the bug
Some kind of integer issue with dividing or something
This commit is contained in:
47
main/main.c
47
main/main.c
@@ -60,14 +60,16 @@ typedef struct {
|
|||||||
|
|
||||||
my_dht22_data latest_datapoint;
|
my_dht22_data latest_datapoint;
|
||||||
|
|
||||||
my_dht22_data ten_minute_rolling_average;
|
|
||||||
uint32_t num_samples_last_ten_mins = 0;
|
|
||||||
|
|
||||||
void read_from_dht22() {
|
void read_from_dht22() {
|
||||||
// From datasheet:
|
// From datasheet:
|
||||||
// - Don't start within 1 second of powering on
|
// - Don't start within 1 second of powering on
|
||||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
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) {
|
while (run_dht) {
|
||||||
int ret = dht22_read();
|
int ret = dht22_read();
|
||||||
if (ret != DHT22_OK) {
|
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.
|
^ 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) {
|
if (num_samples_last_ten_mins == 0) {
|
||||||
ten_minute_rolling_average.rh = latest_datapoint.rh;
|
relative_humidity_tmp = latest_datapoint.rh;
|
||||||
ten_minute_rolling_average.temp = latest_datapoint.temp;
|
temperature_tmp = latest_datapoint.temp;
|
||||||
} else {
|
} else {
|
||||||
ten_minute_rolling_average.rh +=
|
relative_humidity_tmp +=
|
||||||
(latest_datapoint.rh - ten_minute_rolling_average.rh) /
|
((float) latest_datapoint.rh - relative_humidity_tmp) /
|
||||||
num_samples_last_ten_mins;
|
((float) num_samples_last_ten_mins + 1.0);
|
||||||
ten_minute_rolling_average.temp +=
|
temperature_tmp +=
|
||||||
(latest_datapoint.temp - ten_minute_rolling_aevrage.temp) /
|
((float) latest_datapoint.temp - temperature_tmp ) /
|
||||||
num_samples_last_ten_mins;
|
((float) num_samples_last_ten_mins + 1.0);
|
||||||
}
|
}
|
||||||
num_samples_last_ten_mins += 1;
|
num_samples_last_ten_mins += 1;
|
||||||
ESP_LOGI(TAG, "latest_datapoint.temp: %"PRIi16, latest_datapoint.temp);
|
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)
|
// This should be: if (10 minutes of sample taken)
|
||||||
// TODO: check max samples / second that can fit in 10 minutes
|
// TODO: check max samples / second that can fit in 10 minutes
|
||||||
if (num_samples_last_ten_mins >=
|
if (num_samples_last_ten_mins >=
|
||||||
((10 * 60) / (CONFIG_DHT22_PERIOD_POLL))) {
|
((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;
|
num_samples_last_ten_mins = 0;
|
||||||
|
|
||||||
time_t epoch;
|
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
|
// There must be a better way - I couldn't figure out how to do it
|
||||||
// nicely all at once
|
// 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
|
// -5 to get the middle of the averaged window
|
||||||
latest_datapoint.time.myt_min = timeinfo->tm_min - 5;
|
ten_minute_average.time.myt_min = timeinfo->tm_min - 5;
|
||||||
latest_datapoint.time.myt_hour = timeinfo->tm_hour;
|
ten_minute_average.time.myt_hour = timeinfo->tm_hour;
|
||||||
latest_datapoint.time.myt_day = timeinfo->tm_mday;
|
ten_minute_average.time.myt_day = timeinfo->tm_mday;
|
||||||
latest_datapoint.time.myt_month = timeinfo->tm_mon;
|
ten_minute_average.time.myt_month = timeinfo->tm_mon;
|
||||||
latest_datapoint.time.myt_year = timeinfo->tm_year;
|
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: Write the data out somewhere - into RAM maybe
|
||||||
// TODO: maybe push the data somewhere - or put a flag up that data
|
// TODO: maybe push the data somewhere - or put a flag up that data
|
||||||
|
|||||||
Reference in New Issue
Block a user