202-ESP32_SDK開發-station模式配置模組連接路由器熱點


<p><iframe name="ifd" src="https://mnifdv.cn/resource/cnblogs/LearnESP32" frameborder="0" scrolling="auto" width="100%" height="1500"></iframe></p>

 

官方提供的例程在這里

 

 

 

 

 

 

 

 

 

 

配置模塊

連接名稱為  QQQQQQ  密碼為  11223344 的熱點

#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "nvs_flash.h"
#include "esp_log.h"


#define EXAMPLE_ESP_WIFI_SSID      "QQQQQQ"
#define EXAMPLE_ESP_WIFI_PASS      "11223344"


static const char *TAG = "wifi station";


static void event_handler(void* arg, esp_event_base_t event_base,
                                int32_t event_id, void* event_data)
{
    if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {//配置好了wifi的STA模式
        esp_wifi_connect();//連接熱點
    } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {//和路由器斷開
        esp_wifi_connect();//連接熱點
        ESP_LOGI(TAG,"connect to the AP fail");
    } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {//連接上路由器(獲取到了分配的IP地址)
        ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
        ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
    }
}

void wifi_init_sta(void)
{
    ESP_ERROR_CHECK(esp_netif_init());//初始化內部的lwip

    ESP_ERROR_CHECK(esp_event_loop_create_default());//創建系統事件任務
    esp_netif_create_default_wifi_sta();//創建有 TCP/IP 堆棧的默認網絡接口實例綁定 station 或 AP。

    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));//創建 Wi-Fi 驅動程序任務,並初始化 Wi-Fi 驅動程序。

    esp_event_handler_instance_t instance_any_id;
    esp_event_handler_instance_t instance_got_ip;
    /*注冊系統事件回調函數*/
    ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,         //wifi狀態改變事件
                                                        ESP_EVENT_ANY_ID,
                                                        &event_handler,
                                                        NULL,
                                                        &instance_any_id));

    /*注冊系統事件回調函數*/
    ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,            //IP地址改變事件
                                                        IP_EVENT_STA_GOT_IP,
                                                        &event_handler,
                                                        NULL,
                                                        &instance_got_ip));

    /*配置連接的熱點參數*/
    wifi_config_t wifi_config = {
        .sta = {
            .ssid = EXAMPLE_ESP_WIFI_SSID,
            .password = EXAMPLE_ESP_WIFI_PASS,
            .threshold.authmode = WIFI_AUTH_WPA2_PSK,//加密方式
            /*配置pmf,當前最新加密技術*/
            .pmf_cfg = {
                .capable = true,  //告訴熱點這邊有能力使用PMF進行加密通信(防止竊聽密碼)
                .required = false //告訴熱點這邊不強制要求使用PMF進行加密通信(防止竊聽密碼)
            },
        },
    };
    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );//設置STA模式
    ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );//配置STA參數
    ESP_ERROR_CHECK(esp_wifi_start() );//啟動


    /* 取消注冊事件回調 */
    //ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip));
    //ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id));
}

void app_main(void)
{
    //初始化 NVS(配置WiFi的參數存儲需要用到NVS)
    esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
      ESP_ERROR_CHECK(nvs_flash_erase());
      ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK(ret);

    ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
    wifi_init_sta();
}

 

下面這個地方是標准的流程

 

 

https://docs.espressif.com/projects/esp-idf/zh_CN/latest/esp32/api-guides/wifi.html?highlight=esp_netif_init

 

 

 

 

 

測試

改為自己的路由器名稱和密碼

 

 

下載到開發板測試

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM