1
0
Weather_ESP/components/server/server.c

71 lines
1.8 KiB
C

/*
* 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);
}
const char* resp_str = cur_value_str;
httpd_resp_send(req, resp_str, strlen(resp_str));
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;
}
void init_server()
{
start_webserver();
}