softap_example_main.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* WiFi softAP 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 <string.h>
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "esp_mac.h"
  11. #include "esp_wifi.h"
  12. #include "esp_event.h"
  13. #include "esp_log.h"
  14. #include "nvs_flash.h"
  15. #include "lwip/err.h"
  16. #include "lwip/sys.h"
  17. /* The examples use WiFi configuration that you can set via project configuration menu.
  18. If you'd rather not, just change the below entries to strings with
  19. the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
  20. */
  21. #define EXAMPLE_ESP_WIFI_SSID CONFIG_ESP_WIFI_SSID
  22. #define EXAMPLE_ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD
  23. #define EXAMPLE_ESP_WIFI_CHANNEL CONFIG_ESP_WIFI_CHANNEL
  24. #define EXAMPLE_MAX_STA_CONN CONFIG_ESP_MAX_STA_CONN
  25. static const char *TAG = "wifi softAP";
  26. static void wifi_event_handler(void* arg, esp_event_base_t event_base,
  27. int32_t event_id, void* event_data)
  28. {
  29. if (event_id == WIFI_EVENT_AP_STACONNECTED) {
  30. wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data;
  31. ESP_LOGI(TAG, "station "MACSTR" join, AID=%d",
  32. MAC2STR(event->mac), event->aid);
  33. } else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
  34. wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data;
  35. ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d",
  36. MAC2STR(event->mac), event->aid);
  37. }
  38. }
  39. void wifi_init_softap(void)
  40. {
  41. ESP_ERROR_CHECK(esp_netif_init());
  42. ESP_ERROR_CHECK(esp_event_loop_create_default());
  43. esp_netif_create_default_wifi_ap();
  44. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  45. ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  46. ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
  47. ESP_EVENT_ANY_ID,
  48. &wifi_event_handler,
  49. NULL,
  50. NULL));
  51. wifi_config_t wifi_config = {
  52. .ap = {
  53. .ssid = EXAMPLE_ESP_WIFI_SSID,
  54. .ssid_len = strlen(EXAMPLE_ESP_WIFI_SSID),
  55. .channel = EXAMPLE_ESP_WIFI_CHANNEL,
  56. .password = EXAMPLE_ESP_WIFI_PASS,
  57. .max_connection = EXAMPLE_MAX_STA_CONN,
  58. #ifdef CONFIG_ESP_WIFI_SOFTAP_SAE_SUPPORT
  59. .authmode = WIFI_AUTH_WPA3_PSK,
  60. .sae_pwe_h2e = WPA3_SAE_PWE_BOTH,
  61. #else /* CONFIG_ESP_WIFI_SOFTAP_SAE_SUPPORT */
  62. .authmode = WIFI_AUTH_WPA2_PSK,
  63. #endif
  64. .pmf_cfg = {
  65. .required = true,
  66. },
  67. },
  68. };
  69. if (strlen(EXAMPLE_ESP_WIFI_PASS) == 0) {
  70. wifi_config.ap.authmode = WIFI_AUTH_OPEN;
  71. }
  72. ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
  73. ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));
  74. ESP_ERROR_CHECK(esp_wifi_start());
  75. ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s channel:%d",
  76. EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS, EXAMPLE_ESP_WIFI_CHANNEL);
  77. }
  78. void app_main(void)
  79. {
  80. //Initialize NVS
  81. esp_err_t ret = nvs_flash_init();
  82. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  83. ESP_ERROR_CHECK(nvs_flash_erase());
  84. ret = nvs_flash_init();
  85. }
  86. ESP_ERROR_CHECK(ret);
  87. ESP_LOGI(TAG, "ESP_WIFI_MODE_AP");
  88. wifi_init_softap();
  89. }