diff --git a/ESPTest/src/main.c b/ESPTest/src/main.c index c34b11b..72c86d8 100644 --- a/ESPTest/src/main.c +++ b/ESPTest/src/main.c @@ -14,7 +14,6 @@ #include "lwip/sys.h" static const char *WIFI_TAG = "wifi station"; -static const char *SERVER_TAG = "http server"; /* FreeRTOS event group to signal when we are connected*/ static EventGroupHandle_t s_wifi_event_group; @@ -118,80 +117,8 @@ void wifi_init_sta(void) } } -static esp_err_t hello_get_handler(httpd_req_t *req) -{ - /* 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); - return ESP_OK; -} - -static const httpd_uri_t hello = { - .uri = "/hello", - .method = HTTP_GET, - .handler = hello_get_handler, - .user_ctx = "Hello World!"}; - -static httpd_handle_t start_webserver(void) -{ - httpd_handle_t server = NULL; - httpd_config_t config = HTTPD_DEFAULT_CONFIG(); - config.lru_purge_enable = true; - - // Start the httpd server - ESP_LOGI(SERVER_TAG, "Starting server on port: '%d'", config.server_port); - if (httpd_start(&server, &config) == ESP_OK) - { - // Set URI handlers - ESP_LOGI(SERVER_TAG, "Registering URI handlers"); - httpd_register_uri_handler(server, &hello); - return server; - } - - ESP_LOGI(SERVER_TAG, "Error starting server!"); - return NULL; -} - -static esp_err_t stop_webserver(httpd_handle_t server) -{ - // Stop the httpd server - return httpd_stop(server); -} - -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(SERVER_TAG, "Stopping webserver"); - if (stop_webserver(*server) == ESP_OK) - { - *server = NULL; - } - else - { - ESP_LOGE(SERVER_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(SERVER_TAG, "Starting webserver"); - *server = start_webserver(); - } -} - void app_main(void) { - static httpd_handle_t server = NULL; - esp_err_t ret = nvs_flash_init(); if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { @@ -202,8 +129,4 @@ void app_main(void) ESP_LOGI(WIFI_TAG, "ESP_WIFI_MODE_STA"); wifi_init_sta(); - - 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)); - server = start_webserver(); }