1. 完成了ota功能的基本测试,测试通过
2. 封装了一个模板线程类,支持创建来自单例类的成员函数线程,普通类的线程,普通函数线程 3. 封装了一个Wifi模块类,支持Wifi的各种基本配置
This commit is contained in:
+112
-35
@@ -14,46 +14,41 @@ ble_device_info_t *ble_device_list = NULL;
|
||||
void Wireless_Init(void)
|
||||
{
|
||||
// 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_err_t ret = nvs_flash_init(); // 初始化Flash
|
||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { // 如果Flash有错误,则进行修复
|
||||
ESP_ERROR_CHECK(nvs_flash_erase()); // 清空Flash
|
||||
ret = nvs_flash_init(); // 重新初始化Flash
|
||||
}
|
||||
ESP_ERROR_CHECK( ret );
|
||||
// WiFi
|
||||
xTaskCreatePinnedToCore(
|
||||
WIFI_Init,
|
||||
"WIFI task",
|
||||
4096,
|
||||
NULL,
|
||||
1,
|
||||
NULL,
|
||||
0);
|
||||
// BLE
|
||||
xTaskCreatePinnedToCore(
|
||||
BLE_Init,
|
||||
"BLE task",
|
||||
4096,
|
||||
NULL,
|
||||
2,
|
||||
NULL,
|
||||
0);
|
||||
ESP_ERROR_CHECK( ret ); // 检查错误
|
||||
// WiFi 手动调用Wifi初始化
|
||||
// xTaskCreatePinnedToCore(
|
||||
// WIFI_Init,
|
||||
// "WIFI task",
|
||||
// 4096,
|
||||
// NULL,
|
||||
// 1,
|
||||
// NULL,
|
||||
// 0);
|
||||
// BLE 当前工程不启用蓝牙
|
||||
// xTaskCreatePinnedToCore(
|
||||
// BLE_Init,
|
||||
// "BLE task",
|
||||
// 4096,
|
||||
// NULL,
|
||||
// 2,
|
||||
// NULL,
|
||||
// 0);
|
||||
}
|
||||
|
||||
void WIFI_Init(void *arg)
|
||||
{
|
||||
esp_netif_init();
|
||||
esp_event_loop_create_default();
|
||||
esp_netif_create_default_wifi_sta();
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
esp_wifi_init(&cfg);
|
||||
esp_wifi_set_mode(WIFI_MODE_STA);
|
||||
esp_wifi_start();
|
||||
|
||||
WIFI_NUM = WIFI_Scan();
|
||||
printf("WIFI:%d\r\n",WIFI_NUM);
|
||||
|
||||
vTaskDelete(NULL);
|
||||
esp_netif_init(); // 初始化网络接口
|
||||
esp_event_loop_create_default(); // 创建事件循环
|
||||
esp_netif_create_default_wifi_sta(); // 创建默认的WiFi Station
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); // 创建WiFi初始化配置(使用默认配置)
|
||||
esp_wifi_init(&cfg); // 初始化WiFi
|
||||
esp_wifi_set_mode(WIFI_MODE_STA); // 设置WiFi模式为Station
|
||||
esp_wifi_start(); // 启动WiFi
|
||||
}
|
||||
|
||||
|
||||
@@ -269,6 +264,88 @@ uint16_t BLE_Scan(void)
|
||||
return BLE_NUM;
|
||||
}
|
||||
|
||||
|
||||
// 下面是新增的实现
|
||||
static EventGroupHandle_t s_wifi_event_group;
|
||||
#define WIFI_CONNECTED_BIT BIT0
|
||||
#define WIFI_FAIL_BIT BIT1
|
||||
|
||||
static const char *TAG = "WiFi_UTIL";
|
||||
|
||||
/* --------------------------- 事件回调 --------------------------- */
|
||||
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_DISCONNECTED) {
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
|
||||
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
|
||||
}
|
||||
}
|
||||
|
||||
/* --------------------------- 自动连接 --------------------------- */
|
||||
bool WiFi_AutoConnect(const char *ssid, const char *password, uint8_t max_retry)
|
||||
{
|
||||
if (s_wifi_event_group == NULL) {
|
||||
s_wifi_event_group = xEventGroupCreate();
|
||||
}
|
||||
|
||||
/* 注册一次性事件监听 */
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
|
||||
WIFI_EVENT_STA_DISCONNECTED,
|
||||
&event_handler,
|
||||
NULL,
|
||||
NULL));
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
|
||||
IP_EVENT_STA_GOT_IP,
|
||||
&event_handler,
|
||||
NULL,
|
||||
NULL));
|
||||
|
||||
wifi_config_t wifi_config = { 0 };
|
||||
strncpy((char *)wifi_config.sta.ssid, ssid, sizeof(wifi_config.sta.ssid));
|
||||
strncpy((char *)wifi_config.sta.password, password, sizeof(wifi_config.sta.password));
|
||||
wifi_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
|
||||
|
||||
uint8_t retry_cnt = 0;
|
||||
while (retry_cnt <= max_retry) {
|
||||
ESP_LOGI(TAG, "WiFi connect attempt %u/%u to \"%s\"", retry_cnt + 1, max_retry + 1, ssid);
|
||||
|
||||
xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT | WIFI_FAIL_BIT);
|
||||
esp_wifi_connect();
|
||||
|
||||
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
|
||||
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
|
||||
pdFALSE, pdFALSE,
|
||||
pdMS_TO_TICKS(6000)); /* 6 秒超时 */
|
||||
|
||||
if (bits & WIFI_CONNECTED_BIT) {
|
||||
ESP_LOGI(TAG, "WiFi connected");
|
||||
goto success;
|
||||
}
|
||||
|
||||
/* 失败,准备重试 */
|
||||
retry_cnt++;
|
||||
}
|
||||
|
||||
ESP_LOGE(TAG, "WiFi connect failed after %u retries", max_retry + 1);
|
||||
return false;
|
||||
|
||||
success:
|
||||
return true;
|
||||
}
|
||||
|
||||
/* --------------------------- 断开连接 --------------------------- */
|
||||
esp_err_t WiFi_Disconnect(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "Disconnecting WiFi...");
|
||||
return esp_wifi_disconnect();
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 获取WiFi AP列表
|
||||
uint16_t wireless_get_wifi_ap_list(wifi_ap_info_t *ap_list, uint16_t max_aps) {
|
||||
if (!wifi_ap_list || wifi_ap_count == 0) {
|
||||
|
||||
Reference in New Issue
Block a user