测试WIFI先扫描SID
This commit is contained in:
parent
d85cdffa6f
commit
c976c93aa5
@ -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[])
|
||||
{
|
||||
|
@ -17,8 +17,13 @@
|
||||
#include "hardware/adc.h"
|
||||
|
||||
#include "pico/cyw43_arch.h"
|
||||
|
||||
#include "lwip/pbuf.h"
|
||||
#include "lwip/tcp.h"
|
||||
#include "lwip/ip_addr.h" // 包含 IP 地址结构体定义
|
||||
|
||||
#include "hardware/vreg.h"
|
||||
#include "hardware/clocks.h"
|
||||
|
||||
#define BUFER 1024
|
||||
#define DS18B20_PIN 15 // DS18B20 引脚
|
||||
|
@ -194,7 +194,21 @@ static TCP_CLIENT_T* tcp_client_init(void) {
|
||||
DEBUG_printf("failed to allocate state\n"); // 分配失败
|
||||
return NULL;
|
||||
}
|
||||
ip4addr_aton(TEST_TCP_SERVER_IP, &state->remote_addr); // 解析服务器IP地址
|
||||
|
||||
|
||||
// 如果 TEST_TCP_SERVER_IP 是 IP 地址,直接解析为 IP 地址
|
||||
if (ip4addr_aton(TEST_TCP_SERVER_IP, &state->remote_addr) == 0) {
|
||||
// 如果解析失败,尝试作为域名解析
|
||||
DEBUG_printf("Failed to parse IP address, trying DNS resolution\n");
|
||||
|
||||
err_t err = dns_gethostbyname(TEST_TCP_SERVER_IP, &state->remote_addr, NULL, NULL);
|
||||
if (err != ERR_OK) {
|
||||
DEBUG_printf("DNS resolution failed\n");
|
||||
free(state);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
|
@ -10,8 +10,9 @@
|
||||
|
||||
#include "lwip/pbuf.h"
|
||||
#include "lwip/tcp.h"
|
||||
#include "lwip/dns.h"
|
||||
|
||||
#define TEST_TCP_SERVER_IP "192.168.9.90"
|
||||
#define TEST_TCP_SERVER_IP "192.168.31.17"
|
||||
#define TCP_PORT 91
|
||||
|
||||
#define DEBUG_printf printf
|
||||
|
Loading…
Reference in New Issue
Block a user