Wi-Fi

Overview

Instructions

Application Examples

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

API Reference

Functions

esp_err_t esp_wifi_init(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: provide WiFi init configuration

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 eps_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 eps_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 eps_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 eps_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 eps_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.
Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by eps_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 eps_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 eps_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(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 eps_wifi_init
  • ESP_ERR_WIFI_NOT_STARTED: WiFi was not started by esp_wifi_start
  • ESP_ERR_WIFI_TIMEOUT: blocking scan is timeout
  • 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 eps_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 eps_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 eps_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
  • others: fail
Parameters
  • ap_info: the wifi_ap_record_t to hold AP information

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 eps_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 eps_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 eps_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 eps_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 eps_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 eps_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(wifi_country_t country)

Set country code The default value is WIFI_COUNTRY_CN.

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

esp_err_t esp_wifi_get_country(wifi_country_t *country)

Get country code.

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

esp_err_t esp_wifi_set_mac(wifi_interface_t ifx, 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 eps_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 eps_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 eps_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 eps_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 eps_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 filter.

Attention
1. 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 eps_wifi_init
Parameters
  • filter: the packet type filtered by promisucous

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 eps_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 ifx, 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 eps_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
  • ifx: interface
  • conf: station or soft-AP configuration

esp_err_t esp_wifi_get_config(wifi_interface_t ifx, wifi_config_t *conf)

Get configuration of specified interface.

Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by eps_wifi_init
  • ESP_ERR_WIFI_ARG: invalid argument
  • ESP_ERR_WIFI_IF: invalid interface
Parameters
  • ifx: 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 eps_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 eps_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 eps_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 eps_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, uint8_t *vnd_ie)

Set vendor specific element.

Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by eps_wifi_init
  • ESP_ERR_WIFI_ARG: invalid argument
  • ESP_ERR_WIFI_NO_MEM: out of memory
Parameters
  • enable: enable or not
  • type: information element type
  • idx: information element index
  • vnd_ie: pointer to a vendor specific element

esp_err_t esp_wifi_set_vendor_ie_cb(esp_vendor_ie_cb_t cb, void *ctx)

Set vendor specific element callback.

Return
  • ESP_OK: succeed
  • ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by eps_wifi_init
Parameters
  • cb: callback function
  • ctx: reserved

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

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_enable

WiFi AMPDU feature enable flag

int nvs_enable

WiFi NVS flash enable flag

int nano_enable

Nano option for printf/scan family enable flag

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

WIFI_STATIC_TX_BUFFER_NUM
WIFI_DYNAMIC_TX_BUFFER_NUM
WIFI_AMPDU_ENABLED
WIFI_NVS_ENABLED
WIFI_NANO_FORMAT_ENABLED
WIFI_INIT_CONFIG_MAGIC
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
  • buf: Data received. Type of data in buffer (wifi_promiscuous_pkt_t or wifi_pkt_rx_ctrl_t) indicated by ‘type’ parameter.
  • type: promiscuous packet type.

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

Define function pointer for vendor specific element callback.

Parameters
  • ctx: reserved
  • type: information element type
  • sa: source address
  • vnd_ie: pointer to a vendor specific element
  • rssi: received signal strength indication