More on TCP

This commit is contained in:
Martin Brodbeck 2022-12-27 23:57:24 +01:00
parent 65d26b5e3d
commit 10bb31b123
2 changed files with 64 additions and 4 deletions

View file

@ -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})

View file

@ -2,14 +2,17 @@
#include <string>
#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);