1
0
Weather_ESP/components/server/server.c

148 lines
4.7 KiB
C
Raw Normal View History

/*
* server.c
*
* Created on: 21 Apr 2020
* Author: Chris
*/
#include "server.h"
static const char *TAG = "server";
char cur_value_str[255] = "No measurements yet!";
void server_set_values(int32_t temp_raw, uint32_t pressure_raw, uint32_t humidity_raw,
int32_t temp2_raw, uint32_t pressure2_raw, uint32_t humidity2_raw)
{
sprintf(cur_value_str, "<html><body><p>%d %d %d</p><p>%d %d %d</p></body></html>", temp_raw, pressure_raw, humidity_raw, temp2_raw, pressure2_raw, humidity2_raw);
}
static esp_err_t get_handler(httpd_req_t *req)
{
char* buf;
size_t buf_len;
/* Get header value string length and allocate memory for length + 1,
* extra byte for null termination */
buf_len = httpd_req_get_hdr_value_len(req, "Host") + 1;
if (buf_len > 1) {
buf = malloc(buf_len);
/* Copy null terminated value string into buffer */
if (httpd_req_get_hdr_value_str(req, "Host", buf, buf_len) == ESP_OK) {
ESP_LOGI(TAG, "Found header => Host: %s", buf);
}
free(buf);
}
// buf_len = httpd_req_get_hdr_value_len(req, "Test-Header-2") + 1;
// if (buf_len > 1) {
// buf = malloc(buf_len);
// if (httpd_req_get_hdr_value_str(req, "Test-Header-2", buf, buf_len) == ESP_OK) {
// ESP_LOGI(TAG, "Found header => Test-Header-2: %s", buf);
// }
// free(buf);
// }
//
// buf_len = httpd_req_get_hdr_value_len(req, "Test-Header-1") + 1;
// if (buf_len > 1) {
// buf = malloc(buf_len);
// if (httpd_req_get_hdr_value_str(req, "Test-Header-1", buf, buf_len) == ESP_OK) {
// ESP_LOGI(TAG, "Found header => Test-Header-1: %s", buf);
// }
// free(buf);
// }
// /* Read URL query string length and allocate memory for length + 1,
// * extra byte for null termination */
// buf_len = httpd_req_get_url_query_len(req) + 1;
// if (buf_len > 1) {
// buf = malloc(buf_len);
// if (httpd_req_get_url_query_str(req, buf, buf_len) == ESP_OK) {
// ESP_LOGI(TAG, "Found URL query => %s", buf);
// char param[32];
// /* Get value of expected key from query string */
// if (httpd_query_key_value(buf, "query1", param, sizeof(param)) == ESP_OK) {
// ESP_LOGI(TAG, "Found URL query parameter => query1=%s", param);
// }
// if (httpd_query_key_value(buf, "query3", param, sizeof(param)) == ESP_OK) {
// ESP_LOGI(TAG, "Found URL query parameter => query3=%s", param);
// }
// if (httpd_query_key_value(buf, "query2", param, sizeof(param)) == ESP_OK) {
// ESP_LOGI(TAG, "Found URL query parameter => query2=%s", param);
// }
// }
// free(buf);
// }
/* Set some custom headers */
// httpd_resp_set_hdr(req, "Custom-Header-1", "Custom-Value-1");
// httpd_resp_set_hdr(req, "Custom-Header-2", "Custom-Value-2");
/* Send response with custom headers and body set as the
* string passed in user context*/
const char* resp_str = cur_value_str;
httpd_resp_send(req, resp_str, strlen(resp_str));
/* After sending the HTTP response the old HTTP request
* headers are lost. Check if HTTP request headers can be read now. */
// if (httpd_req_get_hdr_value_len(req, "Host") == 0) {
// ESP_LOGI(TAG, "Request headers lost");
// }
return ESP_OK;
}
static httpd_uri_t uri = {
.uri = "/",
.method = HTTP_GET,
.handler = get_handler,
};
static httpd_handle_t start_webserver(void)
{
httpd_handle_t server = NULL;
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
// Start the httpd server
ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
if (httpd_start(&server, &config) == ESP_OK) {
// Set URI handlers
ESP_LOGI(TAG, "Registering URI handlers");
httpd_register_uri_handler(server, &uri);
return server;
}
ESP_LOGI(TAG, "Error starting server!");
return NULL;
}
static void stop_webserver(httpd_handle_t server)
{
// Stop the httpd server
httpd_stop(server);
}
static void connect_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
httpd_handle_t* server = (httpd_handle_t*) arg;
if (*server == NULL) {
ESP_LOGI(TAG, "Starting webserver");
*server = start_webserver();
}
}
static void disconnect_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
httpd_handle_t* server = (httpd_handle_t*) arg;
if (*server) {
ESP_LOGI(TAG, "Stopping webserver");
stop_webserver(*server);
*server = NULL;
}
}
void init_server()
{
start_webserver();
}