From 8e62283299f06c0f002041220298e6ceea409a37 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Wed, 4 Jan 2023 08:02:16 +0100 Subject: [PATCH] fixed receiving data! --- src/abfall.cpp | 2 +- src/http_client.cpp | 36 ++++++++++++++++++------------------ src/http_client.h | 14 +++++++------- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/abfall.cpp b/src/abfall.cpp index 24bdf39..dbc946a 100644 --- a/src/abfall.cpp +++ b/src/abfall.cpp @@ -27,7 +27,7 @@ int main() { rtc_init(); #ifdef DEBUG - printf("!!! DEBUG mode!!!\n"); + printf("!!! DEBUG mode !!!\n"); #endif WS2812 led(WS2812_PIN); diff --git a/src/http_client.cpp b/src/http_client.cpp index 347696f..319360d 100644 --- a/src/http_client.cpp +++ b/src/http_client.cpp @@ -2,47 +2,42 @@ #include - void result_callback(void *arg, httpc_result_t httpc_result, u32_t rx_content_len, u32_t srv_res, err_t err) { +#ifdef DEBUG printf("Transfer complete\n"); - printf("Local result=%d\n", httpc_result); - printf("Http result=%d\n", srv_res); + // printf("Local result=%d\n", httpc_result); + // printf("Http result=%d\n", srv_res); +#endif + received = true; + offset = 0; } err_t headers_callback(httpc_state_t *connection, void *arg, struct pbuf *hdr, u16_t hdr_len, u32_t content_len) { +#ifdef DEBUG printf("Headers received\n"); printf("Content length=%d\n", content_len); printf("Header length=%d\n", hdr_len); - - pbuf_copy_partial(hdr, myHeaderBuffer, hdr->tot_len, 0); - printf("Headers \n"); - printf("%s", myHeaderBuffer); +#endif return ERR_OK; } - err_t body_callback(void *arg, struct altcp_pcb *conn, struct pbuf *p, err_t err) { - bool *received = (bool *)arg; - if (err != ERR_OK) { - printf("PROBLEM!!\n"); return err; } - pbuf_copy_partial(p, myBodyBuffer, p->tot_len, 0); - printf("HUHU:\n%s", myBodyBuffer); - - *received = true; + pbuf_copy_partial(p, &myBodyBuffer[offset], p->tot_len, 0); + offset += p->len; return ERR_OK; } HttpClient::HttpClient() { m_settings.use_proxy = false; - m_settings.result_fn = result_callback; // result_callback; + m_settings.result_fn = result_callback; // result_callback; m_settings.headers_done_fn = headers_callback; // headers_callback; } @@ -52,7 +47,7 @@ std::string HttpClient::retrieveWasteDatesAsCsv() { std::string result(""); err_t err = httpc_get_file_dns("beenas.brodbeck-online.de", port, "/abfall/abfall.csv", - &m_settings, body_callback, &m_received, nullptr); + &m_settings, body_callback, nullptr, nullptr); // If there was an error, return empty result. if (err != ERR_OK) { @@ -60,7 +55,7 @@ std::string HttpClient::retrieveWasteDatesAsCsv() { } uint wait_count = 0; - while (m_received == false) { + while (received == false) { ++wait_count; if (wait_count >= 10) { @@ -70,6 +65,11 @@ std::string HttpClient::retrieveWasteDatesAsCsv() { } result.append(myBodyBuffer); + received = false; + +#ifdef DEBUG + std::cout << result << std::endl; +#endif return result; } \ No newline at end of file diff --git a/src/http_client.h b/src/http_client.h index 2113c42..378aaa7 100644 --- a/src/http_client.h +++ b/src/http_client.h @@ -4,16 +4,17 @@ #include "lwip/apps/http_client.h" #include "pico/cyw43_arch.h" -//#include "pico/stdlib.h" +#include "pico/stdlib.h" -static char myHeaderBuffer[2048]; static char myBodyBuffer[2048]; +static uint16_t offset{0}; +static bool received{false}; -void result_callback(void *arg, httpc_result_t httpc_result, u32_t rx_content_len, - u32_t srv_res, err_t err); +void result_callback(void *arg, httpc_result_t httpc_result, u32_t rx_content_len, u32_t srv_res, + err_t err); -err_t headers_callback(httpc_state_t *connection, void *arg, struct pbuf *hdr, - u16_t hdr_len, u32_t content_len); +err_t headers_callback(httpc_state_t *connection, void *arg, struct pbuf *hdr, u16_t hdr_len, + u32_t content_len); err_t body_callback(void *arg, struct altcp_pcb *conn, struct pbuf *p, err_t err); @@ -21,7 +22,6 @@ class HttpClient { public: HttpClient(); std::string retrieveWasteDatesAsCsv(); - bool m_received{false}; private: httpc_connection_t m_settings;