From c1ed0ae0a6a6075ed98ca2826e66ccb6615b95a6 Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Mon, 25 Jul 2022 09:22:19 +0200 Subject: [PATCH] HTTP Server to test --- ESPTest/.vscode/settings.json | 5 + ESPTest/sdkconfig.az-delivery-devkit-v4 | 41 ++------ ESPTest/sdkconfig.az-delivery-devkit-v4.old | 37 ++----- ESPTest/src/main.c | 106 ++++++++++++++++++-- 4 files changed, 119 insertions(+), 70 deletions(-) create mode 100644 ESPTest/.vscode/settings.json diff --git a/ESPTest/.vscode/settings.json b/ESPTest/.vscode/settings.json new file mode 100644 index 0000000..49c023b --- /dev/null +++ b/ESPTest/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "esp_wifi_default.h": "c" + } +} diff --git a/ESPTest/sdkconfig.az-delivery-devkit-v4 b/ESPTest/sdkconfig.az-delivery-devkit-v4 index f690be4..76b4f9f 100644 --- a/ESPTest/sdkconfig.az-delivery-devkit-v4 +++ b/ESPTest/sdkconfig.az-delivery-devkit-v4 @@ -44,12 +44,12 @@ CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE=y # CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_PERF is not set # CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_NONE is not set # CONFIG_BOOTLOADER_LOG_LEVEL_NONE is not set -# CONFIG_BOOTLOADER_LOG_LEVEL_ERROR is not set -CONFIG_BOOTLOADER_LOG_LEVEL_WARN=y +CONFIG_BOOTLOADER_LOG_LEVEL_ERROR=y +# CONFIG_BOOTLOADER_LOG_LEVEL_WARN is not set # CONFIG_BOOTLOADER_LOG_LEVEL_INFO is not set # CONFIG_BOOTLOADER_LOG_LEVEL_DEBUG is not set # CONFIG_BOOTLOADER_LOG_LEVEL_VERBOSE is not set -CONFIG_BOOTLOADER_LOG_LEVEL=2 +CONFIG_BOOTLOADER_LOG_LEVEL=1 # CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_8V is not set CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_9V=y # CONFIG_BOOTLOADER_FACTORY_RESET is not set @@ -159,12 +159,6 @@ CONFIG_APPTRACE_DEST_NONE=y CONFIG_APPTRACE_LOCK_ENABLE=y # end of Application Level Tracing -# -# ESP-ASIO -# -# CONFIG_ASIO_SSL_SUPPORT is not set -# end of ESP-ASIO - # # Bluetooth # @@ -200,14 +194,7 @@ CONFIG_BT_RESERVE_DRAM=0 CONFIG_BT_NIMBLE_USE_ESP_TIMER=y # end of Bluetooth -# -# CoAP Configuration -# -CONFIG_COAP_MBEDTLS_PSK=y -# CONFIG_COAP_MBEDTLS_PKI is not set -# CONFIG_COAP_MBEDTLS_DEBUG is not set CONFIG_COAP_LOG_DEFAULT_LEVEL=0 -# end of CoAP Configuration # # Driver configurations @@ -669,9 +656,9 @@ CONFIG_LOG_DEFAULT_LEVEL_INFO=y # CONFIG_LOG_DEFAULT_LEVEL_DEBUG is not set # CONFIG_LOG_DEFAULT_LEVEL_VERBOSE is not set CONFIG_LOG_DEFAULT_LEVEL=3 -CONFIG_LOG_COLORS=y -# CONFIG_LOG_TIMESTAMP_SOURCE_RTOS is not set -CONFIG_LOG_TIMESTAMP_SOURCE_SYSTEM=y +# CONFIG_LOG_COLORS is not set +CONFIG_LOG_TIMESTAMP_SOURCE_RTOS=y +# CONFIG_LOG_TIMESTAMP_SOURCE_SYSTEM is not set # end of Log output # @@ -690,9 +677,7 @@ CONFIG_LWIP_SO_REUSE_RXTOALL=y # CONFIG_LWIP_SO_RCVBUF is not set # CONFIG_LWIP_NETBUF_RECVINFO is not set CONFIG_LWIP_IP4_FRAG=y -CONFIG_LWIP_IP6_FRAG=y # CONFIG_LWIP_IP4_REASSEMBLY is not set -# CONFIG_LWIP_IP6_REASSEMBLY is not set # CONFIG_LWIP_IP_FORWARD is not set # CONFIG_LWIP_STATS is not set # CONFIG_LWIP_ETHARP_TRUST_IP_MAC is not set @@ -712,8 +697,7 @@ CONFIG_LWIP_DHCPS_MAX_STATION_NUM=8 # end of DHCP server # CONFIG_LWIP_AUTOIP is not set -CONFIG_LWIP_IPV6=y -# CONFIG_LWIP_IPV6_AUTOCONFIG is not set +# CONFIG_LWIP_IPV6 is not set CONFIG_LWIP_NETIF_LOOPBACK=y CONFIG_LWIP_LOOPBACK_MAX_PBUFS=8 @@ -761,8 +745,6 @@ CONFIG_LWIP_TCPIP_TASK_AFFINITY_NO_AFFINITY=y # CONFIG_LWIP_TCPIP_TASK_AFFINITY_CPU1 is not set CONFIG_LWIP_TCPIP_TASK_AFFINITY=0x7FFFFFFF # CONFIG_LWIP_PPP_SUPPORT is not set -CONFIG_LWIP_IPV6_MEMP_NUM_ND6_QUEUE=3 -CONFIG_LWIP_IPV6_ND6_NUM_NEIGHBORS=5 # CONFIG_LWIP_SLIP_SUPPORT is not set # @@ -794,9 +776,6 @@ CONFIG_LWIP_ESP_LWIP_ASSERT=y # CONFIG_LWIP_HOOK_TCP_ISN_NONE is not set CONFIG_LWIP_HOOK_TCP_ISN_DEFAULT=y # CONFIG_LWIP_HOOK_TCP_ISN_CUSTOM is not set -CONFIG_LWIP_HOOK_IP6_ROUTE_NONE=y -# CONFIG_LWIP_HOOK_IP6_ROUTE_DEFAULT is not set -# CONFIG_LWIP_HOOK_IP6_ROUTE_CUSTOM is not set CONFIG_LWIP_HOOK_NETCONN_EXT_RESOLVE_NONE=y # CONFIG_LWIP_HOOK_NETCONN_EXT_RESOLVE_DEFAULT is not set # CONFIG_LWIP_HOOK_NETCONN_EXT_RESOLVE_CUSTOM is not set @@ -1141,12 +1120,12 @@ CONFIG_WPA_MBEDTLS_CRYPTO=y # Deprecated options for backward compatibility CONFIG_TOOLPREFIX="xtensa-esp32-elf-" # CONFIG_LOG_BOOTLOADER_LEVEL_NONE is not set -# CONFIG_LOG_BOOTLOADER_LEVEL_ERROR is not set -CONFIG_LOG_BOOTLOADER_LEVEL_WARN=y +CONFIG_LOG_BOOTLOADER_LEVEL_ERROR=y +# CONFIG_LOG_BOOTLOADER_LEVEL_WARN is not set # CONFIG_LOG_BOOTLOADER_LEVEL_INFO is not set # CONFIG_LOG_BOOTLOADER_LEVEL_DEBUG is not set # CONFIG_LOG_BOOTLOADER_LEVEL_VERBOSE is not set -CONFIG_LOG_BOOTLOADER_LEVEL=2 +CONFIG_LOG_BOOTLOADER_LEVEL=1 # CONFIG_APP_ROLLBACK_ENABLE is not set # CONFIG_FLASH_ENCRYPTION_ENABLED is not set # CONFIG_FLASHMODE_QIO is not set diff --git a/ESPTest/sdkconfig.az-delivery-devkit-v4.old b/ESPTest/sdkconfig.az-delivery-devkit-v4.old index 3d8d9b2..21b8cae 100644 --- a/ESPTest/sdkconfig.az-delivery-devkit-v4.old +++ b/ESPTest/sdkconfig.az-delivery-devkit-v4.old @@ -44,12 +44,12 @@ CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE=y # CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_PERF is not set # CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_NONE is not set # CONFIG_BOOTLOADER_LOG_LEVEL_NONE is not set -# CONFIG_BOOTLOADER_LOG_LEVEL_ERROR is not set -CONFIG_BOOTLOADER_LOG_LEVEL_WARN=y +CONFIG_BOOTLOADER_LOG_LEVEL_ERROR=y +# CONFIG_BOOTLOADER_LOG_LEVEL_WARN is not set # CONFIG_BOOTLOADER_LOG_LEVEL_INFO is not set # CONFIG_BOOTLOADER_LOG_LEVEL_DEBUG is not set # CONFIG_BOOTLOADER_LOG_LEVEL_VERBOSE is not set -CONFIG_BOOTLOADER_LOG_LEVEL=2 +CONFIG_BOOTLOADER_LOG_LEVEL=1 # CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_8V is not set CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_9V=y # CONFIG_BOOTLOADER_FACTORY_RESET is not set @@ -159,12 +159,6 @@ CONFIG_APPTRACE_DEST_NONE=y CONFIG_APPTRACE_LOCK_ENABLE=y # end of Application Level Tracing -# -# ESP-ASIO -# -# CONFIG_ASIO_SSL_SUPPORT is not set -# end of ESP-ASIO - # # Bluetooth # @@ -200,14 +194,7 @@ CONFIG_BT_RESERVE_DRAM=0 CONFIG_BT_NIMBLE_USE_ESP_TIMER=y # end of Bluetooth -# -# CoAP Configuration -# -CONFIG_COAP_MBEDTLS_PSK=y -# CONFIG_COAP_MBEDTLS_PKI is not set -# CONFIG_COAP_MBEDTLS_DEBUG is not set CONFIG_COAP_LOG_DEFAULT_LEVEL=0 -# end of CoAP Configuration # # Driver configurations @@ -664,12 +651,12 @@ CONFIG_HEAP_TRACING_OFF=y # # CONFIG_LOG_DEFAULT_LEVEL_NONE is not set # CONFIG_LOG_DEFAULT_LEVEL_ERROR is not set -# CONFIG_LOG_DEFAULT_LEVEL_WARN is not set +CONFIG_LOG_DEFAULT_LEVEL_WARN=y # CONFIG_LOG_DEFAULT_LEVEL_INFO is not set # CONFIG_LOG_DEFAULT_LEVEL_DEBUG is not set -CONFIG_LOG_DEFAULT_LEVEL_VERBOSE=y -CONFIG_LOG_DEFAULT_LEVEL=5 -CONFIG_LOG_COLORS=y +# CONFIG_LOG_DEFAULT_LEVEL_VERBOSE is not set +CONFIG_LOG_DEFAULT_LEVEL=2 +# CONFIG_LOG_COLORS is not set # CONFIG_LOG_TIMESTAMP_SOURCE_RTOS is not set CONFIG_LOG_TIMESTAMP_SOURCE_SYSTEM=y # end of Log output @@ -690,9 +677,7 @@ CONFIG_LWIP_SO_REUSE_RXTOALL=y # CONFIG_LWIP_SO_RCVBUF is not set # CONFIG_LWIP_NETBUF_RECVINFO is not set CONFIG_LWIP_IP4_FRAG=y -CONFIG_LWIP_IP6_FRAG=y # CONFIG_LWIP_IP4_REASSEMBLY is not set -# CONFIG_LWIP_IP6_REASSEMBLY is not set # CONFIG_LWIP_IP_FORWARD is not set # CONFIG_LWIP_STATS is not set # CONFIG_LWIP_ETHARP_TRUST_IP_MAC is not set @@ -712,8 +697,7 @@ CONFIG_LWIP_DHCPS_MAX_STATION_NUM=8 # end of DHCP server # CONFIG_LWIP_AUTOIP is not set -CONFIG_LWIP_IPV6=y -# CONFIG_LWIP_IPV6_AUTOCONFIG is not set +# CONFIG_LWIP_IPV6 is not set CONFIG_LWIP_NETIF_LOOPBACK=y CONFIG_LWIP_LOOPBACK_MAX_PBUFS=8 @@ -761,8 +745,6 @@ CONFIG_LWIP_TCPIP_TASK_AFFINITY_NO_AFFINITY=y # CONFIG_LWIP_TCPIP_TASK_AFFINITY_CPU1 is not set CONFIG_LWIP_TCPIP_TASK_AFFINITY=0x7FFFFFFF # CONFIG_LWIP_PPP_SUPPORT is not set -CONFIG_LWIP_IPV6_MEMP_NUM_ND6_QUEUE=3 -CONFIG_LWIP_IPV6_ND6_NUM_NEIGHBORS=5 # CONFIG_LWIP_SLIP_SUPPORT is not set # @@ -794,9 +776,6 @@ CONFIG_LWIP_ESP_LWIP_ASSERT=y # CONFIG_LWIP_HOOK_TCP_ISN_NONE is not set CONFIG_LWIP_HOOK_TCP_ISN_DEFAULT=y # CONFIG_LWIP_HOOK_TCP_ISN_CUSTOM is not set -CONFIG_LWIP_HOOK_IP6_ROUTE_NONE=y -# CONFIG_LWIP_HOOK_IP6_ROUTE_DEFAULT is not set -# CONFIG_LWIP_HOOK_IP6_ROUTE_CUSTOM is not set CONFIG_LWIP_HOOK_NETCONN_EXT_RESOLVE_NONE=y # CONFIG_LWIP_HOOK_NETCONN_EXT_RESOLVE_DEFAULT is not set # CONFIG_LWIP_HOOK_NETCONN_EXT_RESOLVE_CUSTOM is not set diff --git a/ESPTest/src/main.c b/ESPTest/src/main.c index 0142b88..3b458b7 100644 --- a/ESPTest/src/main.c +++ b/ESPTest/src/main.c @@ -8,6 +8,7 @@ #include "esp_event.h" #include "esp_log.h" #include "nvs_flash.h" +#include #include "lwip/err.h" #include "lwip/sys.h" @@ -21,7 +22,8 @@ static EventGroupHandle_t s_wifi_event_group; #define WIFI_CONNECTED_BIT BIT0 #define WIFI_FAIL_BIT BIT1 -static const char *TAG = "wifi station"; +static const char *WIFI_TAG = "wifi station"; +static const char *SERVER_TAG = "http server"; static int s_retry_num = 0; @@ -38,18 +40,18 @@ static void event_handler(void *arg, esp_event_base_t event_base, { esp_wifi_connect(); s_retry_num++; - ESP_LOGI(TAG, "retry to connect to the AP"); + ESP_LOGI(WIFI_TAG, "retry to connect to the AP"); } else { xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT); } - ESP_LOGI(TAG, "connect to the AP fail"); + ESP_LOGI(WIFI_TAG, "connect to the AP fail"); } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; - ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); + ESP_LOGI(WIFI_TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); s_retry_num = 0; xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); } @@ -90,7 +92,7 @@ void wifi_init_sta(void) ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config)); ESP_ERROR_CHECK(esp_wifi_start()); - ESP_LOGI(TAG, "wifi_init_sta finished."); + ESP_LOGI(WIFI_TAG, "wifi_init_sta finished."); /* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum * number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */ @@ -104,20 +106,100 @@ void wifi_init_sta(void) * happened. */ if (bits & WIFI_CONNECTED_BIT) { - ESP_LOGI(TAG, "connected to ap"); + ESP_LOGI(WIFI_TAG, "connected to ap"); } else if (bits & WIFI_FAIL_BIT) { - ESP_LOGI(TAG, "Failed to connect"); + ESP_LOGI(WIFI_TAG, "Failed to connect"); } else { - ESP_LOGE(TAG, "UNEXPECTED EVENT"); + ESP_LOGE(WIFI_TAG, "UNEXPECTED EVENT"); + } +} + +int my_hook(struct pbuf *pbuf, struct netif *input_netif) +{ + ESP_LOGI(WIFI_TAG, "TESTING!"); + return ESP_OK; +} + +#define LWIP_HOOK_IP4_INPUT(pbuf, input_netif) my_hook(pbuf, input_netif) + +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) { @@ -126,6 +208,10 @@ void app_main(void) } ESP_ERROR_CHECK(ret); - ESP_LOGI(TAG, "ESP_WIFI_MODE_STA"); + ESP_LOGI(WIFI_TAG, "ESP_WIFI_MODE_STA"); wifi_init_sta(); -} \ No newline at end of file + + 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(); +}