目录
二、实现原理 espidf框架实现wifi连接可以参考官方的代码,官方提供了多个示例来应对不同的场景。
一、前言
在之前的工作中,我们已经实现了一些lvgl界面上的不同页面的切换,其中一个重要的页面/功能即wifi连接,这里简单复述一下wifi连接的方法。
二、实现原理
espidf框架实现wifi连接可以参考官方的代码,官方提供了多个示例来应对不同的场景。

这里我们是需要让esp32连接到实验室的局域网配合后续的ros应用,因此使用STA模式即可,也就是getting_started目录下的station示例。
二、实现代码
这里我把官方的代码稍微进行了一下整理(其实里面很多代码的作用不是很清楚,但是反正复制粘贴就完了),大致分为三个函数:
- wifiInit()——初始化wifi功能,应用放在esp32主程序的各模块初始化阶段
- wifiStart()——启动wifi连接,应用在界面的启动wifi按钮触发事件内
- event_handler()——wifi功能的触发事件函数,照抄官方文档示例😁
2.1 wifi.h
/**
* @file wifi.h
* @brief
* @author Zhang Xu (tju_zhangxu@tju.edu.cn)
* @version 0.1
* @date 2022-09-07
*
* Copyright (c) 2022 天津大学先进机构学及机器人学中心
*
* REFERENCE
* https://github.com/espressif/esp-idf/blob/v4.4.2/examples/wifi/getting_started/station/main/station_example_main.c
* https://docs.espressif.com/projects/esp-idf/zh_CN/v4.4.2/esp32/api-reference/network/esp_wifi.html
*/
#ifndef WIFI_H
#define WIFI_H
/*********************
* INCLUDES
*********************/
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "esp_wifi.h"
#include "esp_log.h"
#include "esp_event.h"
#include "nvs_flash.h"
/*********************
* DEFINES
*********************/
#define DEFAULT_SCAN_LIST_SIZE 20 //wifi列表大小
#define LAN_SSID "LAB121_NOKOV"
#define LAN_PASSWORD "123456789"
#define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT BIT1
#define ESP_MAXIMUM_RETRY 5
/**********************
* TYPEDEFS
**********************/
/**********************
* GLOBAL PROTOTYPES
**********************/
bool isConnectedLAN;
uint16_t wifi_ip_addr[4];
/**
* @brief wifi初始化设置
*
*/
void wifiInit();
/**
* @brief 尝试连接目标wifi
*
* @return true-连接成功
* @return false-连接失败
*/
bool wifiStart();
/**
* @brief 关闭wifi
*
*/
void wifiClose();
/**********************
* MACROS
**********************/
#endif
2.2 wifi.c
/**
* @file wifi.c
* @brief
* @author Zhang Xu (tju_zhangxu@tju.edu.cn)
* @version 0.1
* @date 2022-09-07
*
* Copyright (c) 2022 天津大学先进机构学及机器人学中心
*
* REFERENCE
*/
/*********************
* INCLUDES
*********************/
#include "wifi.h"
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
/**
* @brief 事件循环handler函数
*
* @param arg
* @param event_base
* @param event_id
* @param event_data
*/
static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data);
/**********************
* STATIC VARIABLES
**********************/
static const char *TAG = "WIFI";
/* FreeRTOS event group to signal when we are connected*/
static EventGroupHandle_t s_wifi_event_group;
static int s_retry_num = 0;
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
void wifiInit(){
//Initialize 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);
s_wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_create_default_wifi_sta();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
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,
ESP_EVENT_ANY_ID,
&event_handler,
NULL,
&instance_any_id));
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
IP_EVENT_STA_GOT_IP,
&event_handler,
NULL,
&instance_got_ip));
wifi_config_t wifi_config = {
.sta = {
.ssid = LAN_SSID,
.password = LAN_PASSWORD,
/* 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_OPEN,
},
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
}
bool wifiStart(){
isConnectedLAN = false;
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) */
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",
LAN_SSID, LAN_PASSWORD );
isConnectedLAN = true;
return true;
} else if (bits & WIFI_FAIL_BIT) {
ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",
LAN_SSID, LAN_PASSWORD );
} else {
ESP_LOGE(TAG, "UNEXPECTED EVENT");
}
return false;
}
void wifiClose(){
esp_wifi_stop();
isConnectedLAN = false;
}
/**********************
* STATIC FUNCTIONS
**********************/
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){
esp_wifi_connect();
}
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED){
if (s_retry_num < ESP_MAXIMUM_RETRY){
esp_wifi_connect();
s_retry_num++;
ESP_LOGI(TAG, "retry to connect to the AP");
}
else {
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
}
ESP_LOGI(TAG,"connect to the AP fail");
}
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_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));
wifi_ip_addr[0] = esp_ip4_addr1_16(&event->ip_info.ip);
wifi_ip_addr[1] = esp_ip4_addr2_16(&event->ip_info.ip);
wifi_ip_addr[2] = esp_ip4_addr3_16(&event->ip_info.ip);
wifi_ip_addr[3] = esp_ip4_addr4_16(&event->ip_info.ip);
s_retry_num = 0;
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
}
}
参考文献
esp-idf/station_example_main.c at v4.4.2 · espressif/esp-idf (github.com)
https://docs.espressif.com/projects/esp-idf/zh_CN/v4.4.2/esp32/api-reference/network/esp_wifi.html