Let ESP32 connect to your WiFi hotspot

Source: Internet
Author: User
Tags idf

This project has been managed to GitHub, the specific path is Https://github.com/tidyjiang8/esp32-projects/tree/master/sta

Git plus--recursive


Function Description: Connect ESP32 as an STA to a WiFi hotspot.

"Quick Start" "Suppose" you have installed the ESP-IDF and toolchain. You already have a WiFi hotspot. "Step" uses the data cable to connect the board to your system, allowing the system to recognize your board (Windows is com\*, Linux is/dev/ttyusb\*). Enter the directory where STA is located. Perform the configuration named Make Menuconfig.
Configure the SSID and password for the hotspot. Go to configuration options in the demo config---, then fill in your SSID and password in the WiFi SSID and WiFi password. Then exit the configuration menu to save the configuration. Configure the serial port. Execute command make to compile execute command makes Flash monitor burn the generated image to the ESP32 board and view the serial output. "Phenomenon"

The serial output is shown in the figure below, and we can see that our ESP32 has successfully connected to the hotspot and obtained the IP address.

Figure-Serial output

"Source Code Analysis"

The whole source code is very simple, a total of dozens of lines, almost all in the call ESP-IDF to provide us with the interface, the process is as follows:

In this process, we need to emphasize three things: Initialize the event processing module esp_event_loop_init () set the WiFi mode Esp_wifi_set_mode () configuration Interface Parameters Esp_wifi_set_config () " Initialize the event scheduler

In ESP-IDF, the entire WiFi stack is a state machine that has a state at all times. The user can automatically handle some work in a certain state, depending on the needs of the protocol stack. When we call the Esp_event_loop_init () function, we pass in a parameter Event_handler, which is a function pointer that, when the state of the WiFi state changes, calls the function Event_handler () and passes the appropriate arguments to it. This part of the content we will be detailed in the next blog, so here is no longer explained, please refer to the deep analysis ESP32 WiFi state machine "set WiFi mode"

ESP32 supports three types of WiFi modes, which are defined as three enumeration values:

typedef enum {
    wifi_mode_null = 0,  /**< NULL mode */
    Wifi_mode_sta       ,/**< WIFI station MODE */
    W Ifi_mode_ap,        /**< wifi soft-ap mode */
    Wifi_mode_apsta,     /**< WiFi station + soft-ap mode */
    Wifi_ Mode_max
} wifi_mode_t;
1 2 3 4 5 6 7

The first enumeration value Wifi_mode_null and the last enumeration value Wifi_mode_max is just a token, not a truly supported pattern, so the supported modes are: Station mode SOFT-AP Mode station + SOFT-AP mode

The interface for the setup mode is esp_err_t Esp_wifi_set_mode (wifi_mode_t mode), and the parameters it needs are one of the three modes we see above.

There is another problem, wifi_mode_null and Wifi_mode_max. Inside the function Esp_wifi_set_mode, these two values can be used to determine whether an incoming parameter is valid. "Configure parameters for Interfaces"

When you set WiFi mode above, the WiFi library assigns the interface according to the parameters we pass in. If we pass in the parameter is Wifi_mode_sta, then the WiFi library will create an STA interface, if we pass in the parameter is Wifi_mode_ap, then the WiFi library will create an AP interface, if we passed the parameter is Wifi_mode_apsta, then wif The library creates an STA interface and an AP interface at the same time. The WiFi library needs to know the parameters of these interfaces at runtime, so we need to set the parameters of the interface before starting WiFi.

The API prototype for setting the WiFi interface parameters is esp_err_t esp_wifi_set_config (wifi_interface_t ifx, wifi_config_t *conf), with two parameters: wifi_interface_t IFX, the interface that needs to be configured wifi_config_t *conf, which is the parameter passed to the interface

The parameters required for the STA interface and the AP interface are different, and it is important to note that they are defined by a union:

typedef Union {
    wifi_ap_config_t  ap;  /**< AP Configuration */
    wifi_sta_config_t STA;/**< STA configuration */
} wifi_config_t;
1 2 3 4

For STA interfaces, the parameters that need to be configured typically include SSID and password. Note that the length of the SSID and password here is limited by, see the structure in detail:

typedef struct {
    uint8_t ssid[32];      /**< SSID of Target ap*/
    uint8_t password[64];  /**< password of target ap*/
    bool Bssid_set;        /**< whether set MAC address of target AP or not. Generally, Station_config.bssid_set needs to BES 0; And it needs to is 1 only if 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_sta_config_t;
1 2 3 4 5 6 7

For the AP interface, the first parameter to be determined is  authmode, which is the authorization mode when the client connects to the AP. If the configured authorization mode is  wifi_auth_open, you no longer need to configure additional parameters. Otherwise, you need to determine what other parameters are required according to the authorization mode, but generally you need at least  ssid  and  password  two parameters, and the function of other parameters should be researched by yourself. The configuration structure of the AP interface is as follows:

typedef struct {
    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, 60000 MS, Default-MS */
} wifi_ap_config_t;
1 2 3 4 5 6 7 8 9 10 "Summary"

Although there are only a short dozens of lines of code, but if we carefully follow the source, there is actually a lot of dry goods.

Here we are just a simple analysis of the next source, not too deep, for example, TCP/IP adaptation layer when the initialization of what is done. What does the event scheduler do? How the/wifi state machine is running is. For the latter, understand clearly help us to write a better application, we will be introduced in the next blog, please refer to the analysis of ESP32 WiFi state machine, for the former, if interested, please self-tracking source, this part of the content is open source.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.