2018-08-29 22:11:21 -04:00
|
|
|
/* WiFi softAP Example
|
2018-03-12 22:44:49 -04: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 01:53:47 -05:00
|
|
|
#include "esp_mac.h"
|
2018-03-12 22:44:49 -04:00
|
|
|
#include "esp_wifi.h"
|
2018-11-20 05:26:53 -05:00
|
|
|
#include "esp_event.h"
|
2018-03-12 22:44:49 -04:00
|
|
|
#include "esp_log.h"
|
|
|
|
#include "nvs_flash.h"
|
|
|
|
|
|
|
|
#include "lwip/err.h"
|
|
|
|
#include "lwip/sys.h"
|
|
|
|
|
2019-06-22 21:54:31 -04:00
|
|
|
/* The examples use WiFi configuration that you can set via project configuration menu.
|
2018-03-12 22:44:49 -04: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 05:31:46 -04:00
|
|
|
#define EXAMPLE_ESP_WIFI_CHANNEL CONFIG_ESP_WIFI_CHANNEL
|
2019-05-09 10:43:06 -04:00
|
|
|
#define EXAMPLE_MAX_STA_CONN CONFIG_ESP_MAX_STA_CONN
|
2018-03-12 22:44:49 -04:00
|
|
|
|
2018-08-29 22:11:21 -04:00
|
|
|
static const char *TAG = "wifi softAP";
|
2018-03-12 22:44:49 -04:00
|
|
|
|
2019-05-22 05:56:10 -04:00
|
|
|
static void wifi_event_handler(void* arg, esp_event_base_t event_base,
|
2018-11-20 05:26:53 -05:00
|
|
|
int32_t event_id, void* event_data)
|
2018-03-12 22:44:49 -04:00
|
|
|
{
|
2018-11-20 05:26:53 -05: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;
|
|
|
|
ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d",
|
|
|
|
MAC2STR(event->mac), event->aid);
|
2018-03-12 22:44:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
void wifi_init_softap(void)
|
2018-03-12 22:44:49 -04:00
|
|
|
{
|
2019-11-29 04:54:02 -05:00
|
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
2018-11-20 05:26:53 -05:00
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
2019-08-31 10:21:31 -04:00
|
|
|
esp_netif_create_default_wifi_ap();
|
2018-03-12 22:44:49 -04:00
|
|
|
|
|
|
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
2018-11-20 05:26:53 -05:00
|
|
|
|
2020-03-10 05:23:03 -04:00
|
|
|
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
|
|
|
|
ESP_EVENT_ANY_ID,
|
|
|
|
&wifi_event_handler,
|
|
|
|
NULL,
|
|
|
|
NULL));
|
2018-11-20 05:26:53 -05:00
|
|
|
|
2018-03-12 22:44:49 -04:00
|
|
|
wifi_config_t wifi_config = {
|
|
|
|
.ap = {
|
|
|
|
.ssid = EXAMPLE_ESP_WIFI_SSID,
|
|
|
|
.ssid_len = strlen(EXAMPLE_ESP_WIFI_SSID),
|
2020-04-17 05:31:46 -04:00
|
|
|
.channel = EXAMPLE_ESP_WIFI_CHANNEL,
|
2018-03-12 22:44:49 -04:00
|
|
|
.password = EXAMPLE_ESP_WIFI_PASS,
|
|
|
|
.max_connection = EXAMPLE_MAX_STA_CONN,
|
2023-02-27 23:25:05 -05: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
|
2022-01-04 23:52:04 -05:00
|
|
|
.pmf_cfg = {
|
2023-02-27 23:25:05 -05:00
|
|
|
.required = true,
|
2022-01-04 23:52:04 -05:00
|
|
|
},
|
2018-03-12 22:44:49 -04: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-18 22:58:04 -05:00
|
|
|
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));
|
2018-03-12 22:44:49 -04:00
|
|
|
ESP_ERROR_CHECK(esp_wifi_start());
|
|
|
|
|
2020-04-17 05:31:46 -04: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-12 22:44:49 -04:00
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
void app_main(void)
|
2018-03-12 22:44:49 -04:00
|
|
|
{
|
|
|
|
//Initialize NVS
|
|
|
|
esp_err_t ret = nvs_flash_init();
|
2018-07-25 11:11:09 -04:00
|
|
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
2018-03-12 22:44:49 -04:00
|
|
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
|
|
|
ret = nvs_flash_init();
|
|
|
|
}
|
|
|
|
ESP_ERROR_CHECK(ret);
|
2019-05-22 05:56:10 -04:00
|
|
|
|
2018-03-12 22:44:49 -04:00
|
|
|
ESP_LOGI(TAG, "ESP_WIFI_MODE_AP");
|
|
|
|
wifi_init_softap();
|
|
|
|
}
|