diff options
| Commit | fd85aa16b32abeae6c9a0524f790fb47dff6ac1c (patch) | |
|---|---|---|
| Author | 2024-05-19 17:53:25 +0300 | |
| Committer | 2024-05-19 17:53:25 +0300 | |
| Files | 7703ed46b0c07623b58c65c2b5b1ceb5c08c8c8b | |
| uni-projects-fd85aa16b32abeae6c9a0524f790fb47dff6ac1c.tar uni-projects-fd85aa16b32abeae6c9a0524f790fb47dff6ac1c.tar.gz uni-projects-fd85aa16b32abeae6c9a0524f790fb47dff6ac1c.zip | ||
| Parent | b7f38d005a130de393e717afae3fa9746e9789b2 (diff) | |
[iot/goam] Added really rough web server config
4 files changed, 187 insertions, 0 deletions
| -rw-r--r-- | Year 3/Internet of things/goam/main/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | Year 3/Internet of things/goam/main/main.c | 3 | ||||
| -rw-r--r-- | Year 3/Internet of things/goam/main/webserver.c | 179 | ||||
| -rw-r--r-- | Year 3/Internet of things/goam/main/webserver.h | 4 |
diff --git a/Year 3/Internet of things/goam/main/CMakeLists.txt b/Year 3/Internet of things/goam/main/CMakeLists.txt index 949428f..09ab23d 100644 --- a/Year 3/Internet of things/goam/main/CMakeLists.txt +++ b/Year 3/Internet of things/goam/main/CMakeLists.txt @@ -3,4 +3,5 @@ idf_component_register(SRCS "main.c" "buttons.c" "display.c" "wifi.c" + "webserver.c" INCLUDE_DIRS "") diff --git a/Year 3/Internet of things/goam/main/main.c b/Year 3/Internet of things/goam/main/main.c index e7603a6..e4c976b 100644 --- a/Year 3/Internet of things/goam/main/main.c +++ b/Year 3/Internet of things/goam/main/main.c @@ -18,6 +18,7 @@ #include "buttons.h" #include "display.h" #include "wifi.h" +#include "webserver.h" void print_chip_data() { /* Print chip information */ @@ -67,6 +68,8 @@ void app_main(void) printf("Starting up!\n"); wifi_init(); + server_init(); + start_server(); ptc06_init(); buttons_init(); diff --git a/Year 3/Internet of things/goam/main/webserver.c b/Year 3/Internet of things/goam/main/webserver.c new file mode 100644 index 0000000..8a633f6 --- /dev/null +++ b/Year 3/Internet of things/goam/main/webserver.c @@ -0,0 +1,179 @@ +#include <string.h> +#include <stdlib.h> +#include <unistd.h> +#include <esp_log.h> +#include <nvs_flash.h> +#include <sys/param.h> +#include "esp_netif.h" +#include "esp_tls_crypto.h" +#include <esp_http_server.h> +#include "esp_event.h" +#include "esp_netif.h" +#include "esp_tls.h" +#include <esp_wifi.h> + +static const char *TAG = "example"; + +#define EXAMPLE_HTTP_QUERY_KEY_MAX_LEN (64) + +static httpd_handle_t server; + +/* An HTTP GET handler */ +static esp_err_t hello_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[EXAMPLE_HTTP_QUERY_KEY_MAX_LEN], dec_param[EXAMPLE_HTTP_QUERY_KEY_MAX_LEN] = {0}; + /* 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); + /* example_uri_decode(dec_param, param, strnlen(param, EXAMPLE_HTTP_QUERY_KEY_MAX_LEN)); */ + /* ESP_LOGI(TAG, "Decoded query parameter => %s", dec_param); */ + } + if (httpd_query_key_value(buf, "query3", param, sizeof(param)) == ESP_OK) { + ESP_LOGI(TAG, "Found URL query parameter => query3=%s", param); + /* example_uri_decode(dec_param, param, strnlen(param, EXAMPLE_HTTP_QUERY_KEY_MAX_LEN)); */ + /* ESP_LOGI(TAG, "Decoded query parameter => %s", dec_param); */ + } + if (httpd_query_key_value(buf, "query2", param, sizeof(param)) == ESP_OK) { + ESP_LOGI(TAG, "Found URL query parameter => query2=%s", param); + /* example_uri_decode(dec_param, param, strnlen(param, EXAMPLE_HTTP_QUERY_KEY_MAX_LEN)); */ + /* ESP_LOGI(TAG, "Decoded query parameter => %s", dec_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 = (const char*) req->user_ctx; + httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN); + + /* 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 const httpd_uri_t hello = { + .uri = "/hello", + .method = HTTP_GET, + .handler = hello_get_handler, + /* Let's pass response string in user + * context to demonstrate it's usage */ + .user_ctx = "Hello World!" +}; + +static httpd_handle_t start_webserver() +{ + httpd_handle_t server = NULL; + httpd_config_t config = HTTPD_DEFAULT_CONFIG(); +#if CONFIG_IDF_TARGET_LINUX + // Setting port as 8001 when building for Linux. Port 80 can be used only by a priviliged user in linux. + // So when a unpriviliged user tries to run the application, it throws bind error and the server is not started. + // Port 8001 can be used by an unpriviliged user as well. So the application will not throw bind error and the + // server will be started. + config.server_port = 8001; +#endif // !CONFIG_IDF_TARGET_LINUX + config.lru_purge_enable = true; + + // 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, &hello); + #if CONFIG_EXAMPLE_BASIC_AUTH + httpd_register_basic_auth(server); + #endif + return server; + } + + ESP_LOGI(TAG, "Error starting server!"); + return NULL; +} + +/* +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"); + if (stop_webserver(*server) == ESP_OK) { + *server = NULL; + } else { + ESP_LOGE(TAG, "Failed to stop http 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(); + } +} + +void server_init() { + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &server)); + /* ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server)); */ +} + + + +void start_server() { + server = start_webserver(); + + while (server) { + sleep(5); + } +} diff --git a/Year 3/Internet of things/goam/main/webserver.h b/Year 3/Internet of things/goam/main/webserver.h new file mode 100644 index 0000000..8d722bf --- /dev/null +++ b/Year 3/Internet of things/goam/main/webserver.h @@ -0,0 +1,4 @@ +#pragma once + +void server_init(); +void start_server(); |
