diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3f6c5fd..f8875d9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -2,6 +2,7 @@ set(SOURCES ws2812.cpp + http_client.cpp ntp_client.cpp abfall.cpp ) diff --git a/src/abfall.cpp b/src/abfall.cpp index af10bcd..50aa8df 100644 --- a/src/abfall.cpp +++ b/src/abfall.cpp @@ -10,9 +10,10 @@ #include "pico/stdlib.h" #include "pico/util/datetime.h" -#include "lwip/apps/http_client.h" +//#include "lwip/apps/http_client.h" #include "ntp_client.h" +#include "http_client.h" #include "ws2812.h" using std::string; @@ -65,40 +66,6 @@ int wifi_setup(uint32_t country, const string &ssid, const string &pw, bool firs return status; } -char myBuffer[2048]; - -void result_callback(void *arg, httpc_result_t httpc_result, u32_t rx_content_len, u32_t srv_res, - err_t err) { - printf("Transfer complete\n"); - printf("Local result=%d\n", httpc_result); - printf("Http result=%d\n", srv_res); -} - -err_t headers_callback(httpc_state_t *connection, void *arg, struct pbuf *hdr, u16_t hdr_len, - u32_t content_len) { - printf("Headers received\n"); - printf("Content length=%d\n", content_len); - printf("Header length=%d\n", hdr_len); - - pbuf_copy_partial(hdr, myBuffer, hdr->tot_len, 0); - printf("Headers \n"); - printf("%s", myBuffer); - - return ERR_OK; -} - -err_t body_callback(void *arg, struct altcp_pcb *conn, struct pbuf *p, err_t err) { - // printf("Body\n"); - pbuf_copy_partial(p, myBuffer, p->tot_len, 0); - // printf("%s", myBuffer); - - // TODO: Parse CSV - - return ERR_OK; -} - - - int main() { const string ssid{"Apis cerana"}; const string pw{"2JkJEh2vptVT"}; @@ -123,16 +90,10 @@ int main() { } while (res != CYW43_LINK_UP); - uint16_t port = 80; - httpc_connection_t settings; - settings.result_fn = nullptr; - settings.headers_done_fn = nullptr; - - err_t err = httpc_get_file_dns("beenas.brodbeck-online.de", port, "/abfall/abfall.csv", - &settings, body_callback, nullptr, nullptr); - // printf("Status %d\n", err); - NtpClient::setDateTime(); + HttpClient client; + std::string test = client.retrieveWasteDatesAsCsv(); + printf("%s\n", test.c_str()); char datetime_buf[256]; char *datetime_str = &datetime_buf[0]; @@ -142,7 +103,7 @@ int main() { rtc_get_datetime(&dt); datetime_to_str(datetime_str, sizeof(datetime_buf), &dt); // printf("DateTime: %s\n", datetime_str); - led.switchColor(Color::RED); + led.switchColor(Color::OFF); sleep_ms(5000); } diff --git a/src/http_client.cpp b/src/http_client.cpp new file mode 100644 index 0000000..4591435 --- /dev/null +++ b/src/http_client.cpp @@ -0,0 +1,61 @@ +#include "http_client.h" + +void result_callback(void *arg, httpc_result_t httpc_result, u32_t rx_content_len, u32_t srv_res, + err_t err) { + printf("Transfer complete\n"); + printf("Local result=%d\n", httpc_result); + printf("Http result=%d\n", srv_res); +} + +err_t headers_callback(httpc_state_t *connection, void *arg, struct pbuf *hdr, u16_t hdr_len, + u32_t content_len) { + printf("Headers received\n"); + printf("Content length=%d\n", content_len); + printf("Header length=%d\n", hdr_len); + + pbuf_copy_partial(hdr, myBuffer, hdr->tot_len, 0); + printf("Headers \n"); + printf("%s", myBuffer); + + return ERR_OK; +} + +err_t body_callback(void *arg, struct altcp_pcb *conn, struct pbuf *p, err_t err) { + bool *test = (bool *)arg; + printf("..."); + pbuf_copy_partial(p, myBuffer, p->tot_len, 0); + // printf("%s", myBuffer); + + // TODO: Parse CSV + *test = true; + + return ERR_OK; +} + +HttpClient::HttpClient() { + m_settings.use_proxy = false; + m_settings.result_fn = nullptr; // result_callback; + m_settings.headers_done_fn = nullptr; // headers_callback; +} + +std::string HttpClient::retrieveWasteDatesAsCsv() { + uint16_t port = 80; + + std::string test(""); + + err_t err = httpc_get_file_dns("beenas.brodbeck-online.de", port, "/abfall/abfall.csv", + &m_settings, body_callback, &m_received, nullptr); + // printf("Status %d\n", err); + + printf("Waiting for waste dates "); + + while (m_received == false) { + sleep_ms(100); + } + + printf(" received.\n"); + + test.append(myBuffer); + + return test; +} \ No newline at end of file diff --git a/src/http_client.h b/src/http_client.h new file mode 100644 index 0000000..c4c81f0 --- /dev/null +++ b/src/http_client.h @@ -0,0 +1,27 @@ +#pragma once + +#include + +#include "lwip/apps/http_client.h" +#include "pico/cyw43_arch.h" +#include "pico/stdlib.h" + +static char myBuffer[2048]; + +extern "C" void result_callback(void *arg, httpc_result_t httpc_result, u32_t rx_content_len, + u32_t srv_res, err_t err); + +extern "C" err_t headers_callback(httpc_state_t *connection, void *arg, struct pbuf *hdr, + u16_t hdr_len, u32_t content_len); + +extern "C" err_t body_callback(void *arg, struct altcp_pcb *conn, struct pbuf *p, err_t err); + +class HttpClient { + public: + HttpClient(); + std::string retrieveWasteDatesAsCsv(); + bool m_received{false}; + + private: + httpc_connection_t m_settings; +};