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

@@ -0,0 +1,4 @@
idf_component_register(SRCS "http.c"
INCLUDE_DIRS "."
PRIV_REQUIRES dht22 wifi esp_http_server
)

View File

@@ -0,0 +1,14 @@
menu "HTTP"
# config WIFI_SSID
# string "WiFi SSID"
# default "VM8094728"
# help
# SSID (network name) for the example to connect to.
# config WIFI_PASSWORD
# string "WiFi Password"
# default "tx3YrxvzMrbr"
# help
# WiFi password (WPA or WPA2) for the example to use.
endmenu

View File

@@ -0,0 +1,98 @@
#include "http.h"
#include "esp_sntp.h"
#include "wifi.h"
#include <math.h>
#include <stdlib.h>
#include "esp_log.h"
#include "esp_http_server.h"
// 2 Days worth of samples
#define NUM_SAMPLES_TO_STORE (( 24 * 60 ) / 10 ) * 2
typedef struct {
uint8_t myt_sec; // 0 -> 61
uint8_t myt_min; // 0 -> 59
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;
typedef struct {
deci_percent rh;
deci_degrees_c temp;
my_time time;
} my_dht22_data;
// I know this is asking for a race condition...
static my_dht22_data *dht22_buffer = NULL;
static int dht22_num = 0;
my_time get_time() {
time_t epoch;
time(&epoch);
struct tm *timeinfo = localtime(&epoch);
my_time my_current_time;
// There must be a better way - I couldn't figure out how to do it
// nicely all at once
my_current_time.myt_sec = timeinfo->tm_sec;
// -5 to get the middle of the averaged window
my_current_time.myt_min = timeinfo->tm_min;
my_current_time.myt_hour = timeinfo->tm_hour;
my_current_time.myt_day = timeinfo->tm_mday;
my_current_time.myt_month = timeinfo->tm_mon;
my_current_time.myt_year = timeinfo->tm_year;
return my_current_time;
}
esp_err_t dht22_data_get_handler(httpd_req_t *req) {
char *resp;
size_t size_of_csv_line = strlen("YYYY: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++) {
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,
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);
return ESP_OK;
}
/* URI handler structure for GET /uri */
httpd_uri_t dht22_data_get = {
.uri = "/dht22.csv",
.method = HTTP_GET,
.handler = dht22_data_get_handler,
.user_ctx = NULL
};
void init_dht22_data() {
dht22_buffer = malloc(NUM_SAMPLES_TO_STORE * sizeof(my_dht22_data));
// register an http endpoint thingy probably?
}
void add_dht22_data(float rh, float temp) {
if (dht22_buffer == NULL) {
return;
}
my_dht22_data dht22_data;
dht22_data.time = get_time();
dht22_data.time.myt_min -= 5;
dht22_data.temp = round(temp);
dht22_data.rh = round(rh);
dht22_buffer[dht22_num] = dht22_data;
dht22_num++;
}

View File

@@ -0,0 +1,10 @@
#pragma once
#include "dht22.h"
void init_dht22_data();
// This function presumes we've got a 10 minute average here
// - could think about abstracting the average out into this
// function maybe. Something for the future!
void add_dht22_data(float rh, float temp);