写一个Lvgl搜索wifi并显示可连接的wifi的代码
时间: 2023-04-08 08:02:04 浏览: 623
esp32S3 lvgl驱动驱动st7789 进行显示ntp实时更新的时间
5星 · 资源好评率100%
好的,以下是Lvgl搜索wifi并显示可连接的wifi的代码:
```c
#include "lvgl/lvgl.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#define WIFI_LIST_NUM 10
static const char *TAG = "wifi_scan";
static lv_obj_t *wifi_list;
static lv_obj_t *wifi_list_label[WIFI_LIST_NUM];
static void wifi_scan_task(void *pvParameter)
{
uint16_t ap_num = 0;
wifi_ap_record_t ap_info[WIFI_LIST_NUM];
memset(ap_info, 0, sizeof(ap_info));
while (1)
{
esp_wifi_scan_start(NULL, true);
esp_wifi_scan_get_ap_records(&ap_num, ap_info);
for (int i = 0; i < WIFI_LIST_NUM; i++)
{
if (ap_info[i].ssid[0] != 0)
{
lv_label_set_text(wifi_list_label[i], (const char *)ap_info[i].ssid);
}
else
{
lv_label_set_text(wifi_list_label[i], "");
}
}
vTaskDelay(5000 / portTICK_PERIOD_MS);
}
}
void lv_ex_wifi(void)
{
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
esp_wifi_init(&cfg);
esp_wifi_set_storage(WIFI_STORAGE_RAM);
esp_wifi_set_mode(WIFI_MODE_STA);
wifi_config_t wifi_config = {
.sta = {
.ssid = "your_wifi_ssid",
.password = "your_wifi_password",
},
};
esp_wifi_set_config(WIFI_IF_STA, &wifi_config);
esp_wifi_start();
lv_obj_t *scr = lv_obj_create(NULL, NULL);
lv_scr_load(scr);
wifi_list = lv_list_create(lv_scr_act(), NULL);
lv_obj_set_size(wifi_list, LV_HOR_RES, LV_VER_RES);
lv_list_set_scrollbar_mode(wifi_list, LV_SCROLLBAR_MODE_OFF);
lv_list_set_style(wifi_list, LV_LIST_STYLE_BG, &lv_style_transp_tight);
lv_list_set_style(wifi_list, LV_LIST_STYLE_SCRL, &lv_style_transp_tight);
lv_list_set_style(wifi_list, LV_LIST_STYLE_BTN_REL, &lv_style_transp_tight);
lv_list_set_style(wifi_list, LV_LIST_STYLE_BTN_PR, &lv_style_transp_tight);
lv_list_set_style(wifi_list, LV_LIST_STYLE_BTN_TGL_REL, &lv_style_transp_tight);
lv_list_set_style(wifi_list, LV_LIST_STYLE_BTN_TGL_PR, &lv_style_transp_tight);
for (int i = 0; i < WIFI_LIST_NUM; i++)
{
wifi_list_label[i] = lv_list_add_btn(wifi_list, NULL, "");
lv_obj_set_style(wifi_list_label[i], &lv_style_transp_tight);
}
xTaskCreate(&wifi_scan_task, "wifi_scan_task", 4096, NULL, 5, NULL);
}
```
希望这个代码能够帮到你!
阅读全文