This commit is contained in:
2020-09-13 18:48:12 -05:00
parent 3cf6a9b07f
commit 0d818b033e

View File

@@ -18,14 +18,14 @@ static esp_err_t root_get_handler(httpd_req_t *req);
static httpd_handle_t start_webserver(void); static httpd_handle_t start_webserver(void);
static void stop_webserver(httpd_handle_t server); static void stop_webserver(httpd_handle_t server);
static void disconnect_handler(void* arg, esp_event_base_t event_base, static void disconnect_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data); int32_t event_id, void* event_data);
static void connect_handler(void* arg, esp_event_base_t event_base, static void connect_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data); int32_t event_id, void* event_data);
static 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
}; };
@@ -36,95 +36,95 @@ extern struct bme280_dev bme280; // from bme280_sup.c
void app_main(void) void app_main(void)
{ {
static httpd_handle_t server = NULL; static httpd_handle_t server = NULL;
ESP_ERROR_CHECK(nvs_flash_init()); ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default()); ESP_ERROR_CHECK(esp_event_loop_create_default());
/* Register event handlers to start server when Wi-Fi or Ethernet is connected, /* Register event handlers to start server when Wi-Fi or Ethernet is connected,
* and stop server when disconnection happens. * and stop server when disconnection happens.
*/ */
#ifdef CONFIG_EXAMPLE_CONNECT_WIFI #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(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)); ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server));
#endif // CONFIG_EXAMPLE_CONNECT_WIFI #endif // CONFIG_EXAMPLE_CONNECT_WIFI
#ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET #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(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)); ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, &disconnect_handler, &server));
#endif // CONFIG_EXAMPLE_CONNECT_ETHERNET #endif // CONFIG_EXAMPLE_CONNECT_ETHERNET
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
* Read "Establishing Wi-Fi or Ethernet Connection" section in * Read "Establishing Wi-Fi or Ethernet Connection" section in
* examples/protocols/README.md for more information about this function. * examples/protocols/README.md for more information about this function.
*/ */
ESP_ERROR_CHECK(example_connect()); ESP_ERROR_CHECK(example_connect());
my_i2c_setup(); my_i2c_setup();
my_bme280_init(); my_bme280_init();
} }
static 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()
return ESP_OK; return ESP_OK;
} }
static httpd_handle_t start_webserver(void) static httpd_handle_t start_webserver(void)
{ {
httpd_handle_t server = NULL; httpd_handle_t server = NULL;
// Start the httpd server // Start the httpd server
ESP_LOGI(TAG, "Starting server"); ESP_LOGI(TAG, "Starting server");
httpd_ssl_config_t conf = HTTPD_SSL_CONFIG_DEFAULT(); httpd_ssl_config_t conf = HTTPD_SSL_CONFIG_DEFAULT();
extern const unsigned char cacert_pem_start[] asm("_binary_cacert_pem_start"); extern const unsigned char cacert_pem_start[] asm("_binary_cacert_pem_start");
extern const unsigned char cacert_pem_end[] asm("_binary_cacert_pem_end"); extern const unsigned char cacert_pem_end[] asm("_binary_cacert_pem_end");
conf.cacert_pem = cacert_pem_start; conf.cacert_pem = cacert_pem_start;
conf.cacert_len = cacert_pem_end - cacert_pem_start; conf.cacert_len = cacert_pem_end - cacert_pem_start;
extern const unsigned char prvtkey_pem_start[] asm("_binary_prvtkey_pem_start"); extern const unsigned char prvtkey_pem_start[] asm("_binary_prvtkey_pem_start");
extern const unsigned char prvtkey_pem_end[] asm("_binary_prvtkey_pem_end"); extern const unsigned char prvtkey_pem_end[] asm("_binary_prvtkey_pem_end");
conf.prvtkey_pem = prvtkey_pem_start; conf.prvtkey_pem = prvtkey_pem_start;
conf.prvtkey_len = prvtkey_pem_end - prvtkey_pem_start; conf.prvtkey_len = prvtkey_pem_end - prvtkey_pem_start;
esp_err_t ret = httpd_ssl_start(&server, &conf); esp_err_t ret = httpd_ssl_start(&server, &conf);
if (ESP_OK != ret) { if (ESP_OK != ret) {
ESP_LOGI(TAG, "Error starting server!"); ESP_LOGI(TAG, "Error starting server!");
return NULL; return NULL;
} }
// Set URI handlers // Set URI handlers
ESP_LOGI(TAG, "Registering URI handlers"); ESP_LOGI(TAG, "Registering URI handlers");
httpd_register_uri_handler(server, &root); httpd_register_uri_handler(server, &root);
return server; return server;
} }
static 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, static void disconnect_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data) int32_t event_id, void* event_data)
{ {
httpd_handle_t* server = (httpd_handle_t*) arg; httpd_handle_t* server = (httpd_handle_t*) arg;
if (*server) { if (*server) {
stop_webserver(*server); stop_webserver(*server);
*server = NULL; *server = NULL;
} }
} }
static void connect_handler(void* arg, esp_event_base_t event_base, static void connect_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data) int32_t event_id, void* event_data)
{ {
httpd_handle_t* server = (httpd_handle_t*) arg; httpd_handle_t* server = (httpd_handle_t*) arg;
if (*server == NULL) { if (*server == NULL) {
*server = start_webserver(); *server = start_webserver();
} }
} }