PICO W WIFI TCP 测试发送CPU 温度
This commit is contained in:
parent
c976c93aa5
commit
dbfdce8ec9
6
.vscode/settings.json
vendored
6
.vscode/settings.json
vendored
@ -3,6 +3,8 @@
|
|||||||
"C_Cpp.errorSquiggles": "disabled",
|
"C_Cpp.errorSquiggles": "disabled",
|
||||||
"files.associations": {
|
"files.associations": {
|
||||||
"cstring": "cpp",
|
"cstring": "cpp",
|
||||||
"random": "cpp"
|
"random": "cpp",
|
||||||
}
|
"system_error": "c"
|
||||||
|
},
|
||||||
|
"cmake.ignoreCMakeListsMissing": true
|
||||||
}
|
}
|
@ -261,7 +261,6 @@ static void core1_main()
|
|||||||
|
|
||||||
#define TARGET_SSID "cpyfb-01" // 替换为你要连接的 SSID
|
#define TARGET_SSID "cpyfb-01" // 替换为你要连接的 SSID
|
||||||
#define TARGET_PASSWORD "aa1122334" // 替换为你的 Wi-Fi 密码
|
#define TARGET_PASSWORD "aa1122334" // 替换为你的 Wi-Fi 密码
|
||||||
|
|
||||||
static bool is_target_ssid_found = false; // 标志:目标 SSID 是否被找到
|
static bool is_target_ssid_found = false; // 标志:目标 SSID 是否被找到
|
||||||
|
|
||||||
// 扫描结果处理函数
|
// 扫描结果处理函数
|
||||||
@ -337,7 +336,13 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
// 连接成功,执行 TCP 客户端测试
|
// 连接成功,执行 TCP 客户端测试
|
||||||
while (1) {
|
while (1) {
|
||||||
run_tcp_client_test();
|
float ONBOARD_TEMPERATURE = read_onboard_temperature();
|
||||||
|
printf("Onboard temperature %.02f°C %.02f°F\n", ONBOARD_TEMPERATURE, (ONBOARD_TEMPERATURE * 9 / 5 + 32));
|
||||||
|
|
||||||
|
char ONBOARD_TEMPERATURE_TEMP[BUFER] = { 0 };
|
||||||
|
sprintf(ONBOARD_TEMPERATURE_TEMP, "PICO_W CPU temperature %.02f°C %.02f°F\n", ONBOARD_TEMPERATURE, (ONBOARD_TEMPERATURE * 9 / 5 + 32));
|
||||||
|
|
||||||
|
run_tcp_client_test(ONBOARD_TEMPERATURE_TEMP);
|
||||||
sleep_ms(3000);
|
sleep_ms(3000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
#include "WIFI.h"
|
#include "WIFI.h"
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
static void dump_bytes(const uint8_t *bptr, uint32_t len) {
|
static void dump_bytes(const uint8_t *bptr, uint32_t len)
|
||||||
|
{
|
||||||
unsigned int i = 0;
|
unsigned int i = 0;
|
||||||
|
|
||||||
printf("dump_bytes %d", len);
|
printf("dump_bytes %d", len);
|
||||||
@ -15,6 +16,7 @@ static void dump_bytes(const uint8_t *bptr, uint32_t len) {
|
|||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#define DUMP_BYTES dump_bytes // 调试用的字节转储函数
|
#define DUMP_BYTES dump_bytes // 调试用的字节转储函数
|
||||||
#else
|
#else
|
||||||
#define DUMP_BYTES(A,B)
|
#define DUMP_BYTES(A,B)
|
||||||
@ -31,7 +33,8 @@ typedef struct TCP_CLIENT_T_ {
|
|||||||
bool connected; // 是否已连接
|
bool connected; // 是否已连接
|
||||||
} TCP_CLIENT_T;
|
} TCP_CLIENT_T;
|
||||||
|
|
||||||
static err_t tcp_client_close(void *arg) {
|
static err_t tcp_client_close(void *arg)
|
||||||
|
{
|
||||||
TCP_CLIENT_T *state = (TCP_CLIENT_T *) arg;
|
TCP_CLIENT_T *state = (TCP_CLIENT_T *) arg;
|
||||||
err_t err = ERR_OK;
|
err_t err = ERR_OK;
|
||||||
if (state->tcp_pcb != NULL) {
|
if (state->tcp_pcb != NULL) {
|
||||||
@ -52,7 +55,8 @@ static err_t tcp_client_close(void *arg) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 操作结果回调
|
// 操作结果回调
|
||||||
static err_t tcp_result(void *arg, int status) {
|
static err_t tcp_result(void *arg, int status)
|
||||||
|
{
|
||||||
TCP_CLIENT_T *state = (TCP_CLIENT_T *) arg;
|
TCP_CLIENT_T *state = (TCP_CLIENT_T *) arg;
|
||||||
if (status == 0) {
|
if (status == 0) {
|
||||||
DEBUG_printf("test success\n"); // 测试成功
|
DEBUG_printf("test success\n"); // 测试成功
|
||||||
@ -64,13 +68,15 @@ static err_t tcp_result(void *arg, int status) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 轮询回调
|
// 轮询回调
|
||||||
static err_t tcp_client_poll(void *arg, struct tcp_pcb *tpcb) {
|
static err_t tcp_client_poll(void *arg, struct tcp_pcb *tpcb)
|
||||||
|
{
|
||||||
DEBUG_printf("tcp_client_poll\n");
|
DEBUG_printf("tcp_client_poll\n");
|
||||||
return tcp_result(arg, -1); // 无响应视为错误
|
return tcp_result(arg, -1); // 无响应视为错误
|
||||||
}
|
}
|
||||||
|
|
||||||
// 错误处理回调
|
// 错误处理回调
|
||||||
static void tcp_client_err(void *arg, err_t err) {
|
static void tcp_client_err(void *arg, err_t err)
|
||||||
|
{
|
||||||
if (err != ERR_ABRT) {
|
if (err != ERR_ABRT) {
|
||||||
DEBUG_printf("tcp_client_err %d\n", err); // 打印错误信息
|
DEBUG_printf("tcp_client_err %d\n", err); // 打印错误信息
|
||||||
tcp_result(arg, err);
|
tcp_result(arg, err);
|
||||||
@ -78,7 +84,8 @@ static void tcp_client_err(void *arg, err_t err) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 连接建立回调
|
// 连接建立回调
|
||||||
static err_t tcp_client_connected(void *arg, struct tcp_pcb *tpcb, err_t err) {
|
static err_t tcp_client_connected(void *arg, struct tcp_pcb *tpcb, err_t err)
|
||||||
|
{
|
||||||
TCP_CLIENT_T *state = (TCP_CLIENT_T *) arg;
|
TCP_CLIENT_T *state = (TCP_CLIENT_T *) arg;
|
||||||
if (err != ERR_OK) {
|
if (err != ERR_OK) {
|
||||||
printf("connect failed %d\n", err); // 连接失败
|
printf("connect failed %d\n", err); // 连接失败
|
||||||
@ -87,18 +94,13 @@ static err_t tcp_client_connected(void *arg, struct tcp_pcb *tpcb, err_t err) {
|
|||||||
state->connected = true; // 标记已连接
|
state->connected = true; // 标记已连接
|
||||||
DEBUG_printf("连接成功,准备发送消息!!!\n"); // 连接成功,准备发送消息
|
DEBUG_printf("连接成功,准备发送消息!!!\n"); // 连接成功,准备发送消息
|
||||||
|
|
||||||
// 定义要发送的字符串
|
|
||||||
const char *message = "PICO W\n";
|
|
||||||
state->buffer_len = strlen(message); // 设置要发送的字符串长度
|
|
||||||
memcpy(state->buffer, message, state->buffer_len); // 将字符串复制到缓冲区
|
|
||||||
|
|
||||||
// 发送字符串到服务器
|
// 发送字符串到服务器
|
||||||
err = tcp_write(tpcb, state->buffer, state->buffer_len, TCP_WRITE_FLAG_COPY);
|
err = tcp_write(tpcb, state->buffer, state->buffer_len, TCP_WRITE_FLAG_COPY);
|
||||||
if (err != ERR_OK) {
|
if (err != ERR_OK) {
|
||||||
DEBUG_printf("发送失败: %d\n", err); // 发送失败
|
DEBUG_printf("发送失败: %d\n", err); // 发送失败
|
||||||
return tcp_result(arg, -1);
|
return tcp_result(arg, -1);
|
||||||
}
|
}
|
||||||
DEBUG_printf("发送数据: %s\n", message); // 打印发送的消息
|
DEBUG_printf("发送数据: %s\n", state->buffer); // 打印发送的消息
|
||||||
|
|
||||||
memset(state->buffer, 0, state->buffer_len);
|
memset(state->buffer, 0, state->buffer_len);
|
||||||
state->buffer_len = 0;
|
state->buffer_len = 0;
|
||||||
@ -106,7 +108,8 @@ static err_t tcp_client_connected(void *arg, struct tcp_pcb *tpcb, err_t err) {
|
|||||||
return ERR_OK;
|
return ERR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
err_t tcp_client_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) {
|
err_t tcp_client_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
|
||||||
|
{
|
||||||
TCP_CLIENT_T *state = (TCP_CLIENT_T *) arg;
|
TCP_CLIENT_T *state = (TCP_CLIENT_T *) arg;
|
||||||
if (!p) {
|
if (!p) {
|
||||||
DEBUG_printf("接收数据失败,p 为 NULL\n"); // 添加调试信息
|
DEBUG_printf("接收数据失败,p 为 NULL\n"); // 添加调试信息
|
||||||
@ -123,8 +126,7 @@ err_t tcp_client_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err
|
|||||||
}
|
}
|
||||||
// Receive the buffer
|
// Receive the buffer
|
||||||
const uint16_t buffer_left = BUF_SIZE - state->buffer_len;
|
const uint16_t buffer_left = BUF_SIZE - state->buffer_len;
|
||||||
state->buffer_len += pbuf_copy_partial(p, state->buffer + state->buffer_len,
|
state->buffer_len += pbuf_copy_partial(p, state->buffer + state->buffer_len, p->tot_len > buffer_left ? buffer_left : p->tot_len, 0);
|
||||||
p->tot_len > buffer_left ? buffer_left : p->tot_len, 0);
|
|
||||||
tcp_recved(tpcb, p->tot_len);
|
tcp_recved(tpcb, p->tot_len);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -140,7 +142,8 @@ err_t tcp_client_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 发送数据回调
|
// 发送数据回调
|
||||||
static err_t tcp_client_sent(void *arg, struct tcp_pcb *tpcb, u16_t len) {
|
static err_t tcp_client_sent(void *arg, struct tcp_pcb *tpcb, u16_t len)
|
||||||
|
{
|
||||||
TCP_CLIENT_T *state = (TCP_CLIENT_T *) arg;
|
TCP_CLIENT_T *state = (TCP_CLIENT_T *) arg;
|
||||||
DEBUG_printf("tcp_client_sent %u\n", len); // 打印发送的数据长度
|
DEBUG_printf("tcp_client_sent %u\n", len); // 打印发送的数据长度
|
||||||
state->sent_len += len; // 更新已发送的数据长度
|
state->sent_len += len; // 更新已发送的数据长度
|
||||||
@ -162,7 +165,8 @@ static err_t tcp_client_sent(void *arg, struct tcp_pcb *tpcb, u16_t len) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 打开TCP连接
|
// 打开TCP连接
|
||||||
static bool tcp_client_open(void *arg) {
|
static bool tcp_client_open(void *arg)
|
||||||
|
{
|
||||||
TCP_CLIENT_T *state = (TCP_CLIENT_T *) arg;
|
TCP_CLIENT_T *state = (TCP_CLIENT_T *) arg;
|
||||||
DEBUG_printf("Connecting to %s port %u\n", ip4addr_ntoa(&state->remote_addr), TCP_PORT);
|
DEBUG_printf("Connecting to %s port %u\n", ip4addr_ntoa(&state->remote_addr), TCP_PORT);
|
||||||
state->tcp_pcb = tcp_new_ip_type(IP_GET_TYPE(&state->remote_addr)); // 创建TCP控制块
|
state->tcp_pcb = tcp_new_ip_type(IP_GET_TYPE(&state->remote_addr)); // 创建TCP控制块
|
||||||
@ -177,7 +181,7 @@ static bool tcp_client_open(void *arg) {
|
|||||||
tcp_recv(state->tcp_pcb, tcp_client_recv); // 设置接收回调
|
tcp_recv(state->tcp_pcb, tcp_client_recv); // 设置接收回调
|
||||||
tcp_err(state->tcp_pcb, tcp_client_err); // 设置错误回调
|
tcp_err(state->tcp_pcb, tcp_client_err); // 设置错误回调
|
||||||
|
|
||||||
state->buffer_len = 0; // 初始化缓冲区长度
|
//state->buffer_len = 0; // 初始化缓冲区长度
|
||||||
|
|
||||||
// 使用cyw43_arch_lwip_begin/end确保正确锁定
|
// 使用cyw43_arch_lwip_begin/end确保正确锁定
|
||||||
cyw43_arch_lwip_begin();
|
cyw43_arch_lwip_begin();
|
||||||
@ -188,14 +192,14 @@ static bool tcp_client_open(void *arg) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 初始化TCP客户端
|
// 初始化TCP客户端
|
||||||
static TCP_CLIENT_T* tcp_client_init(void) {
|
static TCP_CLIENT_T *tcp_client_init(void)
|
||||||
|
{
|
||||||
TCP_CLIENT_T *state = calloc(1, sizeof(TCP_CLIENT_T)); // 分配内存
|
TCP_CLIENT_T *state = calloc(1, sizeof(TCP_CLIENT_T)); // 分配内存
|
||||||
if (!state) {
|
if (!state) {
|
||||||
DEBUG_printf("failed to allocate state\n"); // 分配失败
|
DEBUG_printf("failed to allocate state\n"); // 分配失败
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 如果 TEST_TCP_SERVER_IP 是 IP 地址,直接解析为 IP 地址
|
// 如果 TEST_TCP_SERVER_IP 是 IP 地址,直接解析为 IP 地址
|
||||||
if (ip4addr_aton(TEST_TCP_SERVER_IP, &state->remote_addr) == 0) {
|
if (ip4addr_aton(TEST_TCP_SERVER_IP, &state->remote_addr) == 0) {
|
||||||
// 如果解析失败,尝试作为域名解析
|
// 如果解析失败,尝试作为域名解析
|
||||||
@ -213,7 +217,8 @@ static TCP_CLIENT_T* tcp_client_init(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 运行TCP客户端测试
|
// 运行TCP客户端测试
|
||||||
void run_tcp_client_test(void) {
|
void run_tcp_client_test(char *s)
|
||||||
|
{
|
||||||
TCP_CLIENT_T *state = tcp_client_init(); // 初始化客户端
|
TCP_CLIENT_T *state = tcp_client_init(); // 初始化客户端
|
||||||
if (!state) {
|
if (!state) {
|
||||||
return;
|
return;
|
||||||
@ -223,6 +228,10 @@ void run_tcp_client_test(void) {
|
|||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
strcpy(state->buffer, s);
|
||||||
|
state->buffer_len = strlen(state->buffer);
|
||||||
|
|
||||||
while (!state->complete) { // 循环等待测试完成
|
while (!state->complete) { // 循环等待测试完成
|
||||||
#if PICO_CYW43_ARCH_POLL
|
#if PICO_CYW43_ARCH_POLL
|
||||||
// 如果使用pico_cyw43_arch_poll,需要定期调用cyw43_arch_poll
|
// 如果使用pico_cyw43_arch_poll,需要定期调用cyw43_arch_poll
|
||||||
@ -230,10 +239,9 @@ void run_tcp_client_test(void) {
|
|||||||
// 可以选择休眠直到有工作需要做
|
// 可以选择休眠直到有工作需要做
|
||||||
cyw43_arch_wait_for_work_until(make_timeout_time_ms(1000));
|
cyw43_arch_wait_for_work_until(make_timeout_time_ms(1000));
|
||||||
#else
|
#else
|
||||||
cyw43_arch_poll();
|
//cyw43_arch_poll();
|
||||||
// 可以选择休眠直到有工作需要做
|
// 可以选择休眠直到有工作需要做
|
||||||
cyw43_arch_wait_for_work_until(make_timeout_time_ms(1000));
|
//cyw43_arch_wait_for_work_until(make_timeout_time_ms(1000));
|
||||||
|
|
||||||
|
|
||||||
// 如果不使用pico_cyw43_arch_poll,可以通过中断在后台处理
|
// 如果不使用pico_cyw43_arch_poll,可以通过中断在后台处理
|
||||||
//sleep_ms(1000); // 示例中的阻塞操作
|
//sleep_ms(1000); // 示例中的阻塞操作
|
||||||
|
@ -15,13 +15,11 @@
|
|||||||
#define TEST_TCP_SERVER_IP "192.168.31.17"
|
#define TEST_TCP_SERVER_IP "192.168.31.17"
|
||||||
#define TCP_PORT 91
|
#define TCP_PORT 91
|
||||||
|
|
||||||
#define DEBUG_printf printf
|
|
||||||
#define BUF_SIZE 2048
|
#define BUF_SIZE 2048
|
||||||
|
|
||||||
#define TEST_ITERATIONS 10
|
#define TEST_ITERATIONS 10
|
||||||
#define POLL_TIME_S 5
|
#define POLL_TIME_S 5
|
||||||
|
#define DEBUG_printf printf
|
||||||
|
|
||||||
|
void run_tcp_client_test(char *s);
|
||||||
extern void run_tcp_client_test(void);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user