fixed receiving data!

This commit is contained in:
Martin Brodbeck 2023-01-04 08:02:16 +01:00
parent 132b8d49fe
commit 8e62283299
3 changed files with 26 additions and 26 deletions

View file

@ -27,7 +27,7 @@ int main() {
rtc_init(); rtc_init();
#ifdef DEBUG #ifdef DEBUG
printf("!!! DEBUG mode!!!\n"); printf("!!! DEBUG mode !!!\n");
#endif #endif
WS2812 led(WS2812_PIN); WS2812 led(WS2812_PIN);

View file

@ -2,40 +2,35 @@
#include <iostream> #include <iostream>
void result_callback(void *arg, httpc_result_t httpc_result, u32_t rx_content_len, u32_t srv_res, void result_callback(void *arg, httpc_result_t httpc_result, u32_t rx_content_len, u32_t srv_res,
err_t err) { err_t err) {
#ifdef DEBUG
printf("Transfer complete\n"); printf("Transfer complete\n");
printf("Local result=%d\n", httpc_result); // printf("Local result=%d\n", httpc_result);
printf("Http result=%d\n", srv_res); // 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, err_t headers_callback(httpc_state_t *connection, void *arg, struct pbuf *hdr, u16_t hdr_len,
u32_t content_len) { u32_t content_len) {
#ifdef DEBUG
printf("Headers received\n"); printf("Headers received\n");
printf("Content length=%d\n", content_len); printf("Content length=%d\n", content_len);
printf("Header length=%d\n", hdr_len); printf("Header length=%d\n", hdr_len);
#endif
pbuf_copy_partial(hdr, myHeaderBuffer, hdr->tot_len, 0);
printf("Headers \n");
printf("%s", myHeaderBuffer);
return ERR_OK; return ERR_OK;
} }
err_t body_callback(void *arg, struct altcp_pcb *conn, struct pbuf *p, err_t err) { err_t body_callback(void *arg, struct altcp_pcb *conn, struct pbuf *p, err_t err) {
bool *received = (bool *)arg;
if (err != ERR_OK) { if (err != ERR_OK) {
printf("PROBLEM!!\n");
return err; return err;
} }
pbuf_copy_partial(p, myBodyBuffer, p->tot_len, 0); pbuf_copy_partial(p, &myBodyBuffer[offset], p->tot_len, 0);
printf("HUHU:\n%s", myBodyBuffer); offset += p->len;
*received = true;
return ERR_OK; return ERR_OK;
} }
@ -52,7 +47,7 @@ std::string HttpClient::retrieveWasteDatesAsCsv() {
std::string result(""); std::string result("");
err_t err = httpc_get_file_dns("beenas.brodbeck-online.de", port, "/abfall/abfall.csv", 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 there was an error, return empty result.
if (err != ERR_OK) { if (err != ERR_OK) {
@ -60,7 +55,7 @@ std::string HttpClient::retrieveWasteDatesAsCsv() {
} }
uint wait_count = 0; uint wait_count = 0;
while (m_received == false) { while (received == false) {
++wait_count; ++wait_count;
if (wait_count >= 10) { if (wait_count >= 10) {
@ -70,6 +65,11 @@ std::string HttpClient::retrieveWasteDatesAsCsv() {
} }
result.append(myBodyBuffer); result.append(myBodyBuffer);
received = false;
#ifdef DEBUG
std::cout << result << std::endl;
#endif
return result; return result;
} }

View file

@ -4,16 +4,17 @@
#include "lwip/apps/http_client.h" #include "lwip/apps/http_client.h"
#include "pico/cyw43_arch.h" #include "pico/cyw43_arch.h"
//#include "pico/stdlib.h" #include "pico/stdlib.h"
static char myHeaderBuffer[2048];
static char myBodyBuffer[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, void result_callback(void *arg, httpc_result_t httpc_result, u32_t rx_content_len, u32_t srv_res,
u32_t srv_res, err_t err); err_t err);
err_t headers_callback(httpc_state_t *connection, void *arg, struct pbuf *hdr, err_t headers_callback(httpc_state_t *connection, void *arg, struct pbuf *hdr, u16_t hdr_len,
u16_t hdr_len, u32_t content_len); u32_t content_len);
err_t body_callback(void *arg, struct altcp_pcb *conn, struct pbuf *p, err_t err); err_t body_callback(void *arg, struct altcp_pcb *conn, struct pbuf *p, err_t err);
@ -21,7 +22,6 @@ class HttpClient {
public: public:
HttpClient(); HttpClient();
std::string retrieveWasteDatesAsCsv(); std::string retrieveWasteDatesAsCsv();
bool m_received{false};
private: private:
httpc_connection_t m_settings; httpc_connection_t m_settings;