Added start of HTTP server (& data storage)

This commit is contained in:
2023-11-06 20:41:17 +00:00
parent 37ce171347
commit 8fd658071a
5 changed files with 144 additions and 53 deletions

View File

@@ -6,14 +6,15 @@
#include "dht22.h"
#include "esp_log.h"
#include "esp_sntp.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>
@@ -29,15 +30,6 @@ static TaskHandle_t dht22_handle = NULL;
static TaskHandle_t pms5003_handle = NULL;
static TaskHandle_t ds18b20_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_DS18B20_ENABLED
#include "ds18b20.h"
@@ -65,13 +57,8 @@ void read_from_pms5003() {
#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;
deci_percent dht22_rh;
deci_degrees_c dht22_temp;
void read_from_dht22() {
// From datasheet:
@@ -81,33 +68,32 @@ void read_from_dht22() {
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) {
dht22_handle_error(ret);
} else {
latest_datapoint.temp = dht22_temperature();
latest_datapoint.rh = dht22_relative_humidity();
dht22_temp = dht22_temperature();
dht22_rh = dht22_relative_humidity();
ESP_LOGI(TAG,
"Sucesfully obtained DHT22 data (temp: %"PRIi16" rh: %"PRIu16")",
latest_datapoint.temp, latest_datapoint.rh);
"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 = latest_datapoint.rh;
temperature_tmp = latest_datapoint.temp;
relative_humidity_tmp = dht22_rh;
temperature_tmp = dht22_temp;
} else {
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) /
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, latest_datapoint.temp);
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);
@@ -119,30 +105,8 @@ void read_from_dht22() {
temperature_tmp);
num_samples_last_ten_mins = 0;
time_t epoch;
time(&epoch);
struct tm *timeinfo = localtime(&epoch);
add_dht22_data(relative_humidity_tmp, temperature_tmp);
// There must be a better way - I couldn't figure out how to do it
// nicely all at once
ten_minute_average.time.myt_sec = timeinfo->tm_sec;
// -5 to get the middle of the averaged window
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: Consider rounding this number - although, .1 of a degree C...
// no one is going to miss that. Probably the calculation require to
// do the rounding would affect the temperature that much anyways
// haha
ten_minute_average.temp = temperature_tmp;
ten_minute_average.rh = relative_humidity_tmp;
ESP_LOGW(TAG, "ten_min_avg.temp %" PRIi16 ", .rh %" PRIu16,
ten_minute_average.temp, ten_minute_average.rh);
// 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
@@ -165,6 +129,7 @@ void app_main(void) {
ESP_ERROR_CHECK(ret);
#ifdef CONFIG_DHT22_ENABLED
init_dht22_data();
xTaskCreate(read_from_dht22, "DHT22",
4 * 1024, // honestly I have _no_ idea
NULL,
@@ -190,5 +155,5 @@ void app_main(void) {
5, // no idea either
&ds18b20_handle);
#endif /* CONFIG DS18B20_ENABLED */
// start_wifi();
start_wifi();
}