120 lines
3.3 KiB
C
120 lines
3.3 KiB
C
#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"
|
|
|
|
const static char *TAG = "HTTP";
|
|
|
|
// 2 Days worth of samples
|
|
#define NUM_SAMPLES_TO_STORE (((24 * 60) / 10) * 2)
|
|
|
|
httpd_handle_t server = NULL;
|
|
|
|
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) {
|
|
// if (dht22_num == 0) {
|
|
// httpd_resp_send(req, resp, HTTPD_RESP_USE_STRLEN);
|
|
// }
|
|
char *resp;
|
|
|
|
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,
|
|
"%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, dht22_num * size_of_csv_line + 1);
|
|
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));
|
|
httpd_register_uri_handler(server, &dht22_data_get);
|
|
}
|
|
|
|
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 += 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);
|
|
}
|