From 353e1ee452f1f34a1d31f8d268367a4fd4ad963c Mon Sep 17 00:00:00 2001 From: Arthur Roberts Date: Wed, 29 May 2024 19:12:31 +0100 Subject: [PATCH] Added latest updates --- components/http_server/http.c | 33 +++++++++++++++++++++++++++------ components/http_server/http.h | 2 ++ main/main.c | 6 +++++- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/components/http_server/http.c b/components/http_server/http.c index 6013a12..4727443 100644 --- a/components/http_server/http.c +++ b/components/http_server/http.c @@ -6,8 +6,12 @@ #include "esp_log.h" #include "esp_http_server.h" +const static char *TAG = "HTTP"; + // 2 Days worth of samples -#define NUM_SAMPLES_TO_STORE (( 24 * 60 ) / 10 ) * 2 +#define NUM_SAMPLES_TO_STORE (((24 * 60) / 10) * 2) + +httpd_handle_t server = NULL; typedef struct { uint8_t myt_sec; // 0 -> 61 @@ -49,25 +53,32 @@ my_time get_time() { esp_err_t dht22_data_get_handler(httpd_req_t *req) { +// if (dht22_num == 0) { +// httpd_resp_send(req, resp, HTTPD_RESP_USE_STRLEN); +// } char *resp; - size_t size_of_csv_line = strlen("YYYY:MM:DD HH:MM:SS,XXXX,YYYY\n"); + size_t size_of_csv_line = strlen("YY:MM:DD HH:MM:SS,XXXX,YYYY\n"); resp = calloc(dht22_num, size_of_csv_line + 1); for (int i = 0; i < dht22_num; i++) { + // FIXME - there are random NULL characters that seem to appear + // in the data - I think the snprintf could be modified slightly + // FIXME - For some reason the data string starts with a '1' + // unsure whether it's from the year or what my_dht22_data d = dht22_buffer[i]; #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wformat-truncation" snprintf(resp + (i * size_of_csv_line), size_of_csv_line, - "%04d:%02d:%02d %02d:%02d:%02d\t%04d\t%04d\n", d.time.myt_year, + "%02d:%02d:%02d %02d:%02d:%02d\t%04d\t%04d\n", d.time.myt_year, d.time.myt_month, d.time.myt_day, d.time.myt_hour, d.time.myt_min, d.time.myt_sec, d.rh, d.temp); #pragma GCC diagnostic pop } // shouldn't need to null terminate this becuase of calloc - httpd_resp_send(req, resp, HTTPD_RESP_USE_STRLEN); + httpd_resp_send(req, resp, dht22_num * size_of_csv_line + 1); return ESP_OK; } @@ -81,7 +92,7 @@ httpd_uri_t dht22_data_get = { void init_dht22_data() { dht22_buffer = malloc(NUM_SAMPLES_TO_STORE * sizeof(my_dht22_data)); - // register an http endpoint thingy probably? + httpd_register_uri_handler(server, &dht22_data_get); } void add_dht22_data(float rh, float temp) { @@ -94,5 +105,15 @@ void add_dht22_data(float rh, float temp) { dht22_data.temp = round(temp); dht22_data.rh = round(rh); dht22_buffer[dht22_num] = dht22_data; - dht22_num++; + dht22_num += 1; + ESP_LOGI(TAG, "Logged %i dht22 values", dht22_num); + // TODO: handle if dht22_num == MAX + // use a circular buffer or something maybe? +} + +void start_server() { + httpd_config_t config = HTTPD_DEFAULT_CONFIG(); + + ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port); + httpd_start(&server, &config); } diff --git a/components/http_server/http.h b/components/http_server/http.h index f56c924..36ec09e 100644 --- a/components/http_server/http.h +++ b/components/http_server/http.h @@ -8,3 +8,5 @@ void init_dht22_data(); // - could think about abstracting the average out into this // function maybe. Something for the future! void add_dht22_data(float rh, float temp); + +void start_server(); diff --git a/main/main.c b/main/main.c index cf6e642..8b06e1b 100644 --- a/main/main.c +++ b/main/main.c @@ -99,8 +99,11 @@ void read_from_dht22() { // 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; @@ -129,7 +132,6 @@ 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, @@ -156,4 +158,6 @@ void app_main(void) { &ds18b20_handle); #endif /* CONFIG DS18B20_ENABLED */ start_wifi(); + start_server(); + init_dht22_data(); }