Wi-Fi

Introduction

The WiFi libraries provide support for configuring and monitoring the ESP32 WiFi networking functionality. This includes configuration for:

  • Station mode (aka STA mode or WiFi client mode). ESP32 connects to an access point.
  • AP mode (aka Soft-AP mode or Access Point mode). Stations connect to the ESP32.
  • Combined AP-STA mode (ESP32 is concurrently an access point and a station connected to another access point).
  • Various security modes for the above (WPA, WPA2, WEP, etc.)
  • Scanning for access points (active & passive scanning).
  • Promiscuous mode monitoring of IEEE802.11 WiFi packets.

Application Examples

See wifi directory of ESP-IDF examples that contains the following applications:

API Reference

Functions

esp_err_t esp_wifi_init(const wifi_init_config_t *config)

Init WiFi Alloc resource for WiFi driver, such as WiFi control structure, RX/TX buffer, WiFi NVS structure etc, this WiFi also start WiFi task.

Attention
1. This API must be called before all other WiFi API can be called
Attention
2. Always use WIFI_INIT_CONFIG_DEFAULT macro to init the config to default values, this can guarantee all the fields got correct value when more fields are added into wifi_init_config_t in future release. If you want to set your owner initial values, overwrite the default values which are set by WIFI_INIT_CONFIG_DEFAULT, please be notified that the field ‘magic’ of wifi_init_config_t should always be WIFI_INIT_CONFIG_MAGIC!
Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NO_MEM: out of memory
  • others: refer to error code esp_err.h
Parameters
  • config: pointer to WiFi init configuration structure; can point to a temporary variable.

esp_err_t esp_wifi_deinit(void)

Deinit WiFi Free all resource allocated in esp_wifi_init and stop WiFi task.

Attention
1. This API should be called if you want to remove WiFi driver from the system
Return
ESP_OK: succeed

esp_err_t esp_wifi_set_mode(wifi_mode_t mode)

Set the WiFi operating mode.

Set the WiFi operating mode as station, soft-AP or station+soft-AP, The default mode is soft-AP mode.

Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
  • ESP_ERR_WIFI_ARG: invalid argument
  • others: refer to error code in esp_err.h
Parameters
  • mode: WiFi operating mode

esp_err_t esp_wifi_get_mode(wifi_mode_t *mode)

Get current operating mode of WiFi.

Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
  • ESP_ERR_WIFI_ARG: invalid argument
Parameters
  • mode: store current WiFi mode

esp_err_t esp_wifi_start(void)

Start WiFi according to current configuration If mode is WIFI_MODE_STA, it create station control block and start station If mode is WIFI_MODE_AP, it create soft-AP control block and start soft-AP If mode is WIFI_MODE_APSTA, it create soft-AP and station control block and start soft-AP and station.

Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
  • ESP_ERR_WIFI_ARG: invalid argument
  • ESP_ERR_WIFI_NO_MEM: out of memory
  • ESP_ERR_WIFI_CONN: WiFi internal error, station or soft-AP control block wrong
  • ESP_ERR_WIFI_FAIL: other WiFi internal errors

esp_err_t esp_wifi_stop(void)

Stop WiFi If mode is WIFI_MODE_STA, it stop station and free station control block If mode is WIFI_MODE_AP, it stop soft-AP and free soft-AP control block If mode is WIFI_MODE_APSTA, it stop station/soft-AP and free station/soft-AP control block.

Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init

esp_err_t esp_wifi_restore(void)

Restore WiFi stack persistent settings to default values.

This function will reset settings made using the following APIs:

  • esp_wifi_get_auto_connect,
  • esp_wifi_set_protocol,
  • esp_wifi_set_config related
  • esp_wifi_set_mode

Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init

esp_err_t esp_wifi_connect(void)

Connect the ESP32 WiFi station to the AP.

Attention
1. This API only impact WIFI_MODE_STA or WIFI_MODE_APSTA mode
Attention
2. If the ESP32 is connected to an AP, call esp_wifi_disconnect to disconnect.
Attention
3. The scanning triggered by esp_wifi_start_scan() will not be effective until connection between ESP32 and the AP is established. If ESP32 is scanning and connecting at the same time, ESP32 will abort scanning and return a warning message and error number ESP_ERR_WIFI_STATE. If you want to do reconnection after ESP32 received disconnect event, remember to add the maximum retry time, otherwise the called scan will not work. This is especially true when the AP doesn’t exist, and you still try reconnection after ESP32 received disconnect event with the reason code WIFI_REASON_NO_AP_FOUND.
Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
  • ESP_ERR_WIFI_NOT_START: WiFi is not started by esp_wifi_start
  • ESP_ERR_WIFI_CONN: WiFi internal error, station or soft-AP control block wrong
  • ESP_ERR_WIFI_SSID: SSID of AP which station connects is invalid

esp_err_t esp_wifi_disconnect(void)

Disconnect the ESP32 WiFi station from the AP.

Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi was not initialized by esp_wifi_init
  • ESP_ERR_WIFI_NOT_STARTED: WiFi was not started by esp_wifi_start
  • ESP_ERR_WIFI_FAIL: other WiFi internal errors

esp_err_t esp_wifi_clear_fast_connect(void)

Currently this API is just an stub API.

Return
  • ESP_OK: succeed
  • others: fail

esp_err_t esp_wifi_deauth_sta(uint16_t aid)

deauthenticate all stations or associated id equals to aid

Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
  • ESP_ERR_WIFI_NOT_STARTED: WiFi was not started by esp_wifi_start
  • ESP_ERR_WIFI_ARG: invalid argument
  • ESP_ERR_WIFI_MODE: WiFi mode is wrong
Parameters
  • aid: when aid is 0, deauthenticate all stations, otherwise deauthenticate station whose associated id is aid

esp_err_t esp_wifi_scan_start(const wifi_scan_config_t *config, bool block)

Scan all available APs.

Attention
If this API is called, the found APs are stored in WiFi driver dynamic allocated memory and the will be freed in esp_wifi_scan_get_ap_records, so generally, call esp_wifi_scan_get_ap_records to cause the memory to be freed once the scan is done
Attention
The values of maximum active scan time and passive scan time per channel are limited to 1500 milliseconds. Values above 1500ms may cause station to disconnect from AP and are not recommended.
Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
  • ESP_ERR_WIFI_NOT_STARTED: WiFi was not started by esp_wifi_start
  • ESP_ERR_WIFI_TIMEOUT: blocking scan is timeout
  • ESP_ERR_WIFI_STATE: wifi still connecting when invoke esp_wifi_scan_start
  • others: refer to error code in esp_err.h
Parameters
  • config: configuration of scanning
  • block: if block is true, this API will block the caller until the scan is done, otherwise it will return immediately

esp_err_t esp_wifi_scan_stop(void)

Stop the scan in process.

Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
  • ESP_ERR_WIFI_NOT_STARTED: WiFi is not started by esp_wifi_start

esp_err_t esp_wifi_scan_get_ap_num(uint16_t *number)

Get number of APs found in last scan.

Attention
This API can only be called when the scan is completed, otherwise it may get wrong value.
Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
  • ESP_ERR_WIFI_NOT_STARTED: WiFi is not started by esp_wifi_start
  • ESP_ERR_WIFI_ARG: invalid argument
Parameters
  • number: store number of APIs found in last scan

esp_err_t esp_wifi_scan_get_ap_records(uint16_t *number, wifi_ap_record_t *ap_records)

Get AP list found in last scan.

Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
  • ESP_ERR_WIFI_NOT_STARTED: WiFi is not started by esp_wifi_start
  • ESP_ERR_WIFI_ARG: invalid argument
  • ESP_ERR_WIFI_NO_MEM: out of memory
Parameters
  • number: As input param, it stores max AP number ap_records can hold. As output param, it receives the actual AP number this API returns.
  • ap_records: wifi_ap_record_t array to hold the found APs

esp_err_t esp_wifi_sta_get_ap_info(wifi_ap_record_t *ap_info)

Get information of AP which the ESP32 station is associated with.

Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_CONN: The station interface don’t initialized
  • ESP_ERR_WIFI_NOT_CONNECT: The station is in disconnect status
Parameters

esp_err_t esp_wifi_set_ps(wifi_ps_type_t type)

Set current power save type.

Attention
Default power save type is WIFI_PS_NONE.
Return
ESP_ERR_WIFI_NOT_SUPPORT: not supported yet
Parameters
  • type: power save type

esp_err_t esp_wifi_get_ps(wifi_ps_type_t *type)

Get current power save type.

Attention
Default power save type is WIFI_PS_NONE.
Return
ESP_ERR_WIFI_NOT_SUPPORT: not supported yet
Parameters
  • type: store current power save type

esp_err_t esp_wifi_set_protocol(wifi_interface_t ifx, uint8_t protocol_bitmap)

Set protocol type of specified interface The default protocol is (WIFI_PROTOCOL_11B|WIFI_PROTOCOL_11G|WIFI_PROTOCOL_11N)

Attention
Currently we only support 802.11b or 802.11bg or 802.11bgn mode
Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
  • ESP_ERR_WIFI_IF: invalid interface
  • others: refer to error codes in esp_err.h
Parameters
  • ifx: interfaces
  • protocol_bitmap: WiFi protocol bitmap

esp_err_t esp_wifi_get_protocol(wifi_interface_t ifx, uint8_t *protocol_bitmap)

Get the current protocol bitmap of the specified interface.

Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
  • ESP_ERR_WIFI_IF: invalid interface
  • ESP_ERR_WIFI_ARG: invalid argument
  • others: refer to error codes in esp_err.h
Parameters
  • ifx: interface
  • protocol_bitmap: store current WiFi protocol bitmap of interface ifx

esp_err_t esp_wifi_set_bandwidth(wifi_interface_t ifx, wifi_bandwidth_t bw)

Set the bandwidth of ESP32 specified interface.

Attention
1. API return false if try to configure an interface that is not enabled
Attention
2. WIFI_BW_HT40 is supported only when the interface support 11N
Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
  • ESP_ERR_WIFI_IF: invalid interface
  • ESP_ERR_WIFI_ARG: invalid argument
  • others: refer to error codes in esp_err.h
Parameters
  • ifx: interface to be configured
  • bw: bandwidth

esp_err_t esp_wifi_get_bandwidth(wifi_interface_t ifx, wifi_bandwidth_t *bw)

Get the bandwidth of ESP32 specified interface.

Attention
1. API return false if try to get a interface that is not enable
Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
  • ESP_ERR_WIFI_IF: invalid interface
  • ESP_ERR_WIFI_ARG: invalid argument
Parameters
  • ifx: interface to be configured
  • bw: store bandwidth of interface ifx

esp_err_t esp_wifi_set_channel(uint8_t primary, wifi_second_chan_t second)

Set primary/secondary channel of ESP32.

Attention
1. This is a special API for sniffer
Attention
2. This API should be called after esp_wifi_start() or esp_wifi_set_promiscuous()
Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
  • ESP_ERR_WIFI_IF: invalid interface
  • ESP_ERR_WIFI_ARG: invalid argument
Parameters
  • primary: for HT20, primary is the channel number, for HT40, primary is the primary channel
  • second: for HT20, second is ignored, for HT40, second is the second channel

esp_err_t esp_wifi_get_channel(uint8_t *primary, wifi_second_chan_t *second)

Get the primary/secondary channel of ESP32.

Attention
1. API return false if try to get a interface that is not enable
Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
  • ESP_ERR_WIFI_ARG: invalid argument
Parameters
  • primary: store current primary channel
  • second: store current second channel

esp_err_t esp_wifi_set_country(const wifi_country_t *country)

configure country info

Attention
1. The default country is {.cc=”CN”, .schan=1, .nchan=13, policy=WIFI_COUNTRY_POLICY_AUTO}
Attention
2. When the country policy is WIFI_COUNTRY_POLICY_AUTO, the country info of the AP to which the station is connected is used. E.g. if the configured country info is {.cc=”USA”, .schan=1, .nchan=11} and the country info of the AP to which the station is connected is {.cc=”JP”, .schan=1, .nchan=14} then the country info that will be used is {.cc=”JP”, .schan=1, .nchan=14}. If the station disconnected from the AP the country info is set back back to the country info of the station automatically, {.cc=”USA”, .schan=1, .nchan=11} in the example.
Attention
3. When the country policy is WIFI_COUNTRY_POLICY_MANUAL, always use the configured country info.
Attention
4. When the country info is changed because of configuration or because the station connects to a different external AP, the country IE in probe response/beacon of the soft-AP is changed also.
Attention
5. The country configuration is not stored into flash
Attention
6. This API doesn’t validate the per-country rules, it’s up to the user to fill in all fields according to local regulations.
Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
  • ESP_ERR_WIFI_ARG: invalid argument
Parameters
  • country: the configured country info

esp_err_t esp_wifi_get_country(wifi_country_t *country)

get the current country info

Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
  • ESP_ERR_WIFI_ARG: invalid argument
Parameters
  • country: country info

esp_err_t esp_wifi_set_mac(wifi_interface_t ifx, const uint8_t mac[6])

Set MAC address of the ESP32 WiFi station or the soft-AP interface.

Attention
1. This API can only be called when the interface is disabled
Attention
2. ESP32 soft-AP and station have different MAC addresses, do not set them to be the same.
Attention
3. The bit 0 of the first byte of ESP32 MAC address can not be 1. For example, the MAC address can set to be “1a:XX:XX:XX:XX:XX”, but can not be “15:XX:XX:XX:XX:XX”.
Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
  • ESP_ERR_WIFI_ARG: invalid argument
  • ESP_ERR_WIFI_IF: invalid interface
  • ESP_ERR_WIFI_MAC: invalid mac address
  • ESP_ERR_WIFI_MODE: WiFi mode is wrong
  • others: refer to error codes in esp_err.h
Parameters
  • ifx: interface
  • mac: the MAC address

esp_err_t esp_wifi_get_mac(wifi_interface_t ifx, uint8_t mac[6])

Get mac of specified interface.

Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
  • ESP_ERR_WIFI_ARG: invalid argument
  • ESP_ERR_WIFI_IF: invalid interface
Parameters
  • ifx: interface
  • mac: store mac of the interface ifx

esp_err_t esp_wifi_set_promiscuous_rx_cb(wifi_promiscuous_cb_t cb)

Register the RX callback function in the promiscuous mode.

Each time a packet is received, the registered callback function will be called.

Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
Parameters
  • cb: callback

esp_err_t esp_wifi_set_promiscuous(bool en)

Enable the promiscuous mode.

Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
Parameters
  • en: false - disable, true - enable

esp_err_t esp_wifi_get_promiscuous(bool *en)

Get the promiscuous mode.

Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
  • ESP_ERR_WIFI_ARG: invalid argument
Parameters
  • en: store the current status of promiscuous mode

esp_err_t esp_wifi_set_promiscuous_filter(const wifi_promiscuous_filter_t *filter)

Enable the promiscuous mode packet type filter.

Note
The default filter is to filter all packets except WIFI_PKT_MISC
Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
Parameters
  • filter: the packet type filtered in promiscuous mode.

esp_err_t esp_wifi_get_promiscuous_filter(wifi_promiscuous_filter_t *filter)

Get the promiscuous filter.

Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
  • ESP_ERR_WIFI_ARG: invalid argument
Parameters
  • filter: store the current status of promiscuous filter

esp_err_t esp_wifi_set_config(wifi_interface_t interface, wifi_config_t *conf)

Set the configuration of the ESP32 STA or AP.

Attention
1. This API can be called only when specified interface is enabled, otherwise, API fail
Attention
2. For station configuration, bssid_set needs to be 0; and it needs to be 1 only when users need to check the MAC address of the AP.
Attention
3. ESP32 is limited to only one channel, so when in the soft-AP+station mode, the soft-AP will adjust its channel automatically to be the same as the channel of the ESP32 station.
Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
  • ESP_ERR_WIFI_ARG: invalid argument
  • ESP_ERR_WIFI_IF: invalid interface
  • ESP_ERR_WIFI_MODE: invalid mode
  • ESP_ERR_WIFI_PASSWORD: invalid password
  • ESP_ERR_WIFI_NVS: WiFi internal NVS error
  • others: refer to the erro code in esp_err.h
Parameters
  • interface: interface
  • conf: station or soft-AP configuration

esp_err_t esp_wifi_get_config(wifi_interface_t interface, wifi_config_t *conf)

Get configuration of specified interface.

Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
  • ESP_ERR_WIFI_ARG: invalid argument
  • ESP_ERR_WIFI_IF: invalid interface
Parameters
  • interface: interface
  • conf: station or soft-AP configuration

esp_err_t esp_wifi_ap_get_sta_list(wifi_sta_list_t *sta)

Get STAs associated with soft-AP.

Attention
SSC only API
Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
  • ESP_ERR_WIFI_ARG: invalid argument
  • ESP_ERR_WIFI_MODE: WiFi mode is wrong
  • ESP_ERR_WIFI_CONN: WiFi internal error, the station/soft-AP control block is invalid
Parameters
  • sta: station list

esp_err_t esp_wifi_set_storage(wifi_storage_t storage)

Set the WiFi API configuration storage type.

Attention
1. The default value is WIFI_STORAGE_FLASH
Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
  • ESP_ERR_WIFI_ARG: invalid argument
Parameters
  • storage: : storage type

esp_err_t esp_wifi_set_auto_connect(bool en)

Set auto connect The default value is true.

Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
  • ESP_ERR_WIFI_MODE: WiFi internal error, the station/soft-AP control block is invalid
  • others: refer to error code in esp_err.h
Parameters
  • en: : true - enable auto connect / false - disable auto connect

esp_err_t esp_wifi_get_auto_connect(bool *en)

Get the auto connect flag.

Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
  • ESP_ERR_WIFI_ARG: invalid argument
Parameters
  • en: store current auto connect configuration

esp_err_t esp_wifi_set_vendor_ie(bool enable, wifi_vendor_ie_type_t type, wifi_vendor_ie_id_t idx, const void *vnd_ie)

Set 802.11 Vendor-Specific Information Element.

Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init()
  • ESP_ERR_WIFI_ARG: Invalid argument, including if first byte of vnd_ie is not WIFI_VENDOR_IE_ELEMENT_ID (0xDD) or second byte is an invalid length.
  • ESP_ERR_WIFI_NO_MEM: Out of memory
Parameters
  • enable: If true, specified IE is enabled. If false, specified IE is removed.
  • type: Information Element type. Determines the frame type to associate with the IE.
  • idx: Index to set or clear. Each IE type can be associated with up to two elements (indices 0 & 1).
  • vnd_ie: Pointer to vendor specific element data. First 6 bytes should be a header with fields matching vendor_ie_data_t. If enable is false, this argument is ignored and can be NULL. Data does not need to remain valid after the function returns.

esp_err_t esp_wifi_set_vendor_ie_cb(esp_vendor_ie_cb_t cb, void *ctx)

Register Vendor-Specific Information Element monitoring callback.

Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
Parameters
  • cb: Callback function
  • ctx: Context argument, passed to callback function.

esp_err_t esp_wifi_set_max_tx_power(int8_t power)

Set maximum WiFi transmiting power.

Attention
WiFi transmiting power is divided to six levels in phy init data. Level0 represents highest transmiting power and level5 represents lowest transmiting power. Packets of different rates are transmitted in different powers according to the configuration in phy init data. This API only sets maximum WiFi transmiting power. If this API is called, the transmiting power of every packet will be less than or equal to the value set by this API. If this API is not called, the value of maximum transmitting power set in phy_init_data.bin or menuconfig (depend on whether to use phy init data in partition or not) will be used. Default value is level0. Values passed in power are mapped to transmit power levels as follows:
  • [78, 127]: level0
  • [76, 77]: level1
  • [74, 75]: level2
  • [68, 73]: level3
  • [60, 67]: level4
  • [52, 59]: level5
  • [44, 51]: level5 - 2dBm
  • [34, 43]: level5 - 4.5dBm
  • [28, 33]: level5 - 6dBm
  • [20, 27]: level5 - 8dBm
  • [8, 19]: level5 - 11dBm
  • [-128, 7]: level5 - 14dBm
Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
  • ESP_ERR_WIFI_NOT_START: WiFi is not started by esp_wifi_start
Parameters
  • power: Maximum WiFi transmiting power.

esp_err_t esp_wifi_get_max_tx_power(int8_t *power)

Get maximum WiFi transmiting power.

Attention
This API gets maximum WiFi transmiting power. Values got from power are mapped to transmit power levels as follows:
  • 78: 19.5dBm
  • 76: 19dBm
  • 74: 18.5dBm
  • 68: 17dBm
  • 60: 15dBm
  • 52: 13dBm
  • 44: 11dBm
  • 34: 8.5dBm
  • 28: 7dBm
  • 20: 5dBm
  • 8: 2dBm
  • -4: -1dBm
Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
  • ESP_ERR_WIFI_NOT_START: WiFi is not started by esp_wifi_start
  • ESP_ERR_WIFI_ARG: invalid argument
Parameters
  • power: Maximum WiFi transmiting power.

Structures

struct wifi_init_config_t

WiFi stack configuration parameters passed to esp_wifi_init call.

Public Members

system_event_handler_t event_handler

WiFi event handler

wpa_crypto_funcs_t wpa_crypto_funcs

WiFi station crypto functions when connect

int static_rx_buf_num

WiFi static RX buffer number

int dynamic_rx_buf_num

WiFi dynamic RX buffer number

int tx_buf_type

WiFi TX buffer type

int static_tx_buf_num

WiFi static TX buffer number

int dynamic_tx_buf_num

WiFi dynamic TX buffer number

int ampdu_rx_enable

WiFi AMPDU RX feature enable flag

int ampdu_tx_enable

WiFi AMPDU TX feature enable flag

int nvs_enable

WiFi NVS flash enable flag

int nano_enable

Nano option for printf/scan family enable flag

int tx_ba_win

WiFi Block Ack TX window size

int rx_ba_win

WiFi Block Ack RX window size

int magic

WiFi init magic number, it should be the last field

Macros

ESP_ERR_WIFI_OK

No error

ESP_ERR_WIFI_FAIL

General fail code

ESP_ERR_WIFI_NO_MEM

Out of memory

ESP_ERR_WIFI_ARG

Invalid argument

ESP_ERR_WIFI_NOT_SUPPORT

Indicates that API is not supported yet

ESP_ERR_WIFI_NOT_INIT

WiFi driver was not installed by esp_wifi_init

ESP_ERR_WIFI_NOT_STARTED

WiFi driver was not started by esp_wifi_start

ESP_ERR_WIFI_NOT_STOPPED

WiFi driver was not stopped by esp_wifi_stop

ESP_ERR_WIFI_IF

WiFi interface error

ESP_ERR_WIFI_MODE

WiFi mode error

ESP_ERR_WIFI_STATE

WiFi internal state error

ESP_ERR_WIFI_CONN

WiFi internal control block of station or soft-AP error

ESP_ERR_WIFI_NVS

WiFi internal NVS module error

ESP_ERR_WIFI_MAC

MAC address is invalid

ESP_ERR_WIFI_SSID

SSID is invalid

ESP_ERR_WIFI_PASSWORD

Password is invalid

ESP_ERR_WIFI_TIMEOUT

Timeout error

ESP_ERR_WIFI_WAKE_FAIL

WiFi is in sleep state(RF closed) and wakeup fail

ESP_ERR_WIFI_WOULD_BLOCK

The caller would block

ESP_ERR_WIFI_NOT_CONNECT

Station still in disconnect status

WIFI_STATIC_TX_BUFFER_NUM
WIFI_DYNAMIC_TX_BUFFER_NUM
WIFI_AMPDU_RX_ENABLED
WIFI_AMPDU_TX_ENABLED
WIFI_NVS_ENABLED
WIFI_NANO_FORMAT_ENABLED
WIFI_INIT_CONFIG_MAGIC
WIFI_DEFAULT_TX_BA_WIN
WIFI_DEFAULT_RX_BA_WIN
WIFI_INIT_CONFIG_DEFAULT()

Type Definitions

typedef void (*wifi_promiscuous_cb_t)(void *buf, wifi_promiscuous_pkt_type_t type)

The RX callback function in the promiscuous mode. Each time a packet is received, the callback function will be called.

Parameters

typedef void (*esp_vendor_ie_cb_t)(void *ctx, wifi_vendor_ie_type_t type, const uint8_t sa[6], const vendor_ie_data_t *vnd_ie, int rssi)

Function signature for received Vendor-Specific Information Element callback.

Parameters
  • ctx: Context argument, as passed to esp_wifi_set_vendor_ie_cb() when registering callback.
  • type: Information element type, based on frame type received.
  • sa: Source 802.11 address.
  • vnd_ie: Pointer to the vendor specific element data received.
  • rssi: Received signal strength indication.

Unions

union wifi_scan_time_t
#include <esp_wifi_types.h>

Aggregate of active & passive scan time per channel.

Public Members

wifi_active_scan_time_t active

active scan time per channel, units: millisecond.

uint32_t passive

passive scan time per channel, units: millisecond, values above 1500ms may cause station to disconnect from AP and are not recommended.

union wifi_config_t
#include <esp_wifi_types.h>

Configuration data for ESP32 AP or STA.

The usage of this union (for ap or sta configuration) is determined by the accompanying interface argument passed to esp_wifi_set_config() or esp_wifi_get_config()

Public Members

wifi_ap_config_t ap

configuration of AP

wifi_sta_config_t sta

configuration of STA

Structures

struct wifi_country_t

Structure describing WiFi country-based regional restrictions.

Public Members

char cc[3]

country code string

uint8_t schan

start channel

uint8_t nchan

total channel number

wifi_country_policy_t policy

country policy

struct wifi_active_scan_time_t

Range of active scan times per channel.

Public Members

uint32_t min

minimum active scan time per channel, units: millisecond

uint32_t max

maximum active scan time per channel, units: millisecond, values above 1500ms may cause station to disconnect from AP and are not recommended.

struct wifi_scan_config_t

Parameters for an SSID scan.

Public Members

uint8_t *ssid

SSID of AP

uint8_t *bssid

MAC address of AP

uint8_t channel

channel, scan the specific channel

bool show_hidden

enable to scan AP whose SSID is hidden

wifi_scan_type_t scan_type

scan type, active or passive

wifi_scan_time_t scan_time

scan time per channel

struct wifi_ap_record_t

Description of an WiFi AP.

Public Members

uint8_t bssid[6]

MAC address of AP

uint8_t ssid[33]

SSID of AP

uint8_t primary

channel of AP

wifi_second_chan_t second

second channel of AP

int8_t rssi

signal strength of AP

wifi_auth_mode_t authmode

authmode of AP

wifi_cipher_type_t pairwise_cipher

pairwise cipher of AP

wifi_cipher_type_t group_cipher

group cipher of AP

uint32_t phy_11b

bit: 0 flag to identify if 11b mode is enabled or not

uint32_t phy_11g

bit: 1 flag to identify if 11g mode is enabled or not

uint32_t phy_11n

bit: 2 flag to identify if 11n mode is enabled or not

uint32_t phy_lr

bit: 3 flag to identify if low rate is enabled or not

uint32_t wps

bit: 4 flag to identify if WPS is supported or not

uint32_t reserved

bit: 5..31 reserved

struct wifi_fast_scan_threshold_t

Structure describing parameters for a WiFi fast scan.

Public Members

int8_t rssi

The minimum rssi to accept in the fast scan mode

wifi_auth_mode_t authmode

The weakest authmode to accept in the fast scan mode

struct wifi_ap_config_t

Soft-AP configuration settings for the ESP32.

Public Members

uint8_t ssid[32]

SSID of ESP32 soft-AP

uint8_t password[64]

Password of ESP32 soft-AP

uint8_t ssid_len

Length of SSID. If softap_config.ssid_len==0, check the SSID until there is a termination character; otherwise, set the SSID length according to softap_config.ssid_len.

uint8_t channel

Channel of ESP32 soft-AP

wifi_auth_mode_t authmode

Auth mode of ESP32 soft-AP. Do not support AUTH_WEP in soft-AP mode

uint8_t ssid_hidden

Broadcast SSID or not, default 0, broadcast the SSID

uint8_t max_connection

Max number of stations allowed to connect in, default 4, max 4

uint16_t beacon_interval

Beacon interval, 100 ~ 60000 ms, default 100 ms

struct wifi_sta_config_t

STA configuration settings for the ESP32.

Public Members

uint8_t ssid[32]

SSID of target AP

uint8_t password[64]

password of target AP

wifi_scan_method_t scan_method

do all channel scan or fast scan

bool bssid_set

whether set MAC address of target AP or not. Generally, station_config.bssid_set needs to be 0; and it needs to be 1 only when users need to check the MAC address of the AP.

uint8_t bssid[6]

MAC address of target AP

uint8_t channel

channel of target AP. Set to 1~13 to scan starting from the specified channel before connecting to AP. If the channel of AP is unknown, set it to 0.

wifi_sort_method_t sort_method

sort the connect AP in the list by rssi or security mode

wifi_scan_threshold_t threshold

When scan_method is set, only APs which have an auth mode that is more secure than the selected auth mode and a signal stronger than the minimum RSSI will be used.

struct wifi_sta_info_t

Description of STA associated with AP.

Public Members

uint8_t mac[6]

mac address

struct wifi_sta_list_t

List of stations associated with the ESP32 Soft-AP.

Public Members

wifi_sta_info_t sta[ESP_WIFI_MAX_CONN_NUM]

station list

int num

number of stations in the list (other entries are invalid)

struct vendor_ie_data_t

Vendor Information Element header.

The first bytes of the Information Element will match this header. Payload follows.

Public Members

uint8_t element_id

Should be set to WIFI_VENDOR_IE_ELEMENT_ID (0xDD)

uint8_t length

Length of all bytes in the element data following this field. Minimum 4.

uint8_t vendor_oui[3]

Vendor identifier (OUI).

uint8_t vendor_oui_type

Vendor-specific OUI type.

uint8_t payload[0]

Payload. Length is equal to value in ‘length’ field, minus 4.

struct wifi_pkt_rx_ctrl_t

Received packet radio metadata header, this is the common header at the beginning of all promiscuous mode RX callback buffers.

Public Members

signed rssi

signal intensity of packet

unsigned rate

data rate

unsigned __pad0__

reserve

unsigned sig_mode

0:is not 11n packet; 1:is 11n packet

unsigned __pad1__

reserve

unsigned mcs

if is 11n packet, shows the modulation(range from 0 to 76)

unsigned cwb

if is 11n packet, shows if is HT40 packet or not

unsigned __pad2__

reserve

unsigned smoothing

reserve

unsigned not_sounding

reserve

unsigned __pad3__

reserve

unsigned aggregation

Aggregation

unsigned stbc

STBC

unsigned fec_coding

Flag is set for 11n packets which are LDPC

unsigned sgi

SGI

unsigned noise_floor

noise floor

unsigned ampdu_cnt

ampdu cnt

unsigned channel

which channel this packet in

unsigned __pad4__

reserve

unsigned timestamp

timestamp

unsigned __pad5__

reserve

unsigned __pad6__

reserve

unsigned sig_len

length of packet

unsigned __pad7__

reserve

unsigned rx_state

rx state

struct wifi_promiscuous_pkt_t

Payload passed to ‘buf’ parameter of promiscuous mode RX callback.

Public Members

wifi_pkt_rx_ctrl_t rx_ctrl

metadata header

uint8_t payload[0]

Data or management payload. Length of payload is described by rx_ctrl.sig_len. Type of content determined by packet type argument of callback.

struct wifi_promiscuous_filter_t

Mask for filtering different packet types in promiscuous mode.

Public Members

uint32_t filter_mask

OR of one or more filter values WIFI_PROMIS_FILTER_*

Macros

WIFI_IF_STA
WIFI_IF_AP
WIFI_PROTOCOL_11B
WIFI_PROTOCOL_11G
WIFI_PROTOCOL_11N
WIFI_PROTOCOL_LR
ESP_WIFI_MAX_CONN_NUM

max number of stations which can connect to ESP32 soft-AP

WIFI_VENDOR_IE_ELEMENT_ID
WIFI_PROMIS_FILTER_MASK_ALL

filter all packets

WIFI_PROMIS_FILTER_MASK_MGMT

filter the packets with type of WIFI_PKT_MGMT

WIFI_PROMIS_FILTER_MASK_DATA

filter the packets with type of WIFI_PKT_DATA

WIFI_PROMIS_FILTER_MASK_MISC

filter the packets with type of WIFI_PKT_MISC

WIFI_PROMIS_FILTER_MASK_DATA_MPDU

filter the MPDU which is a kind of WIFI_PKT_DATA

WIFI_PROMIS_FILTER_MASK_DATA_AMPDU

filter the AMPDU which is a kind of WIFI_PKT_DATA

Type Definitions

typedef esp_interface_t wifi_interface_t
typedef wifi_fast_scan_threshold_t wifi_scan_threshold_t

wifi_fast_scan_threshold_t only used in fast scan mode once, now it enabled in all channel scan, the wifi_fast_scan_threshold_t will be remove in version 4.0

Enumerations

enum wifi_mode_t

Values:

WIFI_MODE_NULL = 0

null mode

WIFI_MODE_STA

WiFi station mode

WIFI_MODE_AP

WiFi soft-AP mode

WIFI_MODE_APSTA

WiFi station + soft-AP mode

WIFI_MODE_MAX
enum wifi_country_policy_t

Values:

WIFI_COUNTRY_POLICY_AUTO

Country policy is auto, use the country info of AP to which the station is connected

WIFI_COUNTRY_POLICY_MANUAL

Country policy is manual, always use the configured country info

enum wifi_auth_mode_t

Values:

WIFI_AUTH_OPEN = 0

authenticate mode : open

WIFI_AUTH_WEP

authenticate mode : WEP

WIFI_AUTH_WPA_PSK

authenticate mode : WPA_PSK

WIFI_AUTH_WPA2_PSK

authenticate mode : WPA2_PSK

WIFI_AUTH_WPA_WPA2_PSK

authenticate mode : WPA_WPA2_PSK

WIFI_AUTH_WPA2_ENTERPRISE

authenticate mode : WPA2_ENTERPRISE

WIFI_AUTH_MAX
enum wifi_err_reason_t

Values:

WIFI_REASON_UNSPECIFIED = 1
WIFI_REASON_AUTH_EXPIRE = 2
WIFI_REASON_AUTH_LEAVE = 3
WIFI_REASON_ASSOC_EXPIRE = 4
WIFI_REASON_ASSOC_TOOMANY = 5
WIFI_REASON_NOT_AUTHED = 6
WIFI_REASON_NOT_ASSOCED = 7
WIFI_REASON_ASSOC_LEAVE = 8
WIFI_REASON_ASSOC_NOT_AUTHED = 9
WIFI_REASON_DISASSOC_PWRCAP_BAD = 10
WIFI_REASON_DISASSOC_SUPCHAN_BAD = 11
WIFI_REASON_IE_INVALID = 13
WIFI_REASON_MIC_FAILURE = 14
WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT = 15
WIFI_REASON_GROUP_KEY_UPDATE_TIMEOUT = 16
WIFI_REASON_IE_IN_4WAY_DIFFERS = 17
WIFI_REASON_GROUP_CIPHER_INVALID = 18
WIFI_REASON_PAIRWISE_CIPHER_INVALID = 19
WIFI_REASON_AKMP_INVALID = 20
WIFI_REASON_UNSUPP_RSN_IE_VERSION = 21
WIFI_REASON_INVALID_RSN_IE_CAP = 22
WIFI_REASON_802_1X_AUTH_FAILED = 23
WIFI_REASON_CIPHER_SUITE_REJECTED = 24
WIFI_REASON_BEACON_TIMEOUT = 200
WIFI_REASON_NO_AP_FOUND = 201
WIFI_REASON_AUTH_FAIL = 202
WIFI_REASON_ASSOC_FAIL = 203
WIFI_REASON_HANDSHAKE_TIMEOUT = 204
WIFI_REASON_CONNECTION_FAIL = 205
enum wifi_second_chan_t

Values:

WIFI_SECOND_CHAN_NONE = 0

the channel width is HT20

WIFI_SECOND_CHAN_ABOVE

the channel width is HT40 and the second channel is above the primary channel

WIFI_SECOND_CHAN_BELOW

the channel width is HT40 and the second channel is below the primary channel

enum wifi_scan_type_t

Values:

WIFI_SCAN_TYPE_ACTIVE = 0

active scan

WIFI_SCAN_TYPE_PASSIVE

passive scan

enum wifi_cipher_type_t

Values:

WIFI_CIPHER_TYPE_NONE = 0

the cipher type is none

WIFI_CIPHER_TYPE_WEP40

the cipher type is WEP40

WIFI_CIPHER_TYPE_WEP104

the cipher type is WEP104

WIFI_CIPHER_TYPE_TKIP

the cipher type is TKIP

WIFI_CIPHER_TYPE_CCMP

the cipher type is CCMP

WIFI_CIPHER_TYPE_TKIP_CCMP

the cipher type is TKIP and CCMP

WIFI_CIPHER_TYPE_UNKNOWN

the cipher type is unknown

enum wifi_scan_method_t

Values:

WIFI_FAST_SCAN = 0

Do fast scan, scan will end after find SSID match AP

WIFI_ALL_CHANNEL_SCAN

All channel scan, scan will end after scan all the channel

enum wifi_sort_method_t

Values:

WIFI_CONNECT_AP_BY_SIGNAL = 0

Sort match AP in scan list by RSSI

WIFI_CONNECT_AP_BY_SECURITY

Sort match AP in scan list by security mode

enum wifi_ps_type_t

Values:

WIFI_PS_NONE

No power save

WIFI_PS_MODEM

Modem power save

enum wifi_bandwidth_t

Values:

WIFI_BW_HT20 = 1
WIFI_BW_HT40
enum wifi_storage_t

Values:

WIFI_STORAGE_FLASH

all configuration will strore in both memory and flash

WIFI_STORAGE_RAM

all configuration will only store in the memory

enum wifi_vendor_ie_type_t

Vendor Information Element type.

Determines the frame type that the IE will be associated with.

Values:

WIFI_VND_IE_TYPE_BEACON
WIFI_VND_IE_TYPE_PROBE_REQ
WIFI_VND_IE_TYPE_PROBE_RESP
WIFI_VND_IE_TYPE_ASSOC_REQ
WIFI_VND_IE_TYPE_ASSOC_RESP
enum wifi_vendor_ie_id_t

Vendor Information Element index.

Each IE type can have up to two associated vendor ID elements.

Values:

WIFI_VND_IE_ID_0
WIFI_VND_IE_ID_1
enum wifi_promiscuous_pkt_type_t

Promiscuous frame type.

Passed to promiscuous mode RX callback to indicate the type of parameter in the buffer.

Values:

WIFI_PKT_MGMT

Management frame, indicates ‘buf’ argument is wifi_promiscuous_pkt_t

WIFI_PKT_DATA

Data frame, indiciates ‘buf’ argument is wifi_promiscuous_pkt_t

WIFI_PKT_MISC

Other type, such as MIMO etc. ‘buf’ argument is wifi_promiscuous_pkt_t but the payload is zero length.