mirror of
https://github.com/espressif/esp-idf.git
synced 2025-07-31 19:24:33 +02:00
examples/protocols/http(s)_server: use common network component
This commit is contained in:
@@ -2,7 +2,9 @@
|
|||||||
# in this exact order for cmake to work correctly
|
# in this exact order for cmake to work correctly
|
||||||
cmake_minimum_required(VERSION 3.5)
|
cmake_minimum_required(VERSION 3.5)
|
||||||
|
|
||||||
|
# (Not part of the boilerplate)
|
||||||
|
# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection.
|
||||||
|
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common)
|
||||||
|
|
||||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||||
project(tests)
|
project(tests)
|
||||||
|
|
||||||
target_include_directories(tests.elf PRIVATE main/include)
|
|
||||||
|
@@ -5,5 +5,7 @@
|
|||||||
|
|
||||||
PROJECT_NAME := tests
|
PROJECT_NAME := tests
|
||||||
|
|
||||||
|
EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common
|
||||||
|
|
||||||
include $(IDF_PATH)/make/project.mk
|
include $(IDF_PATH)/make/project.mk
|
||||||
|
|
||||||
|
@@ -66,7 +66,7 @@ def test_examples_protocol_http_server_advanced(env, extra_data):
|
|||||||
|
|
||||||
# Parse IP address of STA
|
# Parse IP address of STA
|
||||||
Utility.console_log("Waiting to connect with AP")
|
Utility.console_log("Waiting to connect with AP")
|
||||||
got_ip = dut1.expect(re.compile(r"(?:[\s\S]*)Got IP: '(\d+.\d+.\d+.\d+)'"), timeout=30)[0]
|
got_ip = dut1.expect(re.compile(r"(?:[\s\S]*)IPv4 address: (\d+.\d+.\d+.\d+)"), timeout=30)[0]
|
||||||
|
|
||||||
got_port = dut1.expect(re.compile(r"(?:[\s\S]*)Started HTTP server on port: '(\d+)'"), timeout=15)[0]
|
got_port = dut1.expect(re.compile(r"(?:[\s\S]*)Started HTTP server on port: '(\d+)'"), timeout=15)[0]
|
||||||
result = dut1.expect(re.compile(r"(?:[\s\S]*)Max URI handlers: '(\d+)'(?:[\s\S]*)Max Open Sessions: " # noqa: W605
|
result = dut1.expect(re.compile(r"(?:[\s\S]*)Max URI handlers: '(\d+)'(?:[\s\S]*)Max Open Sessions: " # noqa: W605
|
||||||
|
@@ -1,16 +0,0 @@
|
|||||||
menu "Example Configuration"
|
|
||||||
|
|
||||||
config WIFI_SSID
|
|
||||||
string "WiFi SSID"
|
|
||||||
default "myssid"
|
|
||||||
help
|
|
||||||
SSID (network name) for the example to connect to.
|
|
||||||
|
|
||||||
config WIFI_PASSWORD
|
|
||||||
string "WiFi Password"
|
|
||||||
default "mypassword"
|
|
||||||
help
|
|
||||||
WiFi password (WPA or WPA2) for the example to use.
|
|
||||||
Can be left blank if the network has no security set.
|
|
||||||
|
|
||||||
endmenu
|
|
@@ -1,81 +1,72 @@
|
|||||||
|
/* HTTP Server Tests
|
||||||
|
|
||||||
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, this
|
||||||
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||||
|
CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
*/
|
||||||
|
|
||||||
#include "esp_wifi.h"
|
#include "esp_wifi.h"
|
||||||
#include "esp_event_loop.h"
|
#include "esp_event_loop.h"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
#include "esp_system.h"
|
#include "esp_system.h"
|
||||||
#include "nvs_flash.h"
|
#include "nvs_flash.h"
|
||||||
|
#include "tcpip_adapter.h"
|
||||||
|
#include "esp_eth.h"
|
||||||
|
#include "protocol_examples_common.h"
|
||||||
|
|
||||||
#include "tests.h"
|
#include "tests.h"
|
||||||
|
|
||||||
/* The examples use simple WiFi configuration that you can set via
|
static const char *TAG = "example";
|
||||||
'make menuconfig'.
|
|
||||||
|
|
||||||
If you'd rather not, just change the below entries to strings with
|
static void disconnect_handler(void* arg, esp_event_base_t event_base,
|
||||||
the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
|
int32_t event_id, void* event_data)
|
||||||
*/
|
|
||||||
#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID
|
|
||||||
#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD
|
|
||||||
|
|
||||||
static const char *TAG="TEST_WIFI";
|
|
||||||
|
|
||||||
static esp_err_t event_handler(void *ctx, system_event_t *event)
|
|
||||||
{
|
{
|
||||||
httpd_handle_t *hd = (httpd_handle_t *) ctx;
|
httpd_handle_t* server = (httpd_handle_t*) arg;
|
||||||
|
if (*server) {
|
||||||
switch(event->event_id) {
|
ESP_LOGI(TAG, "Stopping webserver");
|
||||||
case SYSTEM_EVENT_STA_START:
|
stop_tests(*server);
|
||||||
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START");
|
*server = NULL;
|
||||||
ESP_ERROR_CHECK(esp_wifi_connect());
|
|
||||||
break;
|
|
||||||
case SYSTEM_EVENT_STA_GOT_IP:
|
|
||||||
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP");
|
|
||||||
ESP_LOGI(TAG, "Got IP: '%s'",
|
|
||||||
ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
|
|
||||||
|
|
||||||
// Start webserver tests
|
|
||||||
if (*hd == NULL) {
|
|
||||||
*hd = start_tests();
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
case SYSTEM_EVENT_STA_DISCONNECTED:
|
|
||||||
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED");
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_connect());
|
|
||||||
|
|
||||||
// Stop webserver tests
|
|
||||||
if (*hd) {
|
|
||||||
stop_tests(*hd);
|
|
||||||
*hd = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
return ESP_OK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void initialise_wifi(void)
|
static void connect_handler(void* arg, esp_event_base_t event_base,
|
||||||
|
int32_t event_id, void* event_data)
|
||||||
{
|
{
|
||||||
tcpip_adapter_init();
|
httpd_handle_t* server = (httpd_handle_t*) arg;
|
||||||
static httpd_handle_t hd = NULL;
|
if (*server == NULL) {
|
||||||
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, &hd));
|
ESP_LOGI(TAG, "Starting webserver");
|
||||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
*server = start_tests();
|
||||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
}
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
|
|
||||||
wifi_config_t wifi_config = {
|
|
||||||
.sta = {
|
|
||||||
.ssid = EXAMPLE_WIFI_SSID,
|
|
||||||
.password = EXAMPLE_WIFI_PASS,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_start());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void app_main()
|
void app_main()
|
||||||
{
|
{
|
||||||
|
static httpd_handle_t server = NULL;
|
||||||
|
|
||||||
ESP_ERROR_CHECK(nvs_flash_init());
|
ESP_ERROR_CHECK(nvs_flash_init());
|
||||||
initialise_wifi();
|
tcpip_adapter_init();
|
||||||
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||||
|
|
||||||
|
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
|
||||||
|
* Read "Establishing Wi-Fi or Ethernet Connection" section in
|
||||||
|
* examples/protocols/README.md for more information about this function.
|
||||||
|
*/
|
||||||
|
ESP_ERROR_CHECK(example_connect());
|
||||||
|
|
||||||
|
/* Register event handlers to stop the server when Wi-Fi or Ethernet is disconnected,
|
||||||
|
* and re-start it upon connection.
|
||||||
|
*/
|
||||||
|
#ifdef CONFIG_EXAMPLE_CONNECT_WIFI
|
||||||
|
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));
|
||||||
|
#endif // CONFIG_EXAMPLE_CONNECT_WIFI
|
||||||
|
#ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET
|
||||||
|
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &connect_handler, &server));
|
||||||
|
ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, &disconnect_handler, &server));
|
||||||
|
#endif // CONFIG_EXAMPLE_CONNECT_ETHERNET
|
||||||
|
|
||||||
|
/* Start the server for the first time */
|
||||||
|
server = start_tests();
|
||||||
}
|
}
|
||||||
|
@@ -7,10 +7,9 @@
|
|||||||
|
|
||||||
#include "tests.h"
|
#include "tests.h"
|
||||||
|
|
||||||
static const char *TAG="TESTS";
|
static const char *TAG = "TESTS";
|
||||||
|
|
||||||
int pre_start_mem, post_stop_mem, post_stop_min_mem;
|
static int pre_start_mem, post_stop_mem;
|
||||||
bool basic_sanity = true;
|
|
||||||
|
|
||||||
struct async_resp_arg {
|
struct async_resp_arg {
|
||||||
httpd_handle_t hd;
|
httpd_handle_t hd;
|
||||||
@@ -19,7 +18,7 @@ struct async_resp_arg {
|
|||||||
|
|
||||||
/********************* Basic Handlers Start *******************/
|
/********************* Basic Handlers Start *******************/
|
||||||
|
|
||||||
esp_err_t hello_get_handler(httpd_req_t *req)
|
static esp_err_t hello_get_handler(httpd_req_t *req)
|
||||||
{
|
{
|
||||||
#define STR "Hello World!"
|
#define STR "Hello World!"
|
||||||
ESP_LOGI(TAG, "Free Stack for server task: '%d'", uxTaskGetStackHighWaterMark(NULL));
|
ESP_LOGI(TAG, "Free Stack for server task: '%d'", uxTaskGetStackHighWaterMark(NULL));
|
||||||
@@ -28,7 +27,7 @@ esp_err_t hello_get_handler(httpd_req_t *req)
|
|||||||
#undef STR
|
#undef STR
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t hello_type_get_handler(httpd_req_t *req)
|
static esp_err_t hello_type_get_handler(httpd_req_t *req)
|
||||||
{
|
{
|
||||||
#define STR "Hello World!"
|
#define STR "Hello World!"
|
||||||
httpd_resp_set_type(req, HTTPD_TYPE_TEXT);
|
httpd_resp_set_type(req, HTTPD_TYPE_TEXT);
|
||||||
@@ -37,7 +36,7 @@ esp_err_t hello_type_get_handler(httpd_req_t *req)
|
|||||||
#undef STR
|
#undef STR
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t hello_status_get_handler(httpd_req_t *req)
|
static esp_err_t hello_status_get_handler(httpd_req_t *req)
|
||||||
{
|
{
|
||||||
#define STR "Hello World!"
|
#define STR "Hello World!"
|
||||||
httpd_resp_set_status(req, HTTPD_500);
|
httpd_resp_set_status(req, HTTPD_500);
|
||||||
@@ -46,7 +45,7 @@ esp_err_t hello_status_get_handler(httpd_req_t *req)
|
|||||||
#undef STR
|
#undef STR
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t echo_post_handler(httpd_req_t *req)
|
static esp_err_t echo_post_handler(httpd_req_t *req)
|
||||||
{
|
{
|
||||||
ESP_LOGI(TAG, "/echo handler read content length %d", req->content_len);
|
ESP_LOGI(TAG, "/echo handler read content length %d", req->content_len);
|
||||||
|
|
||||||
@@ -101,7 +100,7 @@ esp_err_t echo_post_handler(httpd_req_t *req)
|
|||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
void adder_free_func(void *ctx)
|
static void adder_free_func(void *ctx)
|
||||||
{
|
{
|
||||||
ESP_LOGI(TAG, "Custom Free Context function called");
|
ESP_LOGI(TAG, "Custom Free Context function called");
|
||||||
free(ctx);
|
free(ctx);
|
||||||
@@ -110,7 +109,7 @@ void adder_free_func(void *ctx)
|
|||||||
/* Create a context, keep incrementing value in the context, by whatever was
|
/* Create a context, keep incrementing value in the context, by whatever was
|
||||||
* received. Return the result
|
* received. Return the result
|
||||||
*/
|
*/
|
||||||
esp_err_t adder_post_handler(httpd_req_t *req)
|
static esp_err_t adder_post_handler(httpd_req_t *req)
|
||||||
{
|
{
|
||||||
char buf[10];
|
char buf[10];
|
||||||
char outbuf[50];
|
char outbuf[50];
|
||||||
@@ -143,7 +142,7 @@ esp_err_t adder_post_handler(httpd_req_t *req)
|
|||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t leftover_data_post_handler(httpd_req_t *req)
|
static esp_err_t leftover_data_post_handler(httpd_req_t *req)
|
||||||
{
|
{
|
||||||
/* Only echo the first 10 bytes of the request, leaving the rest of the
|
/* Only echo the first 10 bytes of the request, leaving the rest of the
|
||||||
* request data as is.
|
* request data as is.
|
||||||
@@ -166,8 +165,9 @@ esp_err_t leftover_data_post_handler(httpd_req_t *req)
|
|||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
int httpd_default_send(httpd_handle_t hd, int sockfd, const char *buf, unsigned buf_len, int flags);
|
extern int httpd_default_send(httpd_handle_t hd, int sockfd, const char *buf, unsigned buf_len, int flags);
|
||||||
void generate_async_resp(void *arg)
|
|
||||||
|
static void generate_async_resp(void *arg)
|
||||||
{
|
{
|
||||||
char buf[250];
|
char buf[250];
|
||||||
struct async_resp_arg *resp_arg = (struct async_resp_arg *)arg;
|
struct async_resp_arg *resp_arg = (struct async_resp_arg *)arg;
|
||||||
@@ -190,7 +190,7 @@ void generate_async_resp(void *arg)
|
|||||||
free(arg);
|
free(arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t async_get_handler(httpd_req_t *req)
|
static esp_err_t async_get_handler(httpd_req_t *req)
|
||||||
{
|
{
|
||||||
#define STR "Hello World!"
|
#define STR "Hello World!"
|
||||||
httpd_resp_send(req, STR, strlen(STR));
|
httpd_resp_send(req, STR, strlen(STR));
|
||||||
@@ -211,7 +211,7 @@ esp_err_t async_get_handler(httpd_req_t *req)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
httpd_uri_t basic_handlers[] = {
|
static const httpd_uri_t basic_handlers[] = {
|
||||||
{ .uri = "/hello/type_html",
|
{ .uri = "/hello/type_html",
|
||||||
.method = HTTP_GET,
|
.method = HTTP_GET,
|
||||||
.handler = hello_type_get_handler,
|
.handler = hello_type_get_handler,
|
||||||
@@ -254,8 +254,9 @@ httpd_uri_t basic_handlers[] = {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
int basic_handlers_no = sizeof(basic_handlers)/sizeof(httpd_uri_t);
|
static const int basic_handlers_no = sizeof(basic_handlers)/sizeof(httpd_uri_t);
|
||||||
void register_basic_handlers(httpd_handle_t hd)
|
|
||||||
|
static void register_basic_handlers(httpd_handle_t hd)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
ESP_LOGI(TAG, "Registering basic handlers");
|
ESP_LOGI(TAG, "Registering basic handlers");
|
||||||
@@ -269,7 +270,7 @@ void register_basic_handlers(httpd_handle_t hd)
|
|||||||
ESP_LOGI(TAG, "Success");
|
ESP_LOGI(TAG, "Success");
|
||||||
}
|
}
|
||||||
|
|
||||||
httpd_handle_t test_httpd_start()
|
static httpd_handle_t test_httpd_start()
|
||||||
{
|
{
|
||||||
pre_start_mem = esp_get_free_heap_size();
|
pre_start_mem = esp_get_free_heap_size();
|
||||||
httpd_handle_t hd;
|
httpd_handle_t hd;
|
||||||
@@ -291,7 +292,7 @@ httpd_handle_t test_httpd_start()
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_httpd_stop(httpd_handle_t hd)
|
static void test_httpd_stop(httpd_handle_t hd)
|
||||||
{
|
{
|
||||||
httpd_stop(hd);
|
httpd_stop(hd);
|
||||||
post_stop_mem = esp_get_free_heap_size();
|
post_stop_mem = esp_get_free_heap_size();
|
||||||
|
@@ -2,5 +2,9 @@
|
|||||||
# in this exact order for cmake to work correctly
|
# in this exact order for cmake to work correctly
|
||||||
cmake_minimum_required(VERSION 3.5)
|
cmake_minimum_required(VERSION 3.5)
|
||||||
|
|
||||||
|
# (Not part of the boilerplate)
|
||||||
|
# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection.
|
||||||
|
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common)
|
||||||
|
|
||||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||||
project(file_server)
|
project(file_server)
|
||||||
|
@@ -5,5 +5,7 @@
|
|||||||
|
|
||||||
PROJECT_NAME := file_server
|
PROJECT_NAME := file_server
|
||||||
|
|
||||||
|
EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common
|
||||||
|
|
||||||
include $(IDF_PATH)/make/project.mk
|
include $(IDF_PATH)/make/project.mk
|
||||||
|
|
||||||
|
@@ -1,16 +0,0 @@
|
|||||||
menu "Example Configuration"
|
|
||||||
|
|
||||||
config WIFI_SSID
|
|
||||||
string "WiFi SSID"
|
|
||||||
default "myssid"
|
|
||||||
help
|
|
||||||
SSID (network name) for the example to connect to.
|
|
||||||
|
|
||||||
config WIFI_PASSWORD
|
|
||||||
string "WiFi Password"
|
|
||||||
default "mypassword"
|
|
||||||
help
|
|
||||||
WiFi password (WPA or WPA2) for the example to use.
|
|
||||||
Can be left blank if the network has no security set.
|
|
||||||
|
|
||||||
endmenu
|
|
@@ -15,66 +15,15 @@
|
|||||||
#include "esp_system.h"
|
#include "esp_system.h"
|
||||||
#include "esp_spiffs.h"
|
#include "esp_spiffs.h"
|
||||||
#include "nvs_flash.h"
|
#include "nvs_flash.h"
|
||||||
|
#include "tcpip_adapter.h"
|
||||||
|
#include "protocol_examples_common.h"
|
||||||
|
|
||||||
/* This example demonstrates how to create file server
|
/* This example demonstrates how to create file server
|
||||||
* using esp_http_server. This file has only startup code.
|
* using esp_http_server. This file has only startup code.
|
||||||
* Look in file_server.c for the implementation */
|
* Look in file_server.c for the implementation */
|
||||||
|
|
||||||
/* The example uses simple WiFi configuration that you can set via
|
|
||||||
* 'make menuconfig'.
|
|
||||||
* If you'd rather not, just change the below entries to strings
|
|
||||||
* with the config you want -
|
|
||||||
* ie. #define EXAMPLE_WIFI_SSID "mywifissid"
|
|
||||||
*/
|
|
||||||
#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID
|
|
||||||
#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD
|
|
||||||
|
|
||||||
static const char *TAG="example";
|
static const char *TAG="example";
|
||||||
|
|
||||||
/* Wi-Fi event handler */
|
|
||||||
static esp_err_t event_handler(void *ctx, system_event_t *event)
|
|
||||||
{
|
|
||||||
switch(event->event_id) {
|
|
||||||
case SYSTEM_EVENT_STA_START:
|
|
||||||
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START");
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_connect());
|
|
||||||
break;
|
|
||||||
case SYSTEM_EVENT_STA_GOT_IP:
|
|
||||||
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP");
|
|
||||||
ESP_LOGI(TAG, "Got IP: '%s'",
|
|
||||||
ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
|
|
||||||
break;
|
|
||||||
case SYSTEM_EVENT_STA_DISCONNECTED:
|
|
||||||
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED");
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_connect());
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return ESP_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Function to initialize Wi-Fi at station */
|
|
||||||
static void initialise_wifi(void)
|
|
||||||
{
|
|
||||||
ESP_ERROR_CHECK(nvs_flash_init());
|
|
||||||
tcpip_adapter_init();
|
|
||||||
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
|
|
||||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
|
|
||||||
wifi_config_t wifi_config = {
|
|
||||||
.sta = {
|
|
||||||
.ssid = EXAMPLE_WIFI_SSID,
|
|
||||||
.password = EXAMPLE_WIFI_PASS,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_start());
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Function to initialize SPIFFS */
|
/* Function to initialize SPIFFS */
|
||||||
static esp_err_t init_spiffs(void)
|
static esp_err_t init_spiffs(void)
|
||||||
{
|
{
|
||||||
@@ -117,7 +66,15 @@ esp_err_t start_file_server(const char *base_path);
|
|||||||
|
|
||||||
void app_main()
|
void app_main()
|
||||||
{
|
{
|
||||||
initialise_wifi();
|
ESP_ERROR_CHECK(nvs_flash_init());
|
||||||
|
tcpip_adapter_init();
|
||||||
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||||
|
|
||||||
|
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
|
||||||
|
* Read "Establishing Wi-Fi or Ethernet Connection" section in
|
||||||
|
* examples/protocols/README.md for more information about this function.
|
||||||
|
*/
|
||||||
|
ESP_ERROR_CHECK(example_connect());
|
||||||
|
|
||||||
/* Initialize file storage */
|
/* Initialize file storage */
|
||||||
ESP_ERROR_CHECK(init_spiffs());
|
ESP_ERROR_CHECK(init_spiffs());
|
||||||
|
@@ -2,6 +2,10 @@
|
|||||||
# in this exact order for cmake to work correctly
|
# in this exact order for cmake to work correctly
|
||||||
cmake_minimum_required(VERSION 3.5)
|
cmake_minimum_required(VERSION 3.5)
|
||||||
|
|
||||||
|
# (Not part of the boilerplate)
|
||||||
|
# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection.
|
||||||
|
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common)
|
||||||
|
|
||||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||||
project(persistent_sockets)
|
project(persistent_sockets)
|
||||||
|
|
||||||
|
@@ -5,5 +5,7 @@
|
|||||||
|
|
||||||
PROJECT_NAME := persistent_sockets
|
PROJECT_NAME := persistent_sockets
|
||||||
|
|
||||||
|
EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common
|
||||||
|
|
||||||
include $(IDF_PATH)/make/project.mk
|
include $(IDF_PATH)/make/project.mk
|
||||||
|
|
||||||
|
@@ -1,12 +1,9 @@
|
|||||||
# HTTPD Server Persistant Sockets Example
|
# HTTPD Server Persistent Sockets Example
|
||||||
|
|
||||||
The Example consists of HTTPD server persistent sockets demo.
|
The Example consists of HTTPD server persistent sockets demo.
|
||||||
This sort of persistancy enables the server to have independent sessions/contexts per client.
|
This sort of persistency enables the server to have independent sessions/contexts per client.
|
||||||
|
|
||||||
* Configure the project using "make menuconfig" and goto :
|
* Run `make menuconfig` (or `idf.py menuconfig` if using CMake build system) to configure Wi-Fi or Ethernet. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details.
|
||||||
* Example Configuration ->
|
|
||||||
1. WIFI SSID: WIFI network to which your PC is also connected to.
|
|
||||||
2. WIFI Password: WIFI password
|
|
||||||
|
|
||||||
* In order to test the HTTPD server persistent sockets demo :
|
* In order to test the HTTPD server persistent sockets demo :
|
||||||
1. compile and burn the firmware "make flash"
|
1. compile and burn the firmware "make flash"
|
||||||
|
@@ -62,7 +62,7 @@ def test_examples_protocol_http_server_persistence(env, extra_data):
|
|||||||
|
|
||||||
# Parse IP address of STA
|
# Parse IP address of STA
|
||||||
Utility.console_log("Waiting to connect with AP")
|
Utility.console_log("Waiting to connect with AP")
|
||||||
got_ip = dut1.expect(re.compile(r"(?:[\s\S]*)Got IP: '(\d+.\d+.\d+.\d+)'"), timeout=120)[0]
|
got_ip = dut1.expect(re.compile(r"(?:[\s\S]*)IPv4 address: (\d+.\d+.\d+.\d+)"), timeout=30)[0]
|
||||||
got_port = dut1.expect(re.compile(r"(?:[\s\S]*)Starting server on port: '(\d+)'"), timeout=30)[0]
|
got_port = dut1.expect(re.compile(r"(?:[\s\S]*)Starting server on port: '(\d+)'"), timeout=30)[0]
|
||||||
|
|
||||||
Utility.console_log("Got IP : " + got_ip)
|
Utility.console_log("Got IP : " + got_ip)
|
||||||
|
@@ -1,16 +0,0 @@
|
|||||||
menu "Example Configuration"
|
|
||||||
|
|
||||||
config WIFI_SSID
|
|
||||||
string "WiFi SSID"
|
|
||||||
default "myssid"
|
|
||||||
help
|
|
||||||
SSID (network name) for the example to connect to.
|
|
||||||
|
|
||||||
config WIFI_PASSWORD
|
|
||||||
string "WiFi Password"
|
|
||||||
default "mypassword"
|
|
||||||
help
|
|
||||||
WiFi password (WPA or WPA2) for the example to use.
|
|
||||||
Can be left blank if the network has no security set.
|
|
||||||
|
|
||||||
endmenu
|
|
@@ -12,22 +12,20 @@
|
|||||||
#include <esp_log.h>
|
#include <esp_log.h>
|
||||||
#include <esp_system.h>
|
#include <esp_system.h>
|
||||||
#include <nvs_flash.h>
|
#include <nvs_flash.h>
|
||||||
|
#include "tcpip_adapter.h"
|
||||||
|
#include "esp_eth.h"
|
||||||
|
#include "protocol_examples_common.h"
|
||||||
|
|
||||||
#include <esp_http_server.h>
|
#include <esp_http_server.h>
|
||||||
|
|
||||||
/* An example to demonstrate persistent sockets, with context maintained across
|
/* An example to demonstrate persistent sockets, with context maintained across
|
||||||
* multiple requests on that socket.
|
* multiple requests on that socket.
|
||||||
* The examples use simple WiFi configuration that you can set via 'make menuconfig'.
|
|
||||||
* If you'd rather not, just change the below entries to strings with
|
|
||||||
* the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
|
|
||||||
*/
|
*/
|
||||||
#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID
|
|
||||||
#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD
|
|
||||||
|
|
||||||
static const char *TAG="APP";
|
|
||||||
|
|
||||||
|
static const char *TAG = "example";
|
||||||
|
|
||||||
/* Function to free context */
|
/* Function to free context */
|
||||||
void adder_free_func(void *ctx)
|
static void adder_free_func(void *ctx)
|
||||||
{
|
{
|
||||||
ESP_LOGI(TAG, "/adder Free Context function called");
|
ESP_LOGI(TAG, "/adder Free Context function called");
|
||||||
free(ctx);
|
free(ctx);
|
||||||
@@ -36,7 +34,7 @@ void adder_free_func(void *ctx)
|
|||||||
/* This handler keeps accumulating data that is posted to it into a per
|
/* This handler keeps accumulating data that is posted to it into a per
|
||||||
* socket/session context. And returns the result.
|
* socket/session context. And returns the result.
|
||||||
*/
|
*/
|
||||||
esp_err_t adder_post_handler(httpd_req_t *req)
|
static esp_err_t adder_post_handler(httpd_req_t *req)
|
||||||
{
|
{
|
||||||
/* Log total visitors */
|
/* Log total visitors */
|
||||||
unsigned *visitors = (unsigned *)req->user_ctx;
|
unsigned *visitors = (unsigned *)req->user_ctx;
|
||||||
@@ -78,7 +76,7 @@ esp_err_t adder_post_handler(httpd_req_t *req)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* This handler gets the present value of the accumulator */
|
/* This handler gets the present value of the accumulator */
|
||||||
esp_err_t adder_get_handler(httpd_req_t *req)
|
static esp_err_t adder_get_handler(httpd_req_t *req)
|
||||||
{
|
{
|
||||||
/* Log total visitors */
|
/* Log total visitors */
|
||||||
unsigned *visitors = (unsigned *)req->user_ctx;
|
unsigned *visitors = (unsigned *)req->user_ctx;
|
||||||
@@ -102,7 +100,7 @@ esp_err_t adder_get_handler(httpd_req_t *req)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* This handler resets the value of the accumulator */
|
/* This handler resets the value of the accumulator */
|
||||||
esp_err_t adder_put_handler(httpd_req_t *req)
|
static esp_err_t adder_put_handler(httpd_req_t *req)
|
||||||
{
|
{
|
||||||
/* Log total visitors */
|
/* Log total visitors */
|
||||||
unsigned *visitors = (unsigned *)req->user_ctx;
|
unsigned *visitors = (unsigned *)req->user_ctx;
|
||||||
@@ -143,28 +141,28 @@ esp_err_t adder_put_handler(httpd_req_t *req)
|
|||||||
* the "/adder" URI has been visited */
|
* the "/adder" URI has been visited */
|
||||||
static unsigned visitors = 0;
|
static unsigned visitors = 0;
|
||||||
|
|
||||||
httpd_uri_t adder_post = {
|
static const httpd_uri_t adder_post = {
|
||||||
.uri = "/adder",
|
.uri = "/adder",
|
||||||
.method = HTTP_POST,
|
.method = HTTP_POST,
|
||||||
.handler = adder_post_handler,
|
.handler = adder_post_handler,
|
||||||
.user_ctx = &visitors
|
.user_ctx = &visitors
|
||||||
};
|
};
|
||||||
|
|
||||||
httpd_uri_t adder_get = {
|
static const httpd_uri_t adder_get = {
|
||||||
.uri = "/adder",
|
.uri = "/adder",
|
||||||
.method = HTTP_GET,
|
.method = HTTP_GET,
|
||||||
.handler = adder_get_handler,
|
.handler = adder_get_handler,
|
||||||
.user_ctx = &visitors
|
.user_ctx = &visitors
|
||||||
};
|
};
|
||||||
|
|
||||||
httpd_uri_t adder_put = {
|
static const httpd_uri_t adder_put = {
|
||||||
.uri = "/adder",
|
.uri = "/adder",
|
||||||
.method = HTTP_PUT,
|
.method = HTTP_PUT,
|
||||||
.handler = adder_put_handler,
|
.handler = adder_put_handler,
|
||||||
.user_ctx = &visitors
|
.user_ctx = &visitors
|
||||||
};
|
};
|
||||||
|
|
||||||
httpd_handle_t start_webserver(void)
|
static httpd_handle_t start_webserver(void)
|
||||||
{
|
{
|
||||||
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
||||||
// Start the httpd server
|
// Start the httpd server
|
||||||
@@ -184,69 +182,61 @@ httpd_handle_t start_webserver(void)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void stop_webserver(httpd_handle_t server)
|
static void stop_webserver(httpd_handle_t server)
|
||||||
{
|
{
|
||||||
// Stop the httpd server
|
// Stop the httpd server
|
||||||
httpd_stop(server);
|
httpd_stop(server);
|
||||||
}
|
}
|
||||||
|
|
||||||
static esp_err_t event_handler(void *ctx, system_event_t *event)
|
|
||||||
|
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 *) ctx;
|
httpd_handle_t* server = (httpd_handle_t*) arg;
|
||||||
|
if (*server) {
|
||||||
switch(event->event_id) {
|
ESP_LOGI(TAG, "Stopping webserver");
|
||||||
case SYSTEM_EVENT_STA_START:
|
stop_webserver(*server);
|
||||||
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START");
|
*server = NULL;
|
||||||
ESP_ERROR_CHECK(esp_wifi_connect());
|
|
||||||
break;
|
|
||||||
case SYSTEM_EVENT_STA_GOT_IP:
|
|
||||||
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP");
|
|
||||||
ESP_LOGI(TAG, "Got IP: '%s'",
|
|
||||||
ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
|
|
||||||
|
|
||||||
/* Start the web server */
|
|
||||||
if (*server == NULL) {
|
|
||||||
*server = start_webserver();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case SYSTEM_EVENT_STA_DISCONNECTED:
|
|
||||||
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED");
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_connect());
|
|
||||||
|
|
||||||
/* Stop the webserver */
|
|
||||||
if (*server) {
|
|
||||||
stop_webserver(*server);
|
|
||||||
*server = NULL;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
return ESP_OK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void initialise_wifi(void *arg)
|
static void connect_handler(void* arg, esp_event_base_t event_base,
|
||||||
|
int32_t event_id, void* event_data)
|
||||||
{
|
{
|
||||||
tcpip_adapter_init();
|
httpd_handle_t* server = (httpd_handle_t*) arg;
|
||||||
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, arg));
|
if (*server == NULL) {
|
||||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
ESP_LOGI(TAG, "Starting webserver");
|
||||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
*server = start_webserver();
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
|
}
|
||||||
wifi_config_t wifi_config = {
|
|
||||||
.sta = {
|
|
||||||
.ssid = EXAMPLE_WIFI_SSID,
|
|
||||||
.password = EXAMPLE_WIFI_PASS,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_start());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void app_main()
|
void app_main()
|
||||||
{
|
{
|
||||||
static httpd_handle_t server = NULL;
|
static httpd_handle_t server = NULL;
|
||||||
|
|
||||||
ESP_ERROR_CHECK(nvs_flash_init());
|
ESP_ERROR_CHECK(nvs_flash_init());
|
||||||
initialise_wifi(&server);
|
tcpip_adapter_init();
|
||||||
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||||
|
|
||||||
|
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
|
||||||
|
* Read "Establishing Wi-Fi or Ethernet Connection" section in
|
||||||
|
* examples/protocols/README.md for more information about this function.
|
||||||
|
*/
|
||||||
|
ESP_ERROR_CHECK(example_connect());
|
||||||
|
|
||||||
|
/* Register event handlers to stop the server when Wi-Fi or Ethernet is disconnected,
|
||||||
|
* and re-start it upon connection.
|
||||||
|
*/
|
||||||
|
#ifdef CONFIG_EXAMPLE_CONNECT_WIFI
|
||||||
|
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));
|
||||||
|
#endif // CONFIG_EXAMPLE_CONNECT_WIFI
|
||||||
|
#ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET
|
||||||
|
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &connect_handler, &server));
|
||||||
|
ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, &disconnect_handler, &server));
|
||||||
|
#endif // CONFIG_EXAMPLE_CONNECT_ETHERNET
|
||||||
|
|
||||||
|
/* Start the server for the first time */
|
||||||
|
server = start_webserver();
|
||||||
}
|
}
|
||||||
|
@@ -2,5 +2,9 @@
|
|||||||
# in this exact order for cmake to work correctly
|
# in this exact order for cmake to work correctly
|
||||||
cmake_minimum_required(VERSION 3.5)
|
cmake_minimum_required(VERSION 3.5)
|
||||||
|
|
||||||
|
# (Not part of the boilerplate)
|
||||||
|
# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection.
|
||||||
|
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common)
|
||||||
|
|
||||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||||
project(simple)
|
project(simple)
|
||||||
|
@@ -5,5 +5,7 @@
|
|||||||
|
|
||||||
PROJECT_NAME := simple
|
PROJECT_NAME := simple
|
||||||
|
|
||||||
|
EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common
|
||||||
|
|
||||||
include $(IDF_PATH)/make/project.mk
|
include $(IDF_PATH)/make/project.mk
|
||||||
|
|
||||||
|
@@ -4,10 +4,7 @@ The Example consists of HTTPD server demo with demostration of URI handling :
|
|||||||
1. URI \hello for GET command returns "Hello World!" message
|
1. URI \hello for GET command returns "Hello World!" message
|
||||||
2. URI \echo for POST command echoes back the POSTed message
|
2. URI \echo for POST command echoes back the POSTed message
|
||||||
|
|
||||||
* Configure the project using "make menuconfig" and goto :
|
* Run `make menuconfig` (or `idf.py menuconfig` if using CMake build system) to configure Wi-Fi or Ethernet. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details.
|
||||||
* Example Configuration ->
|
|
||||||
1. WIFI SSID: WIFI network to which your PC is also connected to.
|
|
||||||
2. WIFI Password: WIFI password
|
|
||||||
|
|
||||||
* In order to test the HTTPD server persistent sockets demo :
|
* In order to test the HTTPD server persistent sockets demo :
|
||||||
1. compile and burn the firmware "make flash"
|
1. compile and burn the firmware "make flash"
|
||||||
|
@@ -63,7 +63,7 @@ def test_examples_protocol_http_server_simple(env, extra_data):
|
|||||||
|
|
||||||
# Parse IP address of STA
|
# Parse IP address of STA
|
||||||
Utility.console_log("Waiting to connect with AP")
|
Utility.console_log("Waiting to connect with AP")
|
||||||
got_ip = dut1.expect(re.compile(r"(?:[\s\S]*)Got IP: '(\d+.\d+.\d+.\d+)'"), timeout=120)[0]
|
got_ip = dut1.expect(re.compile(r"(?:[\s\S]*)IPv4 address: (\d+.\d+.\d+.\d+)"), timeout=30)[0]
|
||||||
got_port = dut1.expect(re.compile(r"(?:[\s\S]*)Starting server on port: '(\d+)'"), timeout=30)[0]
|
got_port = dut1.expect(re.compile(r"(?:[\s\S]*)Starting server on port: '(\d+)'"), timeout=30)[0]
|
||||||
|
|
||||||
Utility.console_log("Got IP : " + got_ip)
|
Utility.console_log("Got IP : " + got_ip)
|
||||||
|
@@ -1,16 +0,0 @@
|
|||||||
menu "Example Configuration"
|
|
||||||
|
|
||||||
config WIFI_SSID
|
|
||||||
string "WiFi SSID"
|
|
||||||
default "myssid"
|
|
||||||
help
|
|
||||||
SSID (network name) for the example to connect to.
|
|
||||||
|
|
||||||
config WIFI_PASSWORD
|
|
||||||
string "WiFi Password"
|
|
||||||
default "mypassword"
|
|
||||||
help
|
|
||||||
WiFi password (WPA or WPA2) for the example to use.
|
|
||||||
Can be left blank if the network has no security set.
|
|
||||||
|
|
||||||
endmenu
|
|
@@ -8,29 +8,26 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <esp_wifi.h>
|
#include <esp_wifi.h>
|
||||||
#include <esp_event_loop.h>
|
#include <esp_event.h>
|
||||||
#include <esp_log.h>
|
#include <esp_log.h>
|
||||||
#include <esp_system.h>
|
#include <esp_system.h>
|
||||||
#include <nvs_flash.h>
|
#include <nvs_flash.h>
|
||||||
#include <sys/param.h>
|
#include <sys/param.h>
|
||||||
|
#include "nvs_flash.h"
|
||||||
|
#include "tcpip_adapter.h"
|
||||||
|
#include "esp_eth.h"
|
||||||
|
#include "protocol_examples_common.h"
|
||||||
|
|
||||||
#include <esp_http_server.h>
|
#include <esp_http_server.h>
|
||||||
|
|
||||||
/* A simple example that demonstrates how to create GET and POST
|
/* A simple example that demonstrates how to create GET and POST
|
||||||
* handlers for the web server.
|
* handlers for the web server.
|
||||||
* The examples use simple WiFi configuration that you can set via
|
*/
|
||||||
* 'make menuconfig'.
|
|
||||||
* If you'd rather not, just change the below entries to strings
|
|
||||||
* with the config you want -
|
|
||||||
* ie. #define EXAMPLE_WIFI_SSID "mywifissid"
|
|
||||||
*/
|
|
||||||
#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID
|
|
||||||
#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD
|
|
||||||
|
|
||||||
static const char *TAG="APP";
|
static const char *TAG = "example";
|
||||||
|
|
||||||
/* An HTTP GET handler */
|
/* An HTTP GET handler */
|
||||||
esp_err_t hello_get_handler(httpd_req_t *req)
|
static esp_err_t hello_get_handler(httpd_req_t *req)
|
||||||
{
|
{
|
||||||
char* buf;
|
char* buf;
|
||||||
size_t buf_len;
|
size_t buf_len;
|
||||||
@@ -104,7 +101,7 @@ esp_err_t hello_get_handler(httpd_req_t *req)
|
|||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
httpd_uri_t hello = {
|
static const httpd_uri_t hello = {
|
||||||
.uri = "/hello",
|
.uri = "/hello",
|
||||||
.method = HTTP_GET,
|
.method = HTTP_GET,
|
||||||
.handler = hello_get_handler,
|
.handler = hello_get_handler,
|
||||||
@@ -114,7 +111,7 @@ httpd_uri_t hello = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/* An HTTP POST handler */
|
/* An HTTP POST handler */
|
||||||
esp_err_t echo_post_handler(httpd_req_t *req)
|
static esp_err_t echo_post_handler(httpd_req_t *req)
|
||||||
{
|
{
|
||||||
char buf[100];
|
char buf[100];
|
||||||
int ret, remaining = req->content_len;
|
int ret, remaining = req->content_len;
|
||||||
@@ -145,7 +142,7 @@ esp_err_t echo_post_handler(httpd_req_t *req)
|
|||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
httpd_uri_t echo = {
|
static const httpd_uri_t echo = {
|
||||||
.uri = "/echo",
|
.uri = "/echo",
|
||||||
.method = HTTP_POST,
|
.method = HTTP_POST,
|
||||||
.handler = echo_post_handler,
|
.handler = echo_post_handler,
|
||||||
@@ -182,7 +179,7 @@ esp_err_t http_404_error_handler(httpd_req_t *req, httpd_err_code_t err)
|
|||||||
/* An HTTP PUT handler. This demonstrates realtime
|
/* An HTTP PUT handler. This demonstrates realtime
|
||||||
* registration and deregistration of URI handlers
|
* registration and deregistration of URI handlers
|
||||||
*/
|
*/
|
||||||
esp_err_t ctrl_put_handler(httpd_req_t *req)
|
static esp_err_t ctrl_put_handler(httpd_req_t *req)
|
||||||
{
|
{
|
||||||
char buf;
|
char buf;
|
||||||
int ret;
|
int ret;
|
||||||
@@ -215,14 +212,14 @@ esp_err_t ctrl_put_handler(httpd_req_t *req)
|
|||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
httpd_uri_t ctrl = {
|
static const httpd_uri_t ctrl = {
|
||||||
.uri = "/ctrl",
|
.uri = "/ctrl",
|
||||||
.method = HTTP_PUT,
|
.method = HTTP_PUT,
|
||||||
.handler = ctrl_put_handler,
|
.handler = ctrl_put_handler,
|
||||||
.user_ctx = NULL
|
.user_ctx = NULL
|
||||||
};
|
};
|
||||||
|
|
||||||
httpd_handle_t start_webserver(void)
|
static httpd_handle_t start_webserver(void)
|
||||||
{
|
{
|
||||||
httpd_handle_t server = NULL;
|
httpd_handle_t server = NULL;
|
||||||
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
||||||
@@ -242,69 +239,60 @@ httpd_handle_t start_webserver(void)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void stop_webserver(httpd_handle_t server)
|
static void stop_webserver(httpd_handle_t server)
|
||||||
{
|
{
|
||||||
// Stop the httpd server
|
// Stop the httpd server
|
||||||
httpd_stop(server);
|
httpd_stop(server);
|
||||||
}
|
}
|
||||||
|
|
||||||
static esp_err_t event_handler(void *ctx, system_event_t *event)
|
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 *) ctx;
|
httpd_handle_t* server = (httpd_handle_t*) arg;
|
||||||
|
if (*server) {
|
||||||
switch(event->event_id) {
|
ESP_LOGI(TAG, "Stopping webserver");
|
||||||
case SYSTEM_EVENT_STA_START:
|
stop_webserver(*server);
|
||||||
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START");
|
*server = NULL;
|
||||||
ESP_ERROR_CHECK(esp_wifi_connect());
|
|
||||||
break;
|
|
||||||
case SYSTEM_EVENT_STA_GOT_IP:
|
|
||||||
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP");
|
|
||||||
ESP_LOGI(TAG, "Got IP: '%s'",
|
|
||||||
ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
|
|
||||||
|
|
||||||
/* Start the web server */
|
|
||||||
if (*server == NULL) {
|
|
||||||
*server = start_webserver();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case SYSTEM_EVENT_STA_DISCONNECTED:
|
|
||||||
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED");
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_connect());
|
|
||||||
|
|
||||||
/* Stop the web server */
|
|
||||||
if (*server) {
|
|
||||||
stop_webserver(*server);
|
|
||||||
*server = NULL;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
return ESP_OK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void initialise_wifi(void *arg)
|
static void connect_handler(void* arg, esp_event_base_t event_base,
|
||||||
|
int32_t event_id, void* event_data)
|
||||||
{
|
{
|
||||||
tcpip_adapter_init();
|
httpd_handle_t* server = (httpd_handle_t*) arg;
|
||||||
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, arg));
|
if (*server == NULL) {
|
||||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
ESP_LOGI(TAG, "Starting webserver");
|
||||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
*server = start_webserver();
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
|
}
|
||||||
wifi_config_t wifi_config = {
|
|
||||||
.sta = {
|
|
||||||
.ssid = EXAMPLE_WIFI_SSID,
|
|
||||||
.password = EXAMPLE_WIFI_PASS,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_start());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void app_main()
|
void app_main()
|
||||||
{
|
{
|
||||||
static httpd_handle_t server = NULL;
|
static httpd_handle_t server = NULL;
|
||||||
|
|
||||||
ESP_ERROR_CHECK(nvs_flash_init());
|
ESP_ERROR_CHECK(nvs_flash_init());
|
||||||
initialise_wifi(&server);
|
tcpip_adapter_init();
|
||||||
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||||
|
|
||||||
|
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
|
||||||
|
* Read "Establishing Wi-Fi or Ethernet Connection" section in
|
||||||
|
* examples/protocols/README.md for more information about this function.
|
||||||
|
*/
|
||||||
|
ESP_ERROR_CHECK(example_connect());
|
||||||
|
|
||||||
|
/* Register event handlers to stop the server when Wi-Fi or Ethernet is disconnected,
|
||||||
|
* and re-start it upon connection.
|
||||||
|
*/
|
||||||
|
#ifdef CONFIG_EXAMPLE_CONNECT_WIFI
|
||||||
|
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));
|
||||||
|
#endif // CONFIG_EXAMPLE_CONNECT_WIFI
|
||||||
|
#ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET
|
||||||
|
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &connect_handler, &server));
|
||||||
|
ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, &disconnect_handler, &server));
|
||||||
|
#endif // CONFIG_EXAMPLE_CONNECT_ETHERNET
|
||||||
|
|
||||||
|
/* Start the server for the first time */
|
||||||
|
server = start_webserver();
|
||||||
}
|
}
|
||||||
|
@@ -2,5 +2,9 @@
|
|||||||
# in this exact order for cmake to work correctly
|
# in this exact order for cmake to work correctly
|
||||||
cmake_minimum_required(VERSION 3.5)
|
cmake_minimum_required(VERSION 3.5)
|
||||||
|
|
||||||
|
# (Not part of the boilerplate)
|
||||||
|
# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection.
|
||||||
|
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common)
|
||||||
|
|
||||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||||
project(https_server)
|
project(https_server)
|
||||||
|
@@ -5,5 +5,7 @@
|
|||||||
|
|
||||||
PROJECT_NAME := https_server
|
PROJECT_NAME := https_server
|
||||||
|
|
||||||
|
EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common
|
||||||
|
|
||||||
include $(IDF_PATH)/make/project.mk
|
include $(IDF_PATH)/make/project.mk
|
||||||
|
|
||||||
|
@@ -4,6 +4,8 @@ This example creates a SSL server that returns a simple HTML page when you visit
|
|||||||
|
|
||||||
See the `esp_https_server` component documentation for details.
|
See the `esp_https_server` component documentation for details.
|
||||||
|
|
||||||
|
Before using the example, run `make menuconfig` (or `idf.py menuconfig` if using CMake build system) to configure Wi-Fi or Ethernet. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../README.md) for more details.
|
||||||
|
|
||||||
## Certificates
|
## Certificates
|
||||||
|
|
||||||
You will need to approve a security exception in your browser. This is because of a self signed
|
You will need to approve a security exception in your browser. This is because of a self signed
|
||||||
|
@@ -1,16 +0,0 @@
|
|||||||
menu "Example Configuration"
|
|
||||||
|
|
||||||
config WIFI_SSID
|
|
||||||
string "WiFi SSID"
|
|
||||||
default "myssid"
|
|
||||||
help
|
|
||||||
SSID (network name) for the example to connect to.
|
|
||||||
|
|
||||||
config WIFI_PASSWORD
|
|
||||||
string "WiFi Password"
|
|
||||||
default "mypassword"
|
|
||||||
help
|
|
||||||
WiFi password (WPA or WPA2) for the example to use.
|
|
||||||
Can be left blank if the network has no security set.
|
|
||||||
|
|
||||||
endmenu
|
|
@@ -13,25 +13,21 @@
|
|||||||
#include <esp_system.h>
|
#include <esp_system.h>
|
||||||
#include <nvs_flash.h>
|
#include <nvs_flash.h>
|
||||||
#include <sys/param.h>
|
#include <sys/param.h>
|
||||||
|
#include "tcpip_adapter.h"
|
||||||
|
#include "esp_eth.h"
|
||||||
|
#include "protocol_examples_common.h"
|
||||||
|
|
||||||
#include <esp_https_server.h>
|
#include <esp_https_server.h>
|
||||||
|
|
||||||
/* A simple example that demonstrates how to create GET and POST
|
/* A simple example that demonstrates how to create GET and POST
|
||||||
* handlers for the web server.
|
* handlers and start an HTTPS server.
|
||||||
* The examples use simple WiFi configuration that you can set via
|
|
||||||
* 'make menuconfig'.
|
|
||||||
* If you'd rather not, just change the below entries to strings
|
|
||||||
* with the config you want -
|
|
||||||
* ie. #define EXAMPLE_WIFI_SSID "mywifissid"
|
|
||||||
*/
|
*/
|
||||||
#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID
|
|
||||||
#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD
|
|
||||||
|
|
||||||
static const char *TAG="APP";
|
static const char *TAG = "example";
|
||||||
|
|
||||||
|
|
||||||
/* An HTTP GET handler */
|
/* An HTTP GET handler */
|
||||||
esp_err_t root_get_handler(httpd_req_t *req)
|
static esp_err_t root_get_handler(httpd_req_t *req)
|
||||||
{
|
{
|
||||||
httpd_resp_set_type(req, "text/html");
|
httpd_resp_set_type(req, "text/html");
|
||||||
httpd_resp_send(req, "<h1>Hello Secure World!</h1>", -1); // -1 = use strlen()
|
httpd_resp_send(req, "<h1>Hello Secure World!</h1>", -1); // -1 = use strlen()
|
||||||
@@ -39,14 +35,14 @@ esp_err_t root_get_handler(httpd_req_t *req)
|
|||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
const httpd_uri_t root = {
|
static const httpd_uri_t root = {
|
||||||
.uri = "/",
|
.uri = "/",
|
||||||
.method = HTTP_GET,
|
.method = HTTP_GET,
|
||||||
.handler = root_get_handler
|
.handler = root_get_handler
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
httpd_handle_t start_webserver(void)
|
static httpd_handle_t start_webserver(void)
|
||||||
{
|
{
|
||||||
httpd_handle_t server = NULL;
|
httpd_handle_t server = NULL;
|
||||||
|
|
||||||
@@ -77,74 +73,55 @@ httpd_handle_t start_webserver(void)
|
|||||||
return server;
|
return server;
|
||||||
}
|
}
|
||||||
|
|
||||||
void stop_webserver(httpd_handle_t server)
|
static void stop_webserver(httpd_handle_t server)
|
||||||
{
|
{
|
||||||
// Stop the httpd server
|
// Stop the httpd server
|
||||||
httpd_ssl_stop(server);
|
httpd_ssl_stop(server);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void disconnect_handler(void* arg, esp_event_base_t event_base,
|
||||||
|
int32_t event_id, void* event_data)
|
||||||
|
|
||||||
// ------------------------- application boilerplate ------------------------
|
|
||||||
|
|
||||||
static esp_err_t event_handler(void *ctx, system_event_t *event)
|
|
||||||
{
|
{
|
||||||
httpd_handle_t *server = (httpd_handle_t *) ctx;
|
httpd_handle_t* server = (httpd_handle_t*) arg;
|
||||||
|
if (*server) {
|
||||||
switch(event->event_id) {
|
stop_webserver(*server);
|
||||||
case SYSTEM_EVENT_STA_START:
|
*server = NULL;
|
||||||
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START");
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_connect());
|
|
||||||
break;
|
|
||||||
case SYSTEM_EVENT_STA_GOT_IP:
|
|
||||||
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP");
|
|
||||||
ESP_LOGI(TAG, "Got IP: '%s'",
|
|
||||||
ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
|
|
||||||
|
|
||||||
/* Start the web server */
|
|
||||||
if (*server == NULL) {
|
|
||||||
*server = start_webserver();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case SYSTEM_EVENT_STA_DISCONNECTED:
|
|
||||||
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED");
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_connect());
|
|
||||||
|
|
||||||
/* Stop the web server */
|
|
||||||
if (*server) {
|
|
||||||
stop_webserver(*server);
|
|
||||||
*server = NULL;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
return ESP_OK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void initialise_wifi(void *arg)
|
static void connect_handler(void* arg, esp_event_base_t event_base,
|
||||||
|
int32_t event_id, void* event_data)
|
||||||
{
|
{
|
||||||
tcpip_adapter_init();
|
httpd_handle_t* server = (httpd_handle_t*) arg;
|
||||||
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, arg));
|
if (*server == NULL) {
|
||||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
*server = start_webserver();
|
||||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
}
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
|
|
||||||
wifi_config_t wifi_config = {
|
|
||||||
.sta = {
|
|
||||||
.ssid = EXAMPLE_WIFI_SSID,
|
|
||||||
.password = EXAMPLE_WIFI_PASS,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_start());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void app_main()
|
void app_main()
|
||||||
{
|
{
|
||||||
static httpd_handle_t server = NULL;
|
static httpd_handle_t server = NULL;
|
||||||
|
|
||||||
ESP_ERROR_CHECK(nvs_flash_init());
|
ESP_ERROR_CHECK(nvs_flash_init());
|
||||||
initialise_wifi(&server);
|
tcpip_adapter_init();
|
||||||
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||||
|
|
||||||
|
/* Register event handlers to start server when Wi-Fi or Ethernet is connected,
|
||||||
|
* and stop server when disconnection happens.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifdef CONFIG_EXAMPLE_CONNECT_WIFI
|
||||||
|
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));
|
||||||
|
#endif // CONFIG_EXAMPLE_CONNECT_WIFI
|
||||||
|
#ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET
|
||||||
|
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &connect_handler, &server));
|
||||||
|
ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, &disconnect_handler, &server));
|
||||||
|
#endif // CONFIG_EXAMPLE_CONNECT_ETHERNET
|
||||||
|
|
||||||
|
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
|
||||||
|
* Read "Establishing Wi-Fi or Ethernet Connection" section in
|
||||||
|
* examples/protocols/README.md for more information about this function.
|
||||||
|
*/
|
||||||
|
ESP_ERROR_CHECK(example_connect());
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user