Files
esp-air-monitor/main/main.c

200 lines
6.9 KiB
C

/**
* Author: Arthur Roberts (c) 2023
*
* License: GPLv3+
*/
#include "dht22.h"
#include "esp_log.h"
#include "esp_sntp.h"
#include "freertos/FreeRTOS.h"
#include "freertos/projdefs.h"
#include "freertos/task.h"
#include "nvs_flash.h"
#include "portmacro.h"
#include "sdkconfig.h"
#include "wifi.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <inttypes.h>
static const char *TAG = "YASPAM";
static volatile bool run_dht = true;
static volatile bool run_pms5003 = true;
static TaskHandle_t dht22_handle = NULL;
static TaskHandle_t pms5003_handle = NULL;
typedef struct {
uint8_t myt_sec; // 0 -> 61
uint8_t myt_min; // 0 -> 69
uint8_t myt_hour; // 0 -> 23
uint8_t myt_day; // 1 -> 31
uint8_t myt_month; // 0 -> 11 !!
uint8_t myt_year; // years since 1900
} my_time;
#ifdef CONFIG_PMS5003_ENABLED
#include "pms5003.h"
void read_from_pms5003() {
while (run_pms5003) {
// TODO: actually collect some data
ESP_LOGI(TAG, "Got PMS5003 data!");
// TODO: Write the data out somewhere
vTaskDelay(pdMS_TO_TICKS(CONFIG_PMS5003_PERIOD_POLL));
}
}
#endif /* CONFIG_PMS5003_ENABLED */
#ifdef CONFIG_DHT22_ENABLED
#include "dht22.h"
typedef struct {
deci_percent rh;
deci_degrees_c temp;
my_time time;
} my_dht22_data;
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));
while (run_dht) {
int ret = dht22_read();
if (ret != DHT22_OK) {
dht22_handle_error(ret);
} else {
latest_datapoint.temp = dht22_temperature();
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 (e.g. time changing from 1970 -> current time within
// a 10 min period or just saving data saying it's from the 70s)
/*
* Appears to be a bug when hitting num samples = 64
* Here's a log:
W (327852) YASPAM: temperature: 19.500000 & relative humidity: 64.000000
W (327852) YASPAM: ten_minute_average.temp: 195, num_samples: 62
W (332862) YASPAM: temperature: 19.400000 & relative humidity: 64.100000
W (332862) YASPAM: ten_minute_average.temp: 2309, num_samples: 63
W (337872) YASPAM: temperature: 19.500000 & relative humidity: 64.300000
W (337872) YASPAM: ten_minute_average.temp: 18919, num_samples: 64
W (342882) YASPAM: temperature: 19.400000 & relative humidity: 64.200000
W (342882) YASPAM: ten_minute_average.temp: 18626, num_samples: 65
W (347892) YASPAM: temperature: 19.500000 & relative humidity: 64.400000
W (347892) YASPAM: ten_minute_average.temp: 34474, num_samples: 66
W (352902) YASPAM: temperature: 19.300000 & relative humidity: 64.300000
W (352912) YASPAM: ten_minute_average.temp: 34947, num_samples: 67
W (357912) YASPAM: temperature: 19.300000 & relative humidity: 64.500000
W (357912) YASPAM: ten_minute_average.temp: 35406, num_samples: 68
W (362922) YASPAM: temperature: 19.400000 & relative humidity: 64.600000
W (362922) YASPAM: ten_minute_average.temp: 35851, num_samples: 69
Another log - suggesting it's not to do with 64:
I (387992) YASPAM: ten_minute_average.temp: 187, num_samples: 74
W (392992) YASPAM: temperature: 18.700000 & relative humidity: 69.900000
I (393002) YASPAM: ten_minute_average.temp: 187, num_samples: 75
W (398002) YASPAM: temperature: 18.600000 & relative humidity: 69.900000
I (398012) YASPAM: ten_minute_average.temp: 53489, num_samples: 76
W (403012) YASPAM: temperature: 18.800000 & relative humidity: 69.900000
I (403022) YASPAM: ten_minute_average.temp: 53649, num_samples: 77
W (408022) YASPAM: temperature: 18.700000 & relative humidity: 69.800000
I (408032) YASPAM: ten_minute_average.temp: 53805, num_samples: 78
W (413032) YASPAM: temperature: 18.800000 & relative humidity: 69.600000
Something overrunning and stomping on my bits?
*/
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;
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);
// 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 >=
((uint32_t)CONFIG_DHT22_PERIOD_POLL * 1000 * 60 * 10)) {
ESP_LOGW(TAG, "10 minutes of samples collected. Average temp: %i", ten_minute_rolling_average.temp);
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
// ^ could do a cute Semaphore thingy here - I don't think I've
// actually used one before
}
}
}
vTaskDelay(pdMS_TO_TICKS(CONFIG_DHT22_PERIOD_POLL));
}
}
#endif /* CONFIG DHT22_ENABLED */
void app_main(void) {
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES ||
ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
#ifdef CONFIG_DHT22_ENABLED
xTaskCreate(read_from_dht22, "DHT22",
4 * 1024, // honestly I have _no_ idea
NULL,
5, // no idea either
&dht22_handle);
#endif /* CONFIG_DHT22_ENABLED */
#ifdef CONFIG_PMS5003_ENABLED
xTaskCreate(read_from_pms5003, "PMS5003",
4 * 1024, // honestly I have _no_ idea
NULL,
5, // no idea either
&pms5003_handle);
#endif /* CONFIG_PMS5003_ENABLED */
start_wifi();
}