Deep analysis of ESP32 's WiFi state machine

Source: Internet
Author: User
Tags auth idf bssid

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


In the previous blog "Let ESP32 connect to your WiFi hotspot", we have simply analyzed the WiFi workflow and briefly prompted the event scheduler/wifi state machine, which we will analyze in detail in this blog post.

In ESP-IDF, the entire WiFi stack is a state machine that has a state at all times. Users can automatically handle certain tasks when they run to a state, depending on their needs. Understanding the entire WiFi state machine is good for us to write a better application, the most basic function is "network disconnection", which has been implemented in our STA project, please refer to the source code. "Status definition of the protocol stack"

In ESP-IDF, the entire network protocol stack contains state definitions in the header file components/esp32/include/esp_event.h, defined by the enumeration type system_event_id_t:

typedef enum {system_event_wifi_ready = 0,/**< ESP32 Wi-Fi Ready */System_event_scan_done, /**< ESP32 Complete Scan AP */System_event_sta_start,/**< ESP32 STA start */system_event_sta_stop ,/**< ESP32 STA stop */system_event_sta_connected,/**< ESP32 STA connected to AP */syste m_event_sta_disconnected,/**< ESP32 STA disconnects from AP */System_event_sta_authmode_change,/**< ESP32 The authorization mode of the AP to which the STA is connected has changed */system_event_sta_got_ip,/**< ESP32 STA obtains the IP address from the AP */System_event_st A_wps_er_success,/**< ESP32 STA WPS succeeds in enrollee mode */system_event_sta_wps_er_failed,/**& Lt ESP32 STA wps fails in enrollee mode */system_event_sta_wps_er_timeout,/**< ESP32 STA wps TIMEOUT in Enroll EE mode */system_event_sta_wps_er_pin,/**< ESP32 STA WPS PIN code in enrollee mode */System_event_           Ap_start,      /**< ESP32 soft-ap Start */system_event_ap_stop,/**< ESP32 soft-ap stop */SYSTEM_EVENT_AP _staconnected,/**< has STA connected to ESP32 SOFT-AP */system_event_ap_stadisconnected,/**< has STA from ESP3 2 soft-ap Disconnect */system_event_ap_probereqrecved,/**< SOFT-AP interface receives probe request message */SYSTEM_EVENT_AP_STA_GOT_IP6 ,/**< ESP32 Sta/ap interface gets to IPV6 address */System_event_eth_start,/**< ESP32 Ethernet START */system_event_eth_stop,/**< ESP32 Ethernet STOP */system_event_eth_connected,/*
    *< ESP32 Ethernet PHY link up */system_event_eth_disconnected,/**< ESP32 Ethernet PHY link down */ SYSTEM_EVENT_ETH_GOT_IP,/**< ESP32 Ethernet GOT IP from connected AP */System_event_max} system_e vent_id_t;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 "View the various states that ESP32 experience when connected to an AP"

The ESP32 log default level is INFO, that is, only messages with levels greater than or equal to the info level will be printed to the serial port, and we want to see each state in the WiFi connection process and need to modify the print level of the log, which is done in the configuration menu.

Run the command make Menuconfig into the graphical configuration menu, then select Component Config--->, Log output--->, Default log verbosity (Info)---, and then choose Choose a print level of debug:

The lowest level of the log is obviously Verbose, and the level we choose is Debug. Please continue to see the subsequent decomposition ^_^

After selecting the log level, exit the configuration screen, save the configuration, and then execute the command make Flash monitor recompile, write the program, and view the serial output.

To avoid the article being too lengthy, a portion of the WiFi-related logs are intercepted below:

I (720) wifi:init dynamic TX buffer num:32 I (720) wifi:init Dynamic RX buffer num:64 I (720) Wifi:wifi driver task: 3ffbd668, prio:23, stack:3584 I (730) wifi:init static RX buffer num:10 I (730) wifi:init Dynamic RX Buffer num:64 I ( 740) wifi:init Rx AMPDU len Mblock:7 I (740) wifi:init lldesc Rx AMPDU Entry mblock:4 I (740) Wifi:wifi Power Manager t  Ask:0x3ffc2a30 prio:21 stack:2560 I (I) Wifi:wifi timer task:3ffc3ab0, prio:22, stack:3584 I (810) Wifi:mode:sta
(30:ae:a4:04:80:84)
D (810) Event:system_event_sta_start I (840) app_sta:connecting to AP ... I (960) Wifi:n:1 0, o:1 0, ap:255 255, sta:1 0, Prof:1 I (1940) Wifi:state:init, auth (B0) I (1950) wifi:state:a Uth, Assoc (0) I (1960) Wifi:state:assoc, run (Ten) I (1990) wifi:connected with Test, Channel 1 D (1990) Even t:system_event_sta_connected, Ssid:test, Ssid_len:4, bssid:d0:5b:a8:c2:91:7e, Channel:1, Authmode:3 D (2324) EVENT: System_event_sta_gotip, ip:192.168.1.120, mask:255.255.255.0, gw:192.168.1.1 I (2324) event:ip:192.168.1.120, mask:255.255.255.0, gw:192.168.1.1 I (2324) app_sta:connected
. I (9964) Wifi:state:run, auth (3a0) I (9964) wifi:n:1 0, o:1 0, ap:255 255, sta:1 0, Prof:1 D (9964) Event:system _event_sta_disconnected, Ssid:test, Ssid_len:4, bssid:d0:5b:a8:c2:91:7e, Reason:3 I (9994) App_sta:wifi DISCONNECTED,
Try to connect ... I (18164) wifi:n:11 0, o:1 0, ap:255 255, sta:11 0, prof:1 I (18174) Wifi:state:auth, auth (b0) I (18174) wifi:st Ate:auth, Assoc (0) I (18184) Wifi:state:assoc, run (Ten) I (18224) wifi:connected with Test, Channel One D (1 8234) event:system_event_sta_connected, Ssid:test, Ssid_len:4, bssid:d0:5b:a8:c2:91:7e, Channel:11, Authmode:3 D ( 18894) Event:system_event_sta_gotip, ip:192.168.1.120, mask:255.255.255.0, gw:192.168.1.1 I (18894) Event:ip:
192.168.1.120, mask:255.255.255.0, gw:192.168.1.1 I (18894) app_sta:connected.

 I (28184) wifi:pm start, type:0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

Note that in order to see more status, I closed the WiFi hotspot once during the connection and then opened the hotspot.

Looking closely at the logs, you can see the following logs related to state changes: Event:system_event_sta_start event:system_event_s

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.