Chapter 14 ESP32 Create a New WIFI Hotspot

Pay attention to the public account of Jiayouchuang Technology

 

Learning goals and objectives

  1. Master the state machine structure of WIFI
  2. Master the procedures of Espressif's new hotspot (AP)

WiFi Features of ESP32

WiFi can be used as AP, STA, AP+STA three modes

As an AP, it can be connected by 4 STAs

AP is the abbreviation of (Wireless) AccessPoint, namely (wireless) access point. In simple terms, it is like a wireless router. After the device is turned on, it enters the AP mode. In the network list of the mobile phone, you can search for a name (SSID) similar to TPLINK_XXX.

STA is that every terminal connected to a wireless network (such as laptops, mobile phones, PADs, and other user equipment that can be networked) can be called a station. STA itself does not accept wireless access, it can connect to APs.

software design

Code logic ( from the web )

Introduction to the I2C master interface of ESP32 ( from the network )

The underlying driver of esp32wifi is not open source. The following is a flow chart of the working principle of the esp32 wifi state machine

ESP32's WIFI state machine summary

After initializing the wifi configuration, the program will return the status in the callback function according to the real-time status of the WIFI, so you only need to perform related operations in the callback, and then explain the analysis of the state machine.

State Machine Analysis

callback prototype

static esp_err_t event_handler

(

void *ctx,

system_event_t *event

)

function

WIFI state machine callback function

parameter

[in] event:

typedef struct {undefined

system_event_id_t event_id; /*Event ID*/

system_event_info_t event_info; /*Event information*/

} system_event_t;

//event ID value

typedef enum {undefined

    SYSTEM_EVENT_WIFI_READY = 0, /*WiFi is ready*/

    SYSTEM_EVENT_SCAN_DONE, /* Scan AP completed*/

    SYSTEM_EVENT_STA_START, /*Start working as STA*/

    SYSTEM_EVENT_STA_STOP, /*End work as STA*/

    SYSTEM_EVENT_STA_CONNECTED, /*Connect to AP as STA*/

    SYSTEM_EVENT_STA_DISCONNECTED, /*Disconnect AP as STA*/

    SYSTEM_EVENT_STA_AUTHMODE_CHANGE, /*AP connected as STA has changed*/

    SYSTEM_EVENT_STA_GOT_IP, /*Get IP from AP as STA*/

    SYSTEM_EVENT_STA_LOST_IP, /* Lost IP as STA, which is wrong */

    SYSTEM_EVENT_STA_WPS_ER_SUCCESS,    /*wps succeeds in enrollee mode */

    SYSTEM_EVENT_STA_WPS_ER_FAILED,    /* wps fails in enrollee mode */

    SYSTEM_EVENT_STA_WPS_ER_TIMEOUT,    /* wps timeout in enrollee mode */

    SYSTEM_EVENT_STA_WPS_ER_PIN,        /*wps pin code in enrollee mode */

    SYSTEM_EVENT_AP_START, /*AP start*/

    SYSTEM_EVENT_AP_STOP, /*AP end*/

    SYSTEM_EVENT_AP_STACONNECTED, /* STA connected to SP32 AP*/

    SYSTEM_EVENT_AP_STADISCONNECTED, /*A STA disconnects the AP of ESP32*/

    SYSTEM_EVENT_AP_PROBEREQRECVED, /*Receive packets from AP*/

    SYSTEM_EVENT_GOT_IP6, /*Get IPv6*/

    SYSTEM_EVENT_ETH_START, /*wired start*/

    SYSTEM_EVENT_ETH_STOP, /*Wired stop*/

    SYSTEM_EVENT_ETH_CONNECTED, /*wired online*/

    SYSTEM_EVENT_ETH_DISCONNECTED, /*Wired disconnected*/

    SYSTEM_EVENT_ETH_GOT_IP, /*Wired got IP*/

    SYSTEM_EVENT_MAX

} system_event_id_t;

 

return value

ESP_OK: success

ESP_ERR_INVALID_ARG : parameter error

 

WiFi initialization API introduction

Set the WiFi callback function esp_event_loop_init();

callback prototype

esp_err_t esp_event_loop_init

(

 

)

function

Set WiFi callback function

parameter

[in] cb: callback function pointer

[in] ctx: reserved

return value

ESP_OK: success

other: failed

Initialize WiFi setting function WIFI_INIT_CONFIG_DEFAULT();

 

Set WiFi working mode function esp_err_t esp_wifi_set_mode();

callback prototype

esp_err_t esp_wifi_set_mode

(

wifi_mode_t mode

)

function

Set WiFi working mode function

parameter

[in] mode: wifi working mode

 

return value

ESP_OK: success

other: failed

Start WiFi function: esp_err_t esp_wifi_start(void);

For more detailed interfaces, please refer to the official guide .

WiFi initialization writing

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

void app_main()

{    

    ESP_ERROR_CHECK( nvs_flash_init() );

    tcpip_adapter_init();

    ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));

 

    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();

    ESP_ERROR_CHECK(esp_wifi_init(&cfg));

    wifi_config_t wifi_config = {undefined

        .ap = {undefined

            .ssid = "Helon_test",

            .ssid_len = 0,

            /* Can only be connected by up to 4 stations at the same time, here is set to only be connected by one station*/

            .max_connection = 1,

            .password = "20180604",

            .authmode = WIFI_AUTH_WPA_WPA2_PSK,

        },

    };    

    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));

    ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config));

    ESP_ERROR_CHECK(esp_wifi_start());    

}

Analysis as a callback function of AP

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

static esp_err_t event_handler(void *ctx, system_event_t *event)

{undefined

    switch (event->event_id)

    {undefined

    case SYSTEM_EVENT_AP_START:

        printf("\nwifi_softap_start\n");

        break;

    case SYSTEM_EVENT_AP_STACONNECTED:

        printf("\nwifi_softap_connectted\n");

        break;

    case SYSTEM_EVENT_AP_STADISCONNECTED:

        printf("\nwifi_softap_disconnectted\n");

        break;

    default:

        break;

    }

    return ESP_OK;

}

Show results

WiFi Summary

Click me -> more ESP32 development guide series catalog

Related: Chapter 14 ESP32 Create a New WIFI Hotspot