get tcp working

This commit is contained in:
Travis Shears 2025-12-21 20:44:11 +01:00
parent f107bfb55b
commit 65ad286add
Signed by: travisshears
GPG key ID: CB9BF1910F3F7469
2 changed files with 23 additions and 28 deletions

14
node1.c
View file

@ -145,13 +145,13 @@ static bool cb_30(__unused struct repeating_timer *t) {
current_bem280_reading.humidity); current_bem280_reading.humidity);
printf("Sending data to backend server...\n"); printf("Sending data to backend server...\n");
printf("MSG: %s", msg); printf("MSG: %s", msg);
// bool success = tcp_client_send_message(&tcp_config, msg); bool success = tcp_client_send_message(&tcp_config, msg);
// if (success) { if (success) {
// printf("✓ Data sent successfully\n"); printf("✓ Data sent successfully\n");
// comms_led_blink(); comms_led_blink();
// } else { } else {
// printf("✗ Failed to send data\n"); printf("✗ Failed to send data\n");
// } }
} else { } else {
printf("WiFi not connected, skipping send\n"); printf("WiFi not connected, skipping send\n");
} }

View file

@ -79,10 +79,10 @@ static err_t tcp_client_connected(void *arg, struct tcp_pcb *tpcb, err_t err) {
DEBUG_printf("tcp_client: connect failed %d\n", err); DEBUG_printf("tcp_client: connect failed %d\n", err);
return tcp_client_result(state, err); return tcp_client_result(state, err);
} }
state->connected = true; state->connected = true;
DEBUG_printf("tcp_client: connected, sending message\n"); DEBUG_printf("tcp_client: connected, sending message\n");
// Send the message // Send the message
cyw43_arch_lwip_begin(); cyw43_arch_lwip_begin();
err_t write_err = tcp_write(tpcb, state->message, state->message_len, TCP_WRITE_FLAG_COPY); err_t write_err = tcp_write(tpcb, state->message, state->message_len, TCP_WRITE_FLAG_COPY);
@ -91,16 +91,16 @@ static err_t tcp_client_connected(void *arg, struct tcp_pcb *tpcb, err_t err) {
cyw43_arch_lwip_end(); cyw43_arch_lwip_end();
return tcp_client_result(state, -1); return tcp_client_result(state, -1);
} }
// Flush the data // Flush the data
err_t output_err = tcp_output(tpcb); err_t output_err = tcp_output(tpcb);
cyw43_arch_lwip_end(); cyw43_arch_lwip_end();
if (output_err != ERR_OK) { if (output_err != ERR_OK) {
DEBUG_printf("tcp_client: failed to output data %d\n", output_err); DEBUG_printf("tcp_client: failed to output data %d\n", output_err);
return tcp_client_result(state, -1); return tcp_client_result(state, -1);
} }
return ERR_OK; return ERR_OK;
} }
@ -121,27 +121,27 @@ static void tcp_client_err(void *arg, err_t err) {
static err_t tcp_client_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) { static err_t tcp_client_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) {
tcp_client_state_t *state = (tcp_client_state_t*)arg; tcp_client_state_t *state = (tcp_client_state_t*)arg;
if (!p) { if (!p) {
DEBUG_printf("tcp_client: connection closed by server\n"); DEBUG_printf("tcp_client: connection closed by server\n");
return tcp_client_result(state, 0); return tcp_client_result(state, 0);
} }
cyw43_arch_lwip_check(); cyw43_arch_lwip_check();
if (p->tot_len > 0) { if (p->tot_len > 0) {
DEBUG_printf("tcp_client: received %d bytes (ignoring)\n", p->tot_len); DEBUG_printf("tcp_client: received %d bytes (ignoring)\n", p->tot_len);
tcp_recved(tpcb, p->tot_len); tcp_recved(tpcb, p->tot_len);
} }
pbuf_free(p); pbuf_free(p);
return ERR_OK; return ERR_OK;
} }
static bool tcp_client_open(tcp_client_state_t *state) { static bool tcp_client_open(tcp_client_state_t *state) {
DEBUG_printf("tcp_client: connecting to %s port %u\n", DEBUG_printf("tcp_client: connecting to %s port %u\n",
ip4addr_ntoa(&state->remote_addr), state->remote_port); ip4addr_ntoa(&state->remote_addr), state->remote_port);
state->tcp_pcb = tcp_new_ip_type(IP_GET_TYPE(&state->remote_addr)); state->tcp_pcb = tcp_new_ip_type(IP_GET_TYPE(&state->remote_addr));
if (!state->tcp_pcb) { if (!state->tcp_pcb) {
DEBUG_printf("tcp_client: failed to create pcb\n"); DEBUG_printf("tcp_client: failed to create pcb\n");
@ -158,7 +158,7 @@ static bool tcp_client_open(tcp_client_state_t *state) {
state->timeout_time = make_timeout_time_ms(state->timeout_ms); state->timeout_time = make_timeout_time_ms(state->timeout_ms);
cyw43_arch_lwip_begin(); cyw43_arch_lwip_begin();
err_t err = tcp_connect(state->tcp_pcb, &state->remote_addr, err_t err = tcp_connect(state->tcp_pcb, &state->remote_addr,
state->remote_port, tcp_client_connected); state->remote_port, tcp_client_connected);
cyw43_arch_lwip_end(); cyw43_arch_lwip_end();
@ -170,7 +170,7 @@ static bool tcp_client_open(tcp_client_state_t *state) {
return true; return true;
} }
bool tcp_client_init(tcp_client_config *config, const char *server_ip, bool tcp_client_init(tcp_client_config *config, const char *server_ip,
uint16_t server_port, uint32_t timeout_ms) { uint16_t server_port, uint32_t timeout_ms) {
if (!config || !server_ip) { if (!config || !server_ip) {
return false; return false;
@ -214,7 +214,7 @@ bool tcp_client_send_message(tcp_client_config *config, const char *message) {
state->success = false; state->success = false;
state->connected = false; state->connected = false;
DEBUG_printf("tcp_client: sending message (%d bytes): %s\n", DEBUG_printf("tcp_client: sending message (%d bytes): %s\n",
state->message_len, message); state->message_len, message);
if (!tcp_client_open(state)) { if (!tcp_client_open(state)) {
@ -232,18 +232,13 @@ bool tcp_client_send_message(tcp_client_config *config, const char *message) {
state->success = false; state->success = false;
break; break;
} }
#if PICO_CYW43_ARCH_POLL
cyw43_arch_poll(); cyw43_arch_poll();
cyw43_arch_wait_for_work_until(make_timeout_time_ms(100)); cyw43_arch_wait_for_work_until(make_timeout_time_ms(100));
#else
sleep_ms(100);
#endif
} }
bool success = state->success; bool success = state->success;
free(state); free(state);
return success; return success;
} }
@ -252,4 +247,4 @@ void tcp_client_cleanup(tcp_client_config *config) {
config->initialized = false; config->initialized = false;
config->internal_state = NULL; config->internal_state = NULL;
} }
} }