Added latest updates
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user