From 10bb31b123b272cb71032fd315b8fbdfde0c8328 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Tue, 27 Dec 2022 23:57:24 +0100 Subject: [PATCH] More on TCP --- src/CMakeLists.txt | 1 + src/abfall.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ce11f1c..7f5e8ac 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -28,6 +28,7 @@ pico_enable_stdio_usb(${CMAKE_PROJECT_NAME} 0) target_link_libraries(${CMAKE_PROJECT_NAME} pico_stdlib pico_cyw43_arch_lwip_threadsafe_background + pico_lwip_http ) pico_add_extra_outputs(${CMAKE_PROJECT_NAME}) diff --git a/src/abfall.cpp b/src/abfall.cpp index 0511394..bc9f4e4 100644 --- a/src/abfall.cpp +++ b/src/abfall.cpp @@ -2,14 +2,17 @@ #include +#include "lwip/apps/http_client.h" #include "pico/cyw43_arch.h" #include "pico/stdlib.h" using std::string; -int wifi_setup(uint32_t country, const string &ssid, const string &pw) { - if (cyw43_arch_init_with_country(country)) { - return 1; +int wifi_setup(uint32_t country, const string &ssid, const string &pw, bool firstTry = true) { + if (firstTry) { + if (cyw43_arch_init_with_country(country)) { + return 1; + } } cyw43_arch_enable_sta_mode(); @@ -28,6 +31,9 @@ int wifi_setup(uint32_t country, const string &ssid, const string &pw) { if (status_new != status) { status = status_new; + if (status < 0) { + continue; + } flashrate = flashrate / (status + 1); printf("Connect status: %d %d\n", status, flashrate); } @@ -48,6 +54,38 @@ int wifi_setup(uint32_t country, const string &ssid, const string &pw) { 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"}; @@ -55,7 +93,28 @@ int main() { stdio_init_all(); - wifi_setup(country, ssid, pw); + bool firstTry = true; + int res = -1; + do { + if (firstTry) { + res = wifi_setup(country, ssid, pw, true); + firstTry = false; + } else { + printf("Setting up connection failed. Trying again after 5 sec...\n"); + sleep_ms(5000); + res = wifi_setup(country, ssid, pw, false); + } + + } 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); while (true) { sleep_ms(1);