ESP32创建AP
1 typedef union { 2 wifi_ap_config_t ap; /**< configuration of AP */ 3 wifi_sta_config_t sta; /**< configuration of STA */ 4 } wifi_config_t;
wifi初始化结构体,里面有两个成员,一个用来配置AP参数,一个用来配置STA模式
1 /** @brief Soft-AP configuration settings for the ESP32 */ 2 typedef struct { 3 uint8_t ssid[32]; /**< SSID of ESP32 soft-AP. If ssid_len field is 0, this must be a Null terminated string. Otherwise, length is set according to ssid_len. */ 4 uint8_t password[64]; /**< Password of ESP32 soft-AP. */ 5 uint8_t ssid_len; /**< Optional length of SSID field. */ 6 uint8_t channel; /**< Channel of ESP32 soft-AP */ 7 wifi_auth_mode_t authmode; /**< Auth mode of ESP32 soft-AP. Do not support AUTH_WEP in soft-AP mode */ 8 uint8_t ssid_hidden; /**< Broadcast SSID or not, default 0, broadcast the SSID */ 9 uint8_t max_connection; /**< Max number of stations allowed to connect in, default 4, max 10 */ 10 uint16_t beacon_interval; /**< Beacon interval which should be multiples of 100. Unit: TU(time unit, 1 TU = 1024 us). Range: 100 ~ 60000. Default value: 100 */ 11 wifi_cipher_type_t pairwise_cipher; /**< pairwise cipher of SoftAP, group cipher will be derived using this. cipher values are valid starting from WIFI_CIPHER_TYPE_TKIP, enum values before that will be considered as invalid and default cipher suites(TKIP+CCMP) will be used. Valid cipher suites in softAP mode are WIFI_CIPHER_TYPE_TKIP, WIFI_CIPHER_TYPE_CCMP and WIFI_CIPHER_TYPE_TKIP_CCMP. */ 12 bool ftm_responder; /**< Enable FTM Responder mode */ 13 } wifi_ap_config_t;
AP参数初始化结构体,一些参数可以在menuconfig中配置,也可以在程序中自行定义。
1 wifi_config_t wifi_config = { 2 .ap = { 3 .ssid = EXAMPLE_ESP_WIFI_SSID, 4 .ssid_len = strlen(EXAMPLE_ESP_WIFI_SSID), 5 .channel = EXAMPLE_ESP_WIFI_CHANNEL, 6 .password = EXAMPLE_ESP_WIFI_PASS, 7 .max_connection = EXAMPLE_MAX_STA_CONN, 8 .authmode = WIFI_AUTH_WPA_WPA2_PSK 9 }, 10 };
创建一个wifi初始化结构体,并对参数进行设置,一些没有设置的参数会采用默认值。
1 ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP)); 2 ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config)); 3 ESP_ERROR_CHECK(esp_wifi_start());
最后调用三个函数,设置wifi模式,wifi初始化,启动wifi。