Files
esp-air-monitor/main/main.c
2024-05-29 19:12:31 +01:00

164 lines
4.6 KiB
C

/**
* Author: Arthur Roberts (c) 2023
*
* License: GPLv3+
*/
#include "dht22.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/projdefs.h"
#include "freertos/task.h"
#include "http.h"
#include "nvs_flash.h"
//#include "portmacro.h"
#include "sdkconfig.h"
#include "wifi.h"
#include "http.h"
#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
static const char *TAG = "YASPAM";
static volatile bool run_dht = true;
static volatile bool run_pms5003 = true;
static volatile bool run_ds18b20 = true;
static TaskHandle_t dht22_handle = NULL;
static TaskHandle_t pms5003_handle = NULL;
static TaskHandle_t ds18b20_handle = NULL;
#ifdef CONFIG_DS18B20_ENABLED
#include "ds18b20.h"
void read_from_ds18b20() {
ESP_LOGI(TAG, "Starting DS18B20 Task");
while (run_pms5003) {
int ret = ds18b20_read();
vTaskDelay(pdMS_TO_TICKS(CONFIG_DS18B20_PERIOD_POLL * 1000));
}
}
#endif /* CONFIG_PMS5003_ENABLED */
#ifdef CONFIG_PMS5003_ENABLED
#include "pms5003.h"
void read_from_pms5003() {
while (run_pms5003) {
int ret = pms5003_read();
vTaskDelay(pdMS_TO_TICKS(CONFIG_PMS5003_PERIOD_POLL * 1000));
}
}
#endif /* CONFIG_PMS5003_ENABLED */
#ifdef CONFIG_DHT22_ENABLED
#include "dht22.h"
deci_percent dht22_rh;
deci_degrees_c dht22_temp;
void read_from_dht22() {
// From datasheet:
// - Don't start within 1 second of powering on
vTaskDelay(pdMS_TO_TICKS(1000));
ESP_LOGI(TAG, "Starting DHT22 Task");
uint32_t num_samples_last_ten_mins = 0;
float relative_humidity_tmp;
float temperature_tmp;
while (run_dht) {
int ret = dht22_read();
if (ret != DHT22_OK) {
dht22_handle_error(ret);
} else {
dht22_temp = dht22_temperature();
dht22_rh = dht22_relative_humidity();
ESP_LOGI(TAG,
"Sucesfully obtained DHT22 data (temp: %" PRIi16 " rh: %" PRIu16
")",
dht22_temp, dht22_rh);
// No point collecting rolling average if we don't know what the time is
if (has_ntp_time_obtained_once()) {
if (num_samples_last_ten_mins == 0) {
relative_humidity_tmp = dht22_rh;
temperature_tmp = dht22_temp;
} else {
relative_humidity_tmp += ((float)dht22_rh - relative_humidity_tmp) /
((float)num_samples_last_ten_mins + 1.0);
temperature_tmp += ((float)dht22_temp - temperature_tmp) /
((float)num_samples_last_ten_mins + 1.0);
}
num_samples_last_ten_mins += 1;
ESP_LOGI(TAG, "latest_datapoint.temp: %" PRIi16, dht22_temp);
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))) {
*/
if (num_samples_last_ten_mins >= 20) {
ESP_LOGW(TAG, "10 minutes of samples collected. Average temp: %f",
temperature_tmp);
num_samples_last_ten_mins = 0;
add_dht22_data(relative_humidity_tmp, temperature_tmp);
// 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 * 1000));
}
}
#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 */
#ifdef CONFIG_DS18B20_ENABLED
xTaskCreate(read_from_ds18b20, "DS18B20",
4 * 1024, // honestly I have _no_ idea
NULL,
5, // no idea either
&ds18b20_handle);
#endif /* CONFIG DS18B20_ENABLED */
start_wifi();
start_server();
init_dht22_data();
}