From a2de9ab8d67b62420eb896ce7f3c6eda30cd8309 Mon Sep 17 00:00:00 2001 From: rasmus Date: Wed, 25 Mar 2026 22:11:01 +0100 Subject: [PATCH] updated the webserver to properly use post --- CMakeLists.txt | 3 +-- lwipopts.h | 3 +++ src/main.cpp | 45 +++++++++++++++++++----------------- src/webserver.c | 61 +++++++++++++++++++++++++++++++++++++++++++------ src/webserver.h | 2 -- 5 files changed, 82 insertions(+), 32 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9cdd037..7bf97cb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,7 +21,7 @@ add_custom_command( OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/src/fsdata.c COMMAND perl ${PICO_SDK_PATH}/lib/lwip/src/apps/http/makefsdata/makefsdata COMMAND ${CMAKE_COMMAND} -E rename fsdata.c src/fsdata.c - COMMAND ${CMAKE_COMMAND} + COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/prepend_include.cmake WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} DEPENDS ${CMAKE_SOURCE_DIR}/fs/index.html @@ -31,7 +31,6 @@ add_custom_command( add_executable(sensor-pico src/main.cpp src/webserver.c - src/fsdata.c ) diff --git a/lwipopts.h b/lwipopts.h index 86cd48a..7515944 100644 --- a/lwipopts.h +++ b/lwipopts.h @@ -1,6 +1,8 @@ #ifndef _LWIPOPTS_H #define _LWIPOPTS_H +#define HTTPD_FSDATA_FILE "src/fsdata.c" + #define NO_SYS 1 #define LWIP_SOCKET 0 #define LWIP_NETCONN 0 @@ -18,6 +20,7 @@ #define LWIP_HTTPD 1 #define LWIP_HTTPD_CGI 1 #define LWIP_HTTPD_SSI 1 +#define LWIP_HTTPD_SUPPORT_POST 1 #define LWIP_NETIF_HOSTNAME 1 #define LWIP_NETIF_STATUS_CALLBACK 1 diff --git a/src/main.cpp b/src/main.cpp index 3803914..25b1b42 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,11 +4,11 @@ #include "lwip/apps/httpd.h" #include "pico/cyw43_arch.h" #include "pico/stdlib.h" -#include "webserver.h" +#include #include #include -void ap_init() { +/* void ap_init() { cyw43_arch_enable_ap_mode("SensorAP", "passwort123", CYW43_AUTH_WPA2_AES_PSK); ip_addr_t gw{}; @@ -18,38 +18,41 @@ void ap_init() { static dhcp_server_t dhcp_server{}; dhcp_server_init(&dhcp_server, &gw, &mask); -} +} */ int main() { - + int ret{}; stdio_init_all(); sleep_ms(3000); cyw43_arch_init(); httpd_init(); - webserver_init(); while (true) { - memset(saved_ssid, 0, sizeof(saved_ssid)); - memset(saved_password, 0, sizeof(saved_password)); - ap_init(); + /* memset(saved_ssid, 0, sizeof(saved_ssid)); + memset(saved_password, 0, sizeof(saved_password)); + ap_init(); */ + printf("Return Code: %d\n", ret); - while (saved_ssid[0] == '\0') { - cyw43_arch_poll(); - sleep_ms(100); - } - cyw43_arch_disable_ap_mode(); + /* while (saved_ssid[0] == '\0') { + cyw43_arch_poll(); + sleep_ms(100); + } */ + // cyw43_arch_disable_ap_mode(); cyw43_arch_enable_sta_mode(); - if (!(cyw43_arch_wifi_connect_timeout_ms(saved_ssid, saved_password, - CYW43_AUTH_WPA2_AES_PSK, 20000))) { + ret = cyw43_arch_wifi_connect_timeout_ms("HainerErnst-IoT", + "vpUaR68xLZzXanS7", + CYW43_AUTH_WPA2_MIXED_PSK, 10000); + if (ret == 0) { break; } - } - while (true) { - cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1); - sleep_ms(200); - cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0); - sleep_ms(200); + while (true) { + cyw43_arch_poll(); + cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1); + sleep_ms(100); + cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0); + sleep_ms(100); + } } } diff --git a/src/webserver.c b/src/webserver.c index 7d3899d..5fe44d0 100644 --- a/src/webserver.c +++ b/src/webserver.c @@ -1,22 +1,69 @@ #include "lwip/apps/httpd.h" #include "string.h" +#include char saved_ssid[33]; char saved_password[65]; -static const char *config_handler(int iIndex, int iNumParams, char *pcParam[], - char *pcValue[]) { - for (int i = 0; i < iNumParams; i++) { - if (strcmp(pcParam[i], "ssid") == 0) { - strncpy(saved_ssid, pcValue[i], 32); +/* static const char *config_handler(int iIndex, int iNumParams, char +*pcParam[], char *pcValue[]) { for (int i = 0; i < iNumParams; i++) { if +(strcmp(pcParam[i], "ssid") == 0) { strncpy(saved_ssid, pcValue[i], 32); } if (strcmp(pcParam[i], "password") == 0) { strncpy(saved_password, pcValue[i], 64); } } return "/index.html"; +} */ + +err_t httpd_post_begin(void *connection, const char *uri, + const char *http_request, u16_t http_request_len, + int content_len, char *response_uri, + u16_t response_uri_len, u8_t *post_auto_wnd) { + if (strcmp(uri, "/config") == 0) { + strncpy(response_uri, "/index.html", response_uri_len); + *post_auto_wnd = 1; + return ERR_OK; + } + return ERR_VAL; } -static const tCGI cgi_handlers[] = {{"/config", config_handler}}; +static char post_buffer[128]; +static uint16_t post_buffer_len = 0; -void webserver_init(void) { http_set_cgi_handlers(cgi_handlers, 1); } +err_t httpd_post_receive_data(void *connection, struct pbuf *p) { + post_buffer_len = + pbuf_copy_partial(p, post_buffer, sizeof(post_buffer) - 1, 0); + post_buffer[post_buffer_len] = '\0'; + pbuf_free(p); + return ERR_OK; +} + +void httpd_post_finished(void *connection, char *response_uri, + u16_t response_uri_len) { + strncpy(response_uri, "/index.html", response_uri_len); + + char *pos = strstr(post_buffer, "ssid="); + pos = pos + 5; + char *end = strchr(pos, '&'); + int len = end - pos; + if (len > 32) { + len = 32; + } + strncpy(saved_ssid, pos, len); + + char *pos2 = strstr(post_buffer, "password=") + 9; + char *end2 = strchr(pos2, '\0'); + int len2 = end2 - pos2; + if (len2 > 64) { + len2 = 64; + } + strncpy(saved_password, pos2, len2); + + saved_ssid[len] = '\0'; + saved_password[len] = '\0'; +} + +// static const tCGI cgi_handlers[] = {{"/config", config_handler}}; + +// void webserver_init(void) { http_set_cgi_handlers(cgi_handlers, 1); } diff --git a/src/webserver.h b/src/webserver.h index 66ad397..cc7b752 100644 --- a/src/webserver.h +++ b/src/webserver.h @@ -8,8 +8,6 @@ extern "C" { extern char saved_ssid[33]; extern char saved_password[65]; -void webserver_init(void); - #ifdef __cplusplus } #endif