2018-08-30 10:11:21 +08:00
|
|
|
/* WiFi softAP Example
|
2018-03-13 10:44:49 +08:00
|
|
|
|
|
|
|
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 <string.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
2022-01-12 12:23:47 +05:30
|
|
|
#include "esp_mac.h"
|
2018-03-13 10:44:49 +08:00
|
|
|
#include "esp_wifi.h"
|
2018-11-20 18:26:53 +08:00
|
|
|
#include "esp_event.h"
|
2018-03-13 10:44:49 +08:00
|
|
|
#include "esp_log.h"
|
|
|
|
#include "nvs_flash.h"
|
|
|
|
|
|
|
|
#include "lwip/err.h"
|
|
|
|
#include "lwip/sys.h"
|
|
|
|
|
2019-06-23 11:54:31 +10:00
|
|
|
/* The examples use WiFi configuration that you can set via project configuration menu.
|
2018-03-13 10:44:49 +08:00
|
|
|
|
|
|
|
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_ESP_WIFI_SSID CONFIG_ESP_WIFI_SSID
|
|
|
|
#define EXAMPLE_ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD
|
2020-04-17 17:31:46 +08:00
|
|
|
#define EXAMPLE_ESP_WIFI_CHANNEL CONFIG_ESP_WIFI_CHANNEL
|
2019-05-09 16:43:06 +02:00
|
|
|
#define EXAMPLE_MAX_STA_CONN CONFIG_ESP_MAX_STA_CONN
|
2018-03-13 10:44:49 +08:00
|
|
|
|
2025-04-13 20:38:59 +05:30
|
|
|
#if CONFIG_ESP_GTK_REKEYING_ENABLE
|
|
|
|
#define EXAMPLE_GTK_REKEY_INTERVAL CONFIG_ESP_GTK_REKEY_INTERVAL
|
|
|
|
#else
|
|
|
|
#define EXAMPLE_GTK_REKEY_INTERVAL 0
|
|
|
|
#endif
|
|
|
|
|
2018-08-30 10:11:21 +08:00
|
|
|
static const char *TAG = "wifi softAP";
|
2018-03-13 10:44:49 +08:00
|
|
|
|
2019-05-22 17:56:10 +08:00
|
|
|
static void wifi_event_handler(void* arg, esp_event_base_t event_base,
|
2018-11-20 18:26:53 +08:00
|
|
|
int32_t event_id, void* event_data)
|
2018-03-13 10:44:49 +08:00
|
|
|
{
|
2018-11-20 18:26:53 +08:00
|
|
|
if (event_id == WIFI_EVENT_AP_STACONNECTED) {
|
|
|
|
wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data;
|
|
|
|
ESP_LOGI(TAG, "station "MACSTR" join, AID=%d",
|
|
|
|
MAC2STR(event->mac), event->aid);
|
|
|
|
} else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
|
|
|
|
wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data;
|
2024-05-18 18:34:51 +05:30
|
|
|
ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d, reason=%d",
|
|
|
|
MAC2STR(event->mac), event->aid, event->reason);
|
2018-03-13 10:44:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 16:33:30 +07:00
|
|
|
void wifi_init_softap(void)
|
2018-03-13 10:44:49 +08:00
|
|
|
{
|
2019-11-29 10:54:02 +01:00
|
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
2018-11-20 18:26:53 +08:00
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
2019-08-31 16:21:31 +02:00
|
|
|
esp_netif_create_default_wifi_ap();
|
2018-03-13 10:44:49 +08:00
|
|
|
|
|
|
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
2018-11-20 18:26:53 +08:00
|
|
|
|
2020-03-10 17:23:03 +08:00
|
|
|
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
|
|
|
|
ESP_EVENT_ANY_ID,
|
|
|
|
&wifi_event_handler,
|
|
|
|
NULL,
|
|
|
|
NULL));
|
2018-11-20 18:26:53 +08:00
|
|
|
|
2018-03-13 10:44:49 +08:00
|
|
|
wifi_config_t wifi_config = {
|
|
|
|
.ap = {
|
|
|
|
.ssid = EXAMPLE_ESP_WIFI_SSID,
|
|
|
|
.ssid_len = strlen(EXAMPLE_ESP_WIFI_SSID),
|
2020-04-17 17:31:46 +08:00
|
|
|
.channel = EXAMPLE_ESP_WIFI_CHANNEL,
|
2018-03-13 10:44:49 +08:00
|
|
|
.password = EXAMPLE_ESP_WIFI_PASS,
|
|
|
|
.max_connection = EXAMPLE_MAX_STA_CONN,
|
2023-02-28 12:25:05 +08:00
|
|
|
#ifdef CONFIG_ESP_WIFI_SOFTAP_SAE_SUPPORT
|
|
|
|
.authmode = WIFI_AUTH_WPA3_PSK,
|
|
|
|
.sae_pwe_h2e = WPA3_SAE_PWE_BOTH,
|
|
|
|
#else /* CONFIG_ESP_WIFI_SOFTAP_SAE_SUPPORT */
|
|
|
|
.authmode = WIFI_AUTH_WPA2_PSK,
|
|
|
|
#endif
|
2025-04-29 11:38:39 +05:30
|
|
|
#ifdef CONFIG_ESP_WIFI_WPA3_COMPATIBLE_SUPPORT
|
|
|
|
.wpa3_compatible_mode = 1,
|
|
|
|
#endif
|
2022-01-05 10:22:04 +05:30
|
|
|
.pmf_cfg = {
|
2023-02-28 12:25:05 +08:00
|
|
|
.required = true,
|
2022-01-05 10:22:04 +05:30
|
|
|
},
|
2025-04-01 12:00:03 +05:30
|
|
|
#ifdef CONFIG_ESP_WIFI_BSS_MAX_IDLE_SUPPORT
|
|
|
|
.bss_max_idle_cfg = {
|
|
|
|
.period = WIFI_AP_DEFAULT_MAX_IDLE_PERIOD,
|
|
|
|
.protected_keep_alive = 1,
|
|
|
|
},
|
|
|
|
#endif
|
2025-04-13 20:38:59 +05:30
|
|
|
.gtk_rekey_interval = EXAMPLE_GTK_REKEY_INTERVAL,
|
2018-03-13 10:44:49 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
if (strlen(EXAMPLE_ESP_WIFI_PASS) == 0) {
|
|
|
|
wifi_config.ap.authmode = WIFI_AUTH_OPEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
|
2021-01-19 11:58:04 +08:00
|
|
|
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));
|
2018-03-13 10:44:49 +08:00
|
|
|
ESP_ERROR_CHECK(esp_wifi_start());
|
|
|
|
|
2020-04-17 17:31:46 +08:00
|
|
|
ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s channel:%d",
|
|
|
|
EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS, EXAMPLE_ESP_WIFI_CHANNEL);
|
2018-03-13 10:44:49 +08:00
|
|
|
}
|
|
|
|
|
2019-07-16 16:33:30 +07:00
|
|
|
void app_main(void)
|
2018-03-13 10:44:49 +08:00
|
|
|
{
|
|
|
|
//Initialize NVS
|
|
|
|
esp_err_t ret = nvs_flash_init();
|
2018-07-25 20:41:09 +05:30
|
|
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
2018-03-13 10:44:49 +08:00
|
|
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
|
|
|
ret = nvs_flash_init();
|
|
|
|
}
|
|
|
|
ESP_ERROR_CHECK(ret);
|
2019-05-22 17:56:10 +08:00
|
|
|
|
2018-03-13 10:44:49 +08:00
|
|
|
ESP_LOGI(TAG, "ESP_WIFI_MODE_AP");
|
|
|
|
wifi_init_softap();
|
|
|
|
}
|