ESP32使用sta模式连接wifi热点

ESP32 WIFI功能支持STA、AP、APSTA三种模式,本文主要讲讲在STA模式下,连接到电脑或手机的移动热点(详细代码请点击下载,代码在ESP32S3上测试通过):

1、创建wifi事件组

s_wifi_event_group = xEventGroupCreate();

2、Wi-Fi/LwIP 初始化,创建LWIP核心任务

ESP_ERROR_CHECK(esp_netif_init());

3、创建系统事件任务,并初始化应用程序事件的回调函数

ESP_ERROR_CHECK(esp_event_loop_create_default());

4、创建有 TCP/IP 堆栈的默认网络接口实例绑定 station

esp_netif_create_default_wifi_sta();

5、由于wifi连接选项设置了使用nvs,会把每次配置的参数存储在nvs中。因此需要设置nvs分区

nvs_flash_init();

6、创建wifi驱动程序任务,并初始化wifi驱动程序

wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));

7、注册用于处理wifi连接的过程中的事件

esp_event_handler_instance_t instance_any_id;   // 用于处理wifi连接时候的事件的句柄
esp_event_handler_instance_t instance_got_ip;   // 用于处理ip分配时候产生的事件的句柄
// 该句柄对wifi连接所有事件都产生响应,连接到event_handler回调函数
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
                                                        ESP_EVENT_ANY_ID,
                                                        &event_handler,
                                                        NULL,
                                                        &instance_any_id));
// 该句柄仅仅处理IP_EVENT事件组中的从AP中获取ip地址事件,连接到event_handler回调函数
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
                                                        IP_EVENT_STA_GOT_IP,
                                                        &event_handler,
                                                        NULL,
                                                        &instance_got_ip));

8、WIFI配置

wifi_config_t wifi_config = {
        .sta = {
            .ssid = EXAMPLE_ESP_WIFI_SSID,
            .password = EXAMPLE_ESP_WIFI_PASS,
            /* Setting a password implies station will connect to all security modes including WEP/WPA.
             * However these modes are deprecated and not advisable to be used. Incase your Access point
             * doesn't support WPA2, these mode can be enabled by commenting below line */
	     .threshold.authmode = WIFI_AUTH_WPA2_PSK,  // 设置快速扫描模式下能接受的最弱的验证模式
	     .sae_pwe_h2e = WPA3_SAE_PWE_BOTH,          // 设置SAE和PWE(wifi协议)的配置
        },
    };

// 2 配置station工作模式
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );

// 3 配置
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );

9、启动WIFI

    ESP_ERROR_CHECK(esp_wifi_start() );     // 会触发回调函数

    ESP_LOGI(TAG, "wifi_init_sta finished.");

    /* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
     * number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
    /******************** 输出wifi连接结果 ********************/
    EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
            WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
            pdFALSE,
            pdFALSE,
            portMAX_DELAY);

    /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
     * happened. */
    if (bits & WIFI_CONNECTED_BIT)
    {
        ESP_LOGI(TAG, "connected to ap SSID:%s password:%s",
                 EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
    }
    else if (bits & WIFI_FAIL_BIT)
    {
        ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",
                 EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
    }
    else
    {
        ESP_LOGE(TAG, "UNEXPECTED EVENT");
    }

10、在app_main函数中调用wifi初始化函数

wifi_init();

11、将程序烧录至ESP32芯片

12、启动芯片,热点显示连接结果

ESP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0x1 (POWERON),boot:0x8 (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fce3820,len:0x10a0
load:0x403c9700,len:0x4
load:0x403c9704,len:0xc80
load:0x403cc700,len:0x31e4
entry 0x403c9920
Hello world!
This is esp32s3 chip with 2 CPU core(s), WiFi/BLE, silicon revision v0.2, 16MB external flash
W (439) image wifi station: wifi init...
W (481) image wifi station: wifi_init_sta finished.
W (1502) image wifi station: got ip:192.168.137.124
free heap size: 7806020 bytes, minimum free heap size: 7803416 bytes
internal free heap size: 161135 bytes

 完整代码请点击下载 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
ESP32可以同时运行在STAStation)模式和AP(Access Point)模式下,实现STA和AP的共存。以下是一种实现方式: 1. 首先,你需要在ESP32上初始化WiFi模块,并分别设置STA和AP的参数。 ```cpp #include <WiFi.h> void setup() { // 初始化串口和WiFi模块 Serial.begin(115200); WiFi.mode(WIFI_MODE_STA); // 设置为STA模式 // 连接WiFi路由器 WiFi.begin("your_ssid", "your_password"); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } // 设置AP模式的参数 WiFi.softAP("your_ap_ssid", "your_ap_password"); } void loop() { // 在这里可以添加其他代码 } ``` 在上述代码中,你需要将`"your_ssid"`和`"your_password"`替换为你的WiFi路由器的名称和密码,将`"your_ap_ssid"`和`"your_ap_password"`替换为你希望设置的AP的名称和密码。 2. 在ESP32上同时运行STA和AP模式后,你可以通过STA模式连接到一个WiFi路由器,并且其他设备可以通过AP模式连接ESP32。这样,ESP32即可以作为一个WiFi客户端连接网络,又可以作为一个WiFi热点提供网络给其他设备连接。 请注意,ESP32STA和AP之间共享同一个物理无线接口,因此在同时使用STA和AP模式时,网络性能可能会受到影响。同时,ESP32的资源有限,如果同时处理大量的STA和AP连接请求,可能会导致性能下降。因此,在实际应用中,需要根据具体需求和设备能力进行合理的配置和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jackiendsc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值