main.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* Captive Portal 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 <sys/param.h>
  8. #include "esp_event.h"
  9. #include "esp_log.h"
  10. #include "esp_mac.h"
  11. #include "nvs_flash.h"
  12. #include "esp_wifi.h"
  13. #include "esp_netif.h"
  14. #include "lwip/inet.h"
  15. #include "esp_http_server.h"
  16. #include "dns_server.h"
  17. #define EXAMPLE_ESP_WIFI_SSID CONFIG_ESP_WIFI_SSID
  18. #define EXAMPLE_ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD
  19. #define EXAMPLE_MAX_STA_CONN CONFIG_ESP_MAX_STA_CONN
  20. extern const char root_start[] asm("_binary_root_html_start");
  21. extern const char root_end[] asm("_binary_root_html_end");
  22. static const char *TAG = "example";
  23. static void wifi_event_handler(void *arg, esp_event_base_t event_base,
  24. int32_t event_id, void *event_data)
  25. {
  26. if (event_id == WIFI_EVENT_AP_STACONNECTED) {
  27. wifi_event_ap_staconnected_t *event = (wifi_event_ap_staconnected_t *)event_data;
  28. ESP_LOGI(TAG, "station " MACSTR " join, AID=%d",
  29. MAC2STR(event->mac), event->aid);
  30. } else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
  31. wifi_event_ap_stadisconnected_t *event = (wifi_event_ap_stadisconnected_t *)event_data;
  32. ESP_LOGI(TAG, "station " MACSTR " leave, AID=%d",
  33. MAC2STR(event->mac), event->aid);
  34. }
  35. }
  36. static void wifi_init_softap(void)
  37. {
  38. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  39. ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  40. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL));
  41. wifi_config_t wifi_config = {
  42. .ap = {
  43. .ssid = EXAMPLE_ESP_WIFI_SSID,
  44. .ssid_len = strlen(EXAMPLE_ESP_WIFI_SSID),
  45. .password = EXAMPLE_ESP_WIFI_PASS,
  46. .max_connection = EXAMPLE_MAX_STA_CONN,
  47. .authmode = WIFI_AUTH_WPA_WPA2_PSK
  48. },
  49. };
  50. if (strlen(EXAMPLE_ESP_WIFI_PASS) == 0) {
  51. wifi_config.ap.authmode = WIFI_AUTH_OPEN;
  52. }
  53. ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
  54. ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config));
  55. ESP_ERROR_CHECK(esp_wifi_start());
  56. esp_netif_ip_info_t ip_info;
  57. esp_netif_get_ip_info(esp_netif_get_handle_from_ifkey("WIFI_AP_DEF"), &ip_info);
  58. char ip_addr[16];
  59. inet_ntoa_r(ip_info.ip.addr, ip_addr, 16);
  60. ESP_LOGI(TAG, "Set up softAP with IP: %s", ip_addr);
  61. ESP_LOGI(TAG, "wifi_init_softap finished. SSID:'%s' password:'%s'",
  62. EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
  63. }
  64. // HTTP GET Handler
  65. static esp_err_t root_get_handler(httpd_req_t *req)
  66. {
  67. const uint32_t root_len = root_end - root_start;
  68. ESP_LOGI(TAG, "Serve root");
  69. httpd_resp_set_type(req, "text/html");
  70. httpd_resp_send(req, root_start, root_len);
  71. return ESP_OK;
  72. }
  73. static const httpd_uri_t root = {
  74. .uri = "/",
  75. .method = HTTP_GET,
  76. .handler = root_get_handler
  77. };
  78. // HTTP Error (404) Handler - Redirects all requests to the root page
  79. esp_err_t http_404_error_handler(httpd_req_t *req, httpd_err_code_t err)
  80. {
  81. // Set status
  82. httpd_resp_set_status(req, "302 Temporary Redirect");
  83. // Redirect to the "/" root directory
  84. httpd_resp_set_hdr(req, "Location", "/");
  85. // iOS requires content in the response to detect a captive portal, simply redirecting is not sufficient.
  86. httpd_resp_send(req, "Redirect to the captive portal", HTTPD_RESP_USE_STRLEN);
  87. ESP_LOGI(TAG, "Redirecting to root");
  88. return ESP_OK;
  89. }
  90. static httpd_handle_t start_webserver(void)
  91. {
  92. httpd_handle_t server = NULL;
  93. httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  94. config.max_open_sockets = 13;
  95. config.lru_purge_enable = true;
  96. // Start the httpd server
  97. ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
  98. if (httpd_start(&server, &config) == ESP_OK) {
  99. // Set URI handlers
  100. ESP_LOGI(TAG, "Registering URI handlers");
  101. httpd_register_uri_handler(server, &root);
  102. httpd_register_err_handler(server, HTTPD_404_NOT_FOUND, http_404_error_handler);
  103. }
  104. return server;
  105. }
  106. void app_main(void)
  107. {
  108. /*
  109. Turn of warnings from HTTP server as redirecting traffic will yield
  110. lots of invalid requests
  111. */
  112. esp_log_level_set("httpd_uri", ESP_LOG_ERROR);
  113. esp_log_level_set("httpd_txrx", ESP_LOG_ERROR);
  114. esp_log_level_set("httpd_parse", ESP_LOG_ERROR);
  115. // Initialize networking stack
  116. ESP_ERROR_CHECK(esp_netif_init());
  117. // Create default event loop needed by the main app
  118. ESP_ERROR_CHECK(esp_event_loop_create_default());
  119. // Initialize NVS needed by Wi-Fi
  120. ESP_ERROR_CHECK(nvs_flash_init());
  121. // Initialize Wi-Fi including netif with default config
  122. esp_netif_create_default_wifi_ap();
  123. // Initialise ESP32 in SoftAP mode
  124. wifi_init_softap();
  125. // Start the server for the first time
  126. start_webserver();
  127. // Start the DNS server that will redirect all queries to the softAP IP
  128. start_dns_server();
  129. }