mirror of
https://git.aixiao.me/aixiao/Danger-alarm.git
synced 2025-07-29 19:13:39 +08:00
测试WIFI先扫描SID
This commit is contained in:
@@ -258,41 +258,95 @@ static void core1_main()
|
||||
return;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
stdio_init_all();
|
||||
sleep_ms(1000);
|
||||
|
||||
#define TARGET_SSID "cpyfb-01" // 替换为你要连接的 SSID
|
||||
#define TARGET_PASSWORD "aa1122334" // 替换为你的 Wi-Fi 密码
|
||||
|
||||
static bool is_target_ssid_found = false; // 标志:目标 SSID 是否被找到
|
||||
|
||||
// 扫描结果处理函数
|
||||
static int scan_result(void *env, const cyw43_ev_scan_result_t *result) {
|
||||
if (result) {
|
||||
// 输出每个扫描结果的信息
|
||||
printf("ssid: %-32s rssi: %4d chan: %3d mac: %02x:%02x:%02x:%02x:%02x:%02x sec: %u\n",
|
||||
result->ssid, result->rssi, result->channel,
|
||||
result->bssid[0], result->bssid[1], result->bssid[2], result->bssid[3], result->bssid[4], result->bssid[5],
|
||||
result->auth_mode);
|
||||
|
||||
// 检查是否是目标 SSID
|
||||
if (!is_target_ssid_found && strcmp((const char*)result->ssid, TARGET_SSID) == 0) {
|
||||
printf("Found target SSID: %s\n", TARGET_SSID);
|
||||
is_target_ssid_found = true;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 执行 Wi-Fi 扫描
|
||||
int wifi_scan() {
|
||||
cyw43_wifi_scan_options_t scan_options = {0};
|
||||
int err = cyw43_wifi_scan(&cyw43_state, &scan_options, NULL, scan_result);
|
||||
if (err != 0) {
|
||||
printf("Failed to start Wi-Fi scan: %d\n", err);
|
||||
return err;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 连接 Wi-Fi
|
||||
int wifi_connect() {
|
||||
printf("Connecting to Wi-Fi...\n");
|
||||
if (cyw43_arch_wifi_connect_timeout_ms(TARGET_SSID, TARGET_PASSWORD, CYW43_AUTH_WPA2_AES_PSK, 30000)) {
|
||||
printf("Wi-Fi connection failed.\n");
|
||||
return -1;
|
||||
} else {
|
||||
printf("Wi-Fi connected successfully.\n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// 主程序
|
||||
int main(int argc, char *argv[]) {
|
||||
stdio_init_all();
|
||||
sleep_ms(3000);
|
||||
|
||||
// 初始化 CYW43 模块
|
||||
if (cyw43_arch_init()) {
|
||||
DEBUG_printf("failed to initialise\n");
|
||||
printf("CYW43 initialization failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
cyw43_arch_enable_sta_mode();
|
||||
|
||||
printf("Connecting to Wi-Fi...\n");
|
||||
if (cyw43_arch_wifi_connect_timeout_ms("root", "@aixiao.19960623", CYW43_AUTH_WPA2_AES_PSK, 30000)) {
|
||||
printf("failed to connect.\n");
|
||||
return 1;
|
||||
} else {
|
||||
printf("Connected.\n");
|
||||
// 执行扫描并等待找到目标 SSID
|
||||
while (!is_target_ssid_found) {
|
||||
int scan_result_code = wifi_scan();
|
||||
if (scan_result_code != 0) {
|
||||
cyw43_arch_deinit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
sleep_ms(7000); // 每隔 7 秒重新扫描一次
|
||||
}
|
||||
|
||||
while (1)
|
||||
{
|
||||
// 目标 Wi-Fi 已找到,连接 Wi-Fi
|
||||
if (wifi_connect() != 0) {
|
||||
cyw43_arch_deinit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 连接成功,执行 TCP 客户端测试
|
||||
while (1) {
|
||||
run_tcp_client_test();
|
||||
sleep_ms(3000);
|
||||
}
|
||||
|
||||
|
||||
|
||||
cyw43_arch_deinit();
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user