main.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* Simple HTTP + SSL Server Example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <esp_wifi.h>
  8. #include <esp_event.h>
  9. #include <esp_log.h>
  10. #include <esp_system.h>
  11. #include <nvs_flash.h>
  12. #include <sys/param.h>
  13. #include "esp_netif.h"
  14. #include "esp_eth.h"
  15. #include "protocol_examples_common.h"
  16. #include <esp_https_server.h>
  17. /* A simple example that demonstrates how to create GET and POST
  18. * handlers and start an HTTPS server.
  19. */
  20. static const char *TAG = "example";
  21. /* An HTTP GET handler */
  22. static esp_err_t root_get_handler(httpd_req_t *req)
  23. {
  24. httpd_resp_set_type(req, "text/html");
  25. httpd_resp_send(req, "<h1>Hello Secure World!</h1>", -1); // -1 = use strlen()
  26. return ESP_OK;
  27. }
  28. static const httpd_uri_t root = {
  29. .uri = "/",
  30. .method = HTTP_GET,
  31. .handler = root_get_handler
  32. };
  33. static httpd_handle_t start_webserver(void)
  34. {
  35. httpd_handle_t server = NULL;
  36. // Start the httpd server
  37. ESP_LOGI(TAG, "Starting server");
  38. httpd_ssl_config_t conf = HTTPD_SSL_CONFIG_DEFAULT();
  39. extern const unsigned char cacert_pem_start[] asm("_binary_cacert_pem_start");
  40. extern const unsigned char cacert_pem_end[] asm("_binary_cacert_pem_end");
  41. conf.cacert_pem = cacert_pem_start;
  42. conf.cacert_len = cacert_pem_end - cacert_pem_start;
  43. extern const unsigned char prvtkey_pem_start[] asm("_binary_prvtkey_pem_start");
  44. extern const unsigned char prvtkey_pem_end[] asm("_binary_prvtkey_pem_end");
  45. conf.prvtkey_pem = prvtkey_pem_start;
  46. conf.prvtkey_len = prvtkey_pem_end - prvtkey_pem_start;
  47. esp_err_t ret = httpd_ssl_start(&server, &conf);
  48. if (ESP_OK != ret) {
  49. ESP_LOGI(TAG, "Error starting server!");
  50. return NULL;
  51. }
  52. // Set URI handlers
  53. ESP_LOGI(TAG, "Registering URI handlers");
  54. httpd_register_uri_handler(server, &root);
  55. return server;
  56. }
  57. static void stop_webserver(httpd_handle_t server)
  58. {
  59. // Stop the httpd server
  60. httpd_ssl_stop(server);
  61. }
  62. static void disconnect_handler(void* arg, esp_event_base_t event_base,
  63. int32_t event_id, void* event_data)
  64. {
  65. httpd_handle_t* server = (httpd_handle_t*) arg;
  66. if (*server) {
  67. stop_webserver(*server);
  68. *server = NULL;
  69. }
  70. }
  71. static void connect_handler(void* arg, esp_event_base_t event_base,
  72. int32_t event_id, void* event_data)
  73. {
  74. httpd_handle_t* server = (httpd_handle_t*) arg;
  75. if (*server == NULL) {
  76. *server = start_webserver();
  77. }
  78. }
  79. void app_main(void)
  80. {
  81. static httpd_handle_t server = NULL;
  82. ESP_ERROR_CHECK(nvs_flash_init());
  83. ESP_ERROR_CHECK(esp_netif_init());
  84. ESP_ERROR_CHECK(esp_event_loop_create_default());
  85. /* Register event handlers to start server when Wi-Fi or Ethernet is connected,
  86. * and stop server when disconnection happens.
  87. */
  88. #ifdef CONFIG_EXAMPLE_CONNECT_WIFI
  89. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &server));
  90. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server));
  91. #endif // CONFIG_EXAMPLE_CONNECT_WIFI
  92. #ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET
  93. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &connect_handler, &server));
  94. ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, &disconnect_handler, &server));
  95. #endif // CONFIG_EXAMPLE_CONNECT_ETHERNET
  96. /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
  97. * Read "Establishing Wi-Fi or Ethernet Connection" section in
  98. * examples/protocols/README.md for more information about this function.
  99. */
  100. ESP_ERROR_CHECK(example_connect());
  101. }