Wi-Fi

Overview

Instructions

Application Example

Instructions

API Reference

Instructions

Macros

WIFI_INIT_CONFIG_DEFAULT { \ .event_handler = &esp_event_send, \ };

Type Definitions

typedef void (*wifi_promiscuous_cb_t)(void *buf, uint16_t len)

The RX callback function in the promiscuous mode.

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

Return
none
Parameters
  • buf: the data received
  • len: data length

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

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. Generally we should init event_q in *config, WiFi driver will post the event to this queue when event happens, such as, when station connects to WiFi, WiFi driver will post station connected event to this queue. If the queue is not initialized, WiFi will not post any events
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_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 is not initialized by eps_wifi_init
  • ESP_ERR_WIFI_NOT_START: WiFi is 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_START: WiFi is 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_get_ap_list, so generally, call esp_wifi_get_ap_list to cause the memory to be freed once the scan is done
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_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_START: 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_START: 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_START: 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
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_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