diff --git a/node1.c b/node1.c index 5b1f0f7..a02010e 100644 --- a/node1.c +++ b/node1.c @@ -145,13 +145,13 @@ static bool cb_30(__unused struct repeating_timer *t) { current_bem280_reading.humidity); printf("Sending data to backend server...\n"); printf("MSG: %s", msg); - // bool success = tcp_client_send_message(&tcp_config, msg); - // if (success) { - // printf("✓ Data sent successfully\n"); - // comms_led_blink(); - // } else { - // printf("✗ Failed to send data\n"); - // } + bool success = tcp_client_send_message(&tcp_config, msg); + if (success) { + printf("✓ Data sent successfully\n"); + comms_led_blink(); + } else { + printf("✗ Failed to send data\n"); + } } else { printf("WiFi not connected, skipping send\n"); } diff --git a/tcp_client.c b/tcp_client.c index 53dd7a1..dcb629c 100644 --- a/tcp_client.c +++ b/tcp_client.c @@ -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); return tcp_client_result(state, err); } - + state->connected = true; DEBUG_printf("tcp_client: connected, sending message\n"); - + // Send the message cyw43_arch_lwip_begin(); 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(); return tcp_client_result(state, -1); } - + // Flush the data err_t output_err = tcp_output(tpcb); cyw43_arch_lwip_end(); - + if (output_err != ERR_OK) { DEBUG_printf("tcp_client: failed to output data %d\n", output_err); return tcp_client_result(state, -1); } - + 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) { tcp_client_state_t *state = (tcp_client_state_t*)arg; - + if (!p) { DEBUG_printf("tcp_client: connection closed by server\n"); return tcp_client_result(state, 0); } - + cyw43_arch_lwip_check(); - + if (p->tot_len > 0) { DEBUG_printf("tcp_client: received %d bytes (ignoring)\n", p->tot_len); tcp_recved(tpcb, p->tot_len); } pbuf_free(p); - + return ERR_OK; } 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); - + state->tcp_pcb = tcp_new_ip_type(IP_GET_TYPE(&state->remote_addr)); if (!state->tcp_pcb) { 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); 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); cyw43_arch_lwip_end(); @@ -170,7 +170,7 @@ static bool tcp_client_open(tcp_client_state_t *state) { 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) { if (!config || !server_ip) { return false; @@ -214,7 +214,7 @@ bool tcp_client_send_message(tcp_client_config *config, const char *message) { state->success = 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); if (!tcp_client_open(state)) { @@ -232,18 +232,13 @@ bool tcp_client_send_message(tcp_client_config *config, const char *message) { state->success = false; break; } - -#if PICO_CYW43_ARCH_POLL cyw43_arch_poll(); cyw43_arch_wait_for_work_until(make_timeout_time_ms(100)); -#else - sleep_ms(100); -#endif } bool success = state->success; free(state); - + return success; } @@ -252,4 +247,4 @@ void tcp_client_cleanup(tcp_client_config *config) { config->initialized = false; config->internal_state = NULL; } -} \ No newline at end of file +}