Skip to content

Commit 6d6a4c8

Browse files
committed
add WiFi List application
1 parent 8647627 commit 6d6a4c8

File tree

6 files changed

+204
-0
lines changed

6 files changed

+204
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ pushd apps/tic_tac_toe
1515
idf.py build
1616
esptool.py --chip esp32s3 --baud 921600 --before default_reset --after hard_reset write_flash 0x220000 build/tic_tac_toe.bin
1717
popd
18+
pushd apps/wifi_list
19+
esptool.py --chip esp32s3 --baud 921600 --before default_reset --after hard_reset write_flash 0x4E0000 build/wifi_list.bin
20+
popd
1821
```
1922

2023
## Build

apps/wifi_list/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# The following five lines of boilerplate have to be in your project's
2+
# CMakeLists in this exact order for cmake to work correctly
3+
cmake_minimum_required(VERSION 3.16)
4+
5+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
6+
project(wifi_list)

apps/wifi_list/main/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
idf_component_register(SRCS "wifi_list.c"
2+
INCLUDE_DIRS "."
3+
REQUIRES app_update esp_wifi nvs_flash)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## IDF Component Manager Manifest File
2+
dependencies:
3+
espressif/esp-box: "^3.1.0"
4+
#espressif/esp-box-3: "^1.2.0"
5+
# Workaround for i2c: CONFLICT! driver_ng is not allowed to be used with this old driver
6+
esp_codec_dev:
7+
public: true
8+
version: "==1.1.0"
9+
## Required IDF version
10+
idf:
11+
version: ">=5.0.0"

apps/wifi_list/main/wifi_list.c

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
#include "freertos/FreeRTOS.h"
2+
#include "freertos/task.h"
3+
#include "freertos/semphr.h"
4+
#include "esp_system.h"
5+
#include "esp_log.h"
6+
#include "lvgl.h"
7+
#include "bsp/esp-bsp.h"
8+
#include "esp_wifi.h"
9+
#include "esp_event.h"
10+
#include "nvs_flash.h"
11+
#include "esp_ota_ops.h"
12+
13+
#define TAG "WiFiList"
14+
#define DEFAULT_SCAN_LIST_SIZE 32
15+
16+
static lv_obj_t *list;
17+
static bool scan_in_progress = false;
18+
static lv_obj_t *search_msg_box = NULL;
19+
20+
static void list_wifi();
21+
static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data);
22+
23+
static void close_message_box() {
24+
if (search_msg_box) {
25+
bsp_display_lock(0);
26+
lv_msgbox_close(search_msg_box);
27+
bsp_display_unlock();
28+
search_msg_box = NULL;
29+
}
30+
}
31+
32+
static void show_message(const char *text) {
33+
ESP_LOGI(TAG, "%s", text);
34+
bsp_display_lock(0);
35+
search_msg_box = lv_msgbox_create(NULL);
36+
lv_msgbox_add_text(search_msg_box, text);
37+
lv_obj_center(search_msg_box);
38+
bsp_display_unlock();
39+
}
40+
41+
void list_wifi() {
42+
if (scan_in_progress) {
43+
ESP_LOGW(TAG, "Scan already in progress");
44+
return;
45+
}
46+
47+
wifi_scan_config_t scan_config = {
48+
.ssid = NULL,
49+
.bssid = NULL,
50+
.channel = 0,
51+
.show_hidden = false, // Disable hidden networks
52+
.scan_type = WIFI_SCAN_TYPE_ACTIVE,
53+
.scan_time = { .active = { .min = 500, .max = 1500 } } // Increased scan times
54+
};
55+
56+
esp_err_t err = esp_wifi_scan_start(&scan_config, false);
57+
if (err != ESP_OK) {
58+
ESP_LOGE(TAG, "Failed to start WiFi scan: %s", esp_err_to_name(err));
59+
return;
60+
}
61+
62+
scan_in_progress = true;
63+
}
64+
65+
void handle_scan_done() {
66+
uint16_t ap_count = 0;
67+
68+
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count));
69+
printf("Found %d access points\n", ap_count);
70+
71+
wifi_ap_record_t *ap_records = (wifi_ap_record_t *)malloc(sizeof(wifi_ap_record_t) * ap_count);
72+
if (ap_records == NULL) {
73+
ESP_LOGE(TAG, "Failed to allocate memory for AP records");
74+
scan_in_progress = false;
75+
return;
76+
}
77+
78+
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&ap_count, ap_records));
79+
printf("Scan done\n");
80+
81+
bsp_display_lock(0);
82+
// Clear the current list items
83+
while (lv_obj_get_child_cnt(list) > 0) {
84+
lv_obj_del(lv_obj_get_child(list, 0));
85+
}
86+
87+
// Add new list items
88+
for (int i = 0; i < ap_count; i++) {
89+
char buffer[128];
90+
snprintf(buffer, sizeof(buffer), "%s (%d)", ap_records[i].ssid, ap_records[i].rssi);
91+
lv_list_add_text(list, buffer);
92+
}
93+
bsp_display_unlock();
94+
95+
free(ap_records);
96+
scan_in_progress = false;
97+
98+
close_message_box(); // Close the "Searching..." message box after scan is done
99+
}
100+
101+
void reset_to_factory_app() {
102+
// Get the partition structure for the factory partition
103+
const esp_partition_t *factory_partition = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_FACTORY, NULL);
104+
if (factory_partition != NULL) {
105+
if (esp_ota_set_boot_partition(factory_partition) == ESP_OK) {
106+
printf("Set boot partition to factory, restarting now.\n");
107+
} else {
108+
printf("Failed to set boot partition to factory.\n");
109+
}
110+
} else {
111+
printf("Factory partition not found.\n");
112+
}
113+
114+
fflush(stdout);
115+
}
116+
117+
void app_main(void) {
118+
// Reset to factory app for the next boot.
119+
// It should return to graphical bootloader.
120+
reset_to_factory_app();
121+
122+
// Initialize the BSP
123+
bsp_i2c_init();
124+
bsp_display_start();
125+
lv_init();
126+
127+
// Initialize NVS
128+
esp_err_t ret = nvs_flash_init();
129+
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
130+
ESP_ERROR_CHECK(nvs_flash_erase());
131+
ret = nvs_flash_init();
132+
}
133+
ESP_ERROR_CHECK(ret);
134+
135+
// Initialize the network interface
136+
ESP_ERROR_CHECK(esp_netif_init());
137+
138+
// Initialize the event loop
139+
ESP_ERROR_CHECK(esp_event_loop_create_default());
140+
141+
// Initialize WiFi
142+
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
143+
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
144+
145+
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_SCAN_DONE, &wifi_event_handler, NULL));
146+
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
147+
ESP_ERROR_CHECK(esp_wifi_start());
148+
149+
// Create a label for WiFi list title
150+
bsp_display_lock(0);
151+
lv_obj_t *label = lv_label_create(lv_scr_act());
152+
lv_label_set_text(label, "List of Wi-Fi networks");
153+
lv_obj_align(label, LV_ALIGN_TOP_MID, 0, 10);
154+
155+
// Create a list for WiFi networks
156+
list = lv_list_create(lv_scr_act());
157+
lv_obj_set_size(list, 300, 180); // Adjust height to leave space for the label
158+
lv_obj_align(list, LV_ALIGN_CENTER, 0, 20);
159+
bsp_display_unlock();
160+
161+
bsp_display_backlight_on();
162+
163+
show_message("Searching..."); // Show the "Searching..." message at the start
164+
165+
while (1) {
166+
list_wifi();
167+
vTaskDelay(pdMS_TO_TICKS(60000)); // Refresh every 60 seconds
168+
}
169+
}
170+
171+
static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
172+
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_SCAN_DONE) {
173+
handle_scan_done();
174+
}
175+
}

apps/wifi_list/partitions.csv

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Name, Type, Subtype, Offset, Size, Flags
2+
nvs, data, nvs, 0x9000, 24K,
3+
otadata, data, ota, , 8K,
4+
phy_init, data, phy, , 4K,
5+
factory, app, factory, , 2M,
6+

0 commit comments

Comments
 (0)